I need to iterate through the XML and print only those elements that have a specific attribute. For example -
<root>
<fruits>
<name>apple</name>
<size ps:oldvalue="big">small</size>
<fruits>
<veggies>
<name>cabbage</name>
<size>small</size>
<price ps:oldvalue="30.0">40.0</price>
</veggies>
</root>
因此,我需要获取具有oldvalue属性的所有元素,并且输出应该具有名称和具有oldvalue属性的元素:
<root>
<name>apple<name>
<size ps:oldvalue="big">small</size>
<name>cabbage</name>
<price ps:oldvalue="30.0">40.0</price>
</root>
我已经看过使用身份模板,但一直没有成功:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[(name()='@ps:oldvalue')]"/>
答案 0 :(得分:0)
当我将其更正为
时,您的输入格式不正确<root xmlns:ps="http://example.com/ps">
<fruits>
<name>apple</name>
<size ps:oldvalue="big">small</size>
</fruits>
<veggies>
<name>cabbage</name>
<size>small</size>
<price ps:oldvalue="30.0">40.0</price>
</veggies>
</root>
并申请
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:ps="http://example.com/ps">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[*/@ps:oldvalue]">
<xsl:apply-templates select="name | *[@ps:oldvalue]"/>
</xsl:template>
</xsl:transform>
我得到了
<root xmlns:ps="http://example.com/ps">
<name>apple</name><size ps:oldvalue="big">small</size>
<name>cabbage</name><price ps:oldvalue="30.0">40.0</price>
</root>