就这么简单:
<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:param name="pMajor" select="1"/>
<xsl:param name="pMinor" select="2"/>
<xsl:template match="/*/*">
<xsl:copy-of select=
"self::*[*/*[@MajorVersion = $pMajor and @MinorVersion = $pMinor]]"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时(提供的格式严重错误且无用!):
<MyXml>
<Machine1>
<SupportedOS>
<OS1 MajorVersion="1" MinorVersion="2"/>
<OS2 MajorVersion="2" MinorVersion="0"/>
<OS3 MajorVersion="1" MinorVersion="1"/>
<OS4 MajorVersion="2" MinorVersion="1"/>
<OS5 MajorVersion="3" MinorVersion="0"/>
</SupportedOS>
</Machine1>
<Machine2>
<SupportedOS>
<OS1 MajorVersion="1" MinorVersion="3"/>
<OS2 MajorVersion="2" MinorVersion="0"/>
<OS3 MajorVersion="1" MinorVersion="1"/>
<OS4 MajorVersion="2" MinorVersion="1"/>
<OS5 MajorVersion="3" MinorVersion="0"/>
</SupportedOS>
</Machine2>
</MyXml>
产生了想要的正确结果:
<Machine1>
<SupportedOS>
<OS1 MajorVersion="1" MinorVersion="2"/>
<OS2 MajorVersion="2" MinorVersion="0"/>
<OS3 MajorVersion="1" MinorVersion="1"/>
<OS4 MajorVersion="2" MinorVersion="1"/>
<OS5 MajorVersion="3" MinorVersion="0"/>
</SupportedOS>
</Machine1>