我已經在這裏搜索過,而且我無法找到如何根據其屬性過濾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。
你確定它不工作?我剛剛嘗試過,它確實產生了您的預期輸出! – 2012-07-18 16:23:01
@ tim-c你好。是的,我用Java嘗試了幾次,沒有任何結果。它給了我我發佈的結果。它似乎沒有檢查第二種情況:( – StrayChild01 2012-07-18 16:34:30