一、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>
应用于提供的 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 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>