2012-07-18 100 views
3

我已經在這裏搜索過,而且我無法找到如何根據其屬性過濾xml。我有這樣的XML:使用xslt過濾基於屬性值的XML

 




    <?xml version="1.0" encoding="utf-8"?> 
     <document> 
      <document_head> 
       <title>This is the title</title> 
       <version>This is the title</version> 
      </document_head> 
      <document_body> 
       <paragraph id="AXD"> 
        <text> 
         This is a text that should be in the result 
        </text> 
        <properties> 
         <size>13px</size> 
         <color>#000000</color> 
        </properties> 
        <author>Current user</author> 
       </paragraph> 
       <paragraph id="SFI"> 
        <properties> 
         <text> 
          This is some other text that should not be in there 
         </text> 
        </properties> 
       </paragraph> 
       <paragraph id="SFA"> 
        <author>Some random guy</author> 
       </paragraph>  
       <paragraph id="ARG"> 
        This doesn't mean anything. 
       </paragraph> 
       <paragraph id="RRR"> 
        This does, hence should be in there. 
       </paragraph> 
      </document_body> 
     </document> 

 

我期待這樣的結果:

 


    <?xml version="1.0" encoding="UTF-8"?> 
    <document> 
     <document_head> 
      <title>This is the title</title> 
      <version>This is the title</version> 
     </document_head> 
     <document_body> 
      <paragraph id="AXD"> 
       <text> 
        This is a text that should be in the result 
       </text> 
       <properties> 
        <size>13px</size> 
        <color>#000000</color> 
       </properties> 
       <author>Current user</author> 
      </paragraph> 
      <paragraph id="RRR"> 
       This does, hence should be in there. 
      </paragraph>  
     </document_body> 
    </document> 

 

目前,我有這樣的XSLT:

 


    <?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output method="xml" indent="yes"/> 
     <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     </xsl:template> 

     <xsl:template match="document_body/paragraph[not(@id='AXD')][not(@id='RRR')]" /> 
    </xsl:stylesheet> 

 

將會產生這個XML:

 




    <?xml version="1.0" encoding="UTF-8"?> 
     <document> 
      <document_head> 
       <title>This is the title</title> 
       <version>This is the title</version> 
      </document_head> 
      <document_body> 
       <paragraph id="AXD"> 
        <text> 
         This is a text that should be in the result 
        </text> 
        <properties> 
         <size>13px</size> 
         <color>#000000</color> 
        </properties> 
        <author>Current user</author> 
       </paragraph>  
      </document_body> 
     </document> 

 

你知道我錯過了什麼嗎?

謝謝。

更新:看起來代碼適用於另一個XSLT處理器,但它不適用於Java Transformer。

+0

你確定它不工作?我剛剛嘗試過,它確實產生了您的預期輸出! – 2012-07-18 16:23:01

+0

@ tim-c你好。是的,我用Java嘗試了幾次,沒有任何結果。它給了我我發佈的結果。它似乎沒有檢查第二種情況:( – StrayChild01 2012-07-18 16:34:30

回答

6

我相信你的情況應該可以工作!但是,這裏有幾種可供選擇的方法來檢查,所以請給出一個解決方案,以確定是否有所作爲。

<xsl:template match="document_body/paragraph[not(@id='AXD' or @id='RRR')]"/> 

<xsl:template match="document_body/paragraph[@id != 'AXD' and @id != 'RRR']"/> 
+0

它的工作原理!非常感謝。旁邊的問題:是否有可能通過AND來比較這個OR?我知道這是一種無關的,但它爲我節省了另一個問題 – StrayChild01 2012-07-18 16:46:59

+0

我不確定我是否完全理解,但爲了以防萬一,我編輯了回答,也許另一個問題是最好的,沒有什麼壞處! – 2012-07-18 16:51:49

+0

對不起,關於操作員本身。這兩個答案的工作。非常感謝。 – StrayChild01 2012-07-18 17:08:08