2012-02-27 65 views
3

我有下面的XML文檔清除從節點值和XML文件使用XSLT

<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" solutionVersion="1.0.0.65" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="https://devcoop.oceanspray.com/sites/TCM2/FormServerTemplates/TCM-TCP.xsn"?> 
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?> 
<my:myFields xml:lang="en-US" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"> 
    <my:Header> 
    <my:ProjectName>Something</my:ProjectName> 
    <my:ProjectCreationDate>2/21/2012 12:00 AM</my:ProjectCreationDate> 
    </my:Header> 
    <my:Details> 
    <my:DescriptionOfProposal> 
     Will be filled in when saved<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml"><p>testestdsgsgasfgf a<strong>asdf </strong>adf asdf asdf as</p></html> 
    </my:DescriptionOfProposal> 
    <my:PurposeOfChange>Will be filled in when saved</my:PurposeOfChange> 
    </my:Details> 
</my:myFields> 

我想運行此文件,將清除出我的價值在XSLT文件:DescriptionOfPropsal節點。它將完全清除文本和子節點。我有以下XSLT樣式表

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:my="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//my:DescriptionOfProposal"> 
    <my:DescriptionOfProposal> 
    </my:DescriptionOfProposal> 
    </xsl:template> 


</xsl:stylesheet> 

但是,這似乎並沒有工作。我用它做了一堆測試,我相信它與節點上的命名空間有關,因爲我可以對另一個沒有命名空間的簡單文檔執行相同類型的轉換,並且可以使其工作。

回答

1

這很明顯 - 您使用的是不正確的命名空間

提供的XML文檔中的my:DescriptionOfProposal節點所屬的命名空間:

"http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" 

然而,在你的XSLT代碼,你的"my:"前綴不同的命名空間相關聯:

"urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" 

解決方案

更正轉換中的名稱空間。

更換

xmlns:my="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" 

xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" 

隨着這一變化,其中最模板輕微的重構,你的改造就變成了:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="my:DescriptionOfProposal/node()"/> 
</xsl:stylesheet> 

,當它被應用所提供的XML文檔

<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" solutionVersion="1.0.0.65" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="https://devcoop.oceanspray.com/sites/TCM2/FormServerTemplates/TCM-TCP.xsn"?> 
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?> 
<my:myFields xml:lang="en-US" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"> 
    <my:Header> 
     <my:ProjectName>Something</my:ProjectName> 
     <my:ProjectCreationDate>2/21/2012 12:00 AM</my:ProjectCreationDate> 
    </my:Header> 
    <my:Details> 
     <my:DescriptionOfProposal>  Will be filled in when saved 
      <html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml"> 
       <p>testestdsgsgasfgf a 
        <strong>asdf </strong>adf asdf asdf as 
       </p> 
      </html> 
     </my:DescriptionOfProposal> 
     <my:PurposeOfChange>Will be filled in when saved</my:PurposeOfChange> 
    </my:Details> 
</my:myFields> 

想要的,正確的結果產生

<?xml version="1.0" encoding="UTF-16"?> 
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" solutionVersion="1.0.0.65" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="https://devcoop.oceanspray.com/sites/TCM2/FormServerTemplates/TCM-TCP.xsn"?> 
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?> 
<my:myFields xml:lang="en-US" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"> 
    <my:Header> 
     <my:ProjectName>Something</my:ProjectName> 
     <my:ProjectCreationDate>2/21/2012 12:00 AM</my:ProjectCreationDate> 
    </my:Header> 
    <my:Details> 
     <my:DescriptionOfProposal></my:DescriptionOfProposal> 
     <my:PurposeOfChange>Will be filled in when saved</my:PurposeOfChange> 
    </my:Details> 
</my:myFields> 
+0

你會介意查看我的答案嗎? :) – 2012-02-28 04:54:52

+0

謝謝你完美的作品 – 2012-02-28 13:27:01

+0

@PaulCavacas:不客氣。 – 2012-02-28 14:23:53

0

你勢必my的命名空間是在XML和XSLT不同:

XML:

xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" 

XSLT:

xmlns:my="urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22" 

要麼更改命名空間中的XSLT或添加一個新的。例如: -

xmlns:my2="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-20T14:33:22" 

,然後在模板:

<xsl:template match="my2:DescriptionOfProposal"> 
    <my2:DescriptionOfProposal/> 
</xsl:template> 

注意,//是完全不必要的。

+0

我更改了XSLT,以使我的命名空間是一樣什麼是XML文檔中去除,並從模板匹配//,但它仍然不清除價值 – 2012-02-28 13:16:31

1

至於其他人已經確診的命名空間問題,並發現一也請修復,請參閱答案以更正您的代碼。不過我想建議你不要糾正那些命名空間。它的contains() function使用。我知道這是不是有用這裏在這種情況下,但只是分享知識我寫的解決方案:)

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node()[contains(name(),'DescriptionOfProposal')]"> 
    <xsl:copy/> 
    </xsl:template> 
</xsl:stylesheet> 

上面的代碼沒有關心過你正在使用的XML文件什麼的命名空間: )

這是該替代..

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node()[parent:: node()[contains(name(),'DescriptionOfProposal')]]"/> 
</xsl:stylesheet> 

可以使用它們中的:) 編輯:第二方法至少優選的,因爲反向軸:)
這種方法是根據Dimitre的建議加入...

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node()[local-name() = 'DescriptionOfProposal']"> 
    <xsl:copy/> 
    </xsl:template> 
</xsl:stylesheet> 
+1

一般很好的答案。可以有一些改進。 1. contains(name(),'DescriptionOfProposal')'太籠統了。使用更精確:'local-name()='DescriptionOfProposal'' 2.只要有可能,最好*不使用反向軸。所以,我建議編寫'* [local-name()='DescriptionOfProposal']/node()'這比node()更容易理解[parent :: node()[contains(name(),'DescriptionOfProposal ')]]'3.由於您沒有使用任何擴展功能,因此可以安全地刪除第3行和第4行。最後,我個人更喜歡'xsl:output'上的'omit-xml-declaration =「yes」'。 – 2012-02-28 05:05:24

+0

非常感謝你:) – 2012-02-28 05:39:23

+0

嬰兒程序員'Aravind':不客氣:) – 2012-02-28 06:04:15