这是执行此操作的一种方法:
<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:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="UnitID|DateLastModified"/>
<Identification StoredUnderName="true">
<xsl:apply-templates select=
"*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
concat('|',name(),'|')))]"/>
<IdentificationDate>
<xsl:apply-templates select="StartMonth|StartYear"/>
</IdentificationDate>
<xsl:apply-templates select="TypeStatus"/>
</Identification>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<Unit>
<UnitID>K000009107</UnitID>
<DateLastModified>2003-06-23</DateLastModified>
<Family>SAPOTACEAE</Family>
<Genus>Pouteria</Genus>
<Species>ferrugineo-tomentos</Species>
<Identifier>Smith, J</Identifier>
<StartMonth>05</StartMonth>
<StartYear>1997</StartYear>
<TypeStatus>Type</TypeStatus>
</Unit>
产生了想要的正确结果:
<Unit>
<UnitID>K000009107</UnitID>
<DateLastModified>2003-06-23</DateLastModified>
<Identification StoredUnderName="true">
<Family>SAPOTACEAE</Family>
<Genus>Pouteria</Genus>
<Species>ferrugineo-tomentos</Species>
<Identifier>Smith, J</Identifier>
<IdentificationDate>
<StartMonth>05</StartMonth>
<StartYear>1997</StartYear>
</IdentificationDate>
<TypeStatus>Type</TypeStatus>
</Identification>
</Unit>
这种转换可以以失去灵活性为代价缩短一点:
<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:template match="/*">
<xsl:copy>
<xsl:copy-of select="UnitID|DateLastModified"/>
<Identification StoredUnderName="true">
<xsl:copy-of select=
"*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
concat('|',name(),'|')))]"/>
<IdentificationDate>
<xsl:copy-of select="StartMonth|StartYear"/>
</IdentificationDate>
<xsl:copy-of select="TypeStatus"/>
</Identification>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>