使用xslt基于属性值过滤XML
我在这里搜索过,但找不到如何根据属性过滤xml。我有以下xml:使用xslt基于属性值过滤XML,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我在这里搜索过,但找不到如何根据属性过滤xml。我有以下xml: <?xml version="1.0" encoding="utf-8"?> <document> <document_head> <title>This is the title</title> <version>This is the
<?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>
这是标题
这是标题
这是一个应该出现在结果中的文本
13px
#000000
当前用户
这是其他一些不应该在那里的文本
随便找个家伙
这并不意味着什么。
确实如此,因此应该在那里。
我期望这一结果:
<?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>
这是标题
这是标题
这是一个应该出现在结果中的文本
13px
#000000
当前用户
确实如此,因此应该在那里。
目前,我有以下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>
这是标题
这是标题
这是一个应该出现在结果中的文本
13px
#000000
当前用户
你知道我错过了什么吗
谢谢
更新:该代码似乎适用于另一个XSLT处理器,但不适用于Java Transformer。我确信您的条件应该适用!然而,这里有两种可供选择的检查方法,所以试试看这是否有区别
<xsl:template match="document_body/paragraph[not(@id='AXD' or @id='RRR')]"/>
<xsl:template match="document_body/paragraph[@id != 'AXD' and @id != 'RRR']"/>
您确定它不工作吗?我刚试过,它确实产生了你的预期输出@嗨,蒂姆。是的,我用Java试了好几次,什么都没有。它给出了我发布的结果。它似乎没有检查第二个条件:(这很有效!非常感谢。旁白:是否有可能通过和进行比较来改变它?我知道这有点不相关,但它在这里为我省去了另一个问题。我不确定我是否完全理解,但为了以防万一,我已经编辑了答案。也许另一个问题最好,这没有害处!对不起。这是关于操作员的问题或者它本身。是的,两个答案都有效。非常感谢。