这种转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kContained" match="myCompareInfo"
use="boolean(/*/*/containedInfo[contains(., current())])"/>
<xsl:template match="/*">
<OutputInfo><xsl:apply-templates/></OutputInfo>
</xsl:template>
<xsl:template match="outerInfo">
<Package><xsl:apply-templates/></Package>
</xsl:template>
<xsl:template match="myCompareInfo">
<PnNum><xsl:apply-templates/></PnNum>
<Status>available</Status>
</xsl:template>
<xsl:template match=
"myCompareInfo
[count(.|key('kContained', 'true'))
=
count(key('kContained', 'true'))
]">
<PnNum><xsl:apply-templates/></PnNum>
<Status>split</Status>
</xsl:template>
<xsl:template match="containedInfo"/>
</xsl:stylesheet>
当应用于(更正为格式良好的)提供的 XML 文档时:
<PackageInfo>
<outerInfo>
<myCompareInfo>12345</myCompareInfo>
</outerInfo>
<outerInfo>
<myCompareInfo>67890</myCompareInfo>
<containedInfo>12345</containedInfo>
</outerInfo>
<outerInfo>
<myCompareInfo>55667</myCompareInfo>
<containedInfo>12345-C</containedInfo>
</outerInfo>
</PackageInfo>
产生想要的正确结果:
<OutputInfo>
<Package>
<PnNum>12345</PnNum>
<Status>split</Status>
</Package>
<Package>
<PnNum>67890</PnNum>
<Status>available</Status>
</Package>
<Package>
<PnNum>55667</PnNum>
<Status>available</Status>
</Package>
</OutputInfo>
二、XSLT 2.0 解决方案:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kContained" match="myCompareInfo"
use="boolean(/*/*/containedInfo[contains(., current())])"/>
<xsl:template match="/*">
<OutputInfo><xsl:apply-templates/></OutputInfo>
</xsl:template>
<xsl:template match="myCompareInfo">
<PnNum><xsl:apply-templates/></PnNum>
<Status>available</Status>
</xsl:template>
<xsl:template match=
"myCompareInfo[. intersect key('kContained', true())]">
<PnNum><xsl:apply-templates/></PnNum>
<Status>split</Status>
</xsl:template>
<xsl:template match="outerInfo">
<Package><xsl:apply-templates/></Package>
</xsl:template>
<xsl:template match="containedInfo"/>
</xsl:stylesheet>