我有一个id列表,我想用它来'过滤'提供的xml文件。列表中的每个节点都应保留,而不在列表中的每个节点都应删除。
对我来说,困难是当我删除一个节点(不在IDList中)时,我也最终删除了一个可能在IDList中的子节点。
最后,我需要保持节点的嵌套(参见下面的示例)。
这是我的id列表
IDList = 2,3,5,6
这是我的输入xml是:
<Associations> <Association ASSOCIATION_ID="1"> Elmwood Association <Homes/> </Association> <Association ASSOCIATION_ID="2"> Oakwood Association <Homes/> <Association ASSOCIATION_ID="4"> Oakwood Sub Association A <Homes/> <Association ASSOCIATION_ID="6"> Oakwood Sub Sub Association <Homes/> </Association> </Association> <Association ASSOCIATION_ID="5"> Oakwood Sub Association B <Homes/> </Association> </Association> <Association ASSOCIATION_ID="3"> Cedarwood Association <Homes/> </Association> </Associations>
这是我期望的输出:
<Associations> <Association ASSOCIATION_ID="2"> Oakwood Association <Homes/> <Association ASSOCIATION_ID="6"> Oakwood Sub Sub Association <Homes/> </Association> <Association ASSOCIATION_ID="5"> Oakwood Sub Association B <Homes/> </Association> </Association> <Association ASSOCIATION_ID="3"> Cedarwood Association <Homes/> </Association> </Associations>
任何帮助将非常感激!
XSLT 2.0解决方案:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pIds" select="2, 3, 5, 6" as="xs:integer+"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template priority="2" match=
"Association[not(@ASSOCIATION_ID = $pIds)
and not(.//Association[@ASSOCIATION_ID = $pIds])]"/>
<xsl:template match=
"Association[@ASSOCIATION_ID = $pIds]
//Association[not(@ASSOCIATION_ID = $pIds)]
|
*[not(self::Association[@ASSOCIATION_ID = $pIds])]
/node()[not(descendant-or-self::Association[@ASSOCIATION_ID = $pIds])]
">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<Associations>
<Association ASSOCIATION_ID="1">
Elmwood Association
<Homes/>
</Association>
<Association ASSOCIATION_ID="2">
Oakwood Association
<Homes/>
<Association ASSOCIATION_ID="4">
Oakwood Sub Association A
<Homes/>
<Association ASSOCIATION_ID="6">
Oakwood Sub Sub Association
<Homes/>
</Association>
</Association>
<Association ASSOCIATION_ID="5">
Oakwood Sub Association B
<Homes/>
</Association>
</Association>
<Association ASSOCIATION_ID="3">
Cedarwood Association
<Homes/>
</Association>
</Associations>
生成所需的正确结果:
<Associations>
<Association ASSOCIATION_ID="2">
Oakwood Association
<Homes/>
<Association ASSOCIATION_ID="6">
Oakwood Sub Sub Association
<Homes/>
</Association>
<Association ASSOCIATION_ID="5">
Oakwood Sub Association B
<Homes/>
</Association>
</Association>
<Association ASSOCIATION_ID="3">
Cedarwood Association
<Homes/>
</Association>
</Associations>
二世。XSLT 1.0解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:ids>
<id>2</id>
<id>3</id>
<id>5</id>
<id>6</id>
</my:ids>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template priority="2" match=
"Association[not(@ASSOCIATION_ID = document('')/*/my:ids/*)
and not(.//Association[@ASSOCIATION_ID = document('')/*/my:ids/*])]"/>
<xsl:template match=
"Association[@ASSOCIATION_ID = document('')/*/my:ids/*]
//Association[not(@ASSOCIATION_ID = document('')/*/my:ids/*)]
|
*[not(self::Association[@ASSOCIATION_ID = document('')/*/my:ids/*])]
/node()[not(descendant-or-self::Association
[@ASSOCIATION_ID = document('')/*/my:ids/*])]
">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>