2011-12-08 34 views
2

我試圖從XML文件中過濾掉不需要的數據。 我有以下XML:如何根據他的一個孩子的值使用XSLT刪除完整節點

<Client> 
    <Address> 
    </Address> 
    <Documents> 
     <Invoice> 
      <Document></Document> 
      <No>9999<No> 
      <Content>H4s==</Content> 
     </Invoice> 
     <Invoice> 
      <Document> 
       <File> 
        <Code>THIS IS THE ONE<Code> 
        <Name> 
         <value locale="en">Invoice for January</value> 
        </Name> 
       </File> 
      </Document> 
      <No>7777</No> 
      <Content>H4sIAA1XyDQA=</Content> 
     </Invoice> 
    </Documents> 
</Client> 

而且我不希望在結果中的所有<Invoice>其中<Document>標籤是空的。

因此,在某種意義上,結果會是什麼樣子:

<Client> 
    <Address> 
    </Address> 
    <Documents> 
     <Invoice> 
      <Document> 
       <File> 
        <Code>THIS IS THE ONE<Code> 
        <Name> 
         <value locale="en">Invoice for January</value> 
        </Name> 
       </File> 
      </Document> 
      <No>7777</No> 
      <Content>H4sIAA1XyDQA=</Content> 
     </Invoice> 
    </Documents> 
</Client> 

感謝,

回答

2

你會做到這一點通過重寫的身份模板。

這XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="Invoice[not(Document/node())]"/> 

</xsl:stylesheet> 

施加在您的XML輸入(固定爲合格的):

<Client> 
    <Address> 
    </Address> 
    <Documents> 
    <Invoice> 
     <Document></Document> 
     <No>9999</No> 
     <Content>H4s==</Content> 
    </Invoice> 
    <Invoice> 
     <Document> 
     <File> 
      <Code>THIS IS THE ONE</Code> 
      <Name> 
       <value locale="en">Invoice for January</value> 
      </Name> 
     </File> 
     </Document> 
     <No>7777</No> 
     <Content>H4sIAA1XyDQA=</Content> 
    </Invoice> 
    </Documents> 
</Client> 

產生以下輸出:

<Client> 
    <Address/> 
    <Documents> 
     <Invoice> 
     <Document> 
      <File> 
       <Code>THIS IS THE ONE</Code> 
       <Name> 
        <value locale="en">Invoice for January</value> 
       </Name> 
      </File> 
     </Document> 
     <No>7777</No> 
     <Content>H4sIAA1XyDQA=</Content> 
     </Invoice> 
    </Documents> 
</Client> 
+0

1爲一個正確答案。 –

+0

謝謝。那做了這個工作。 – Ergosum

+0

@Ergosum - 不客氣。請考慮通過點擊答案旁邊的複選標記來接受我的答案。謝謝! –

相關問題