此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Hierarchy[position() != 1]"/>
<xsl:template match="Hierarchy[1]">
<xsl:apply-templates select="." mode="Hierarchy"/>
</xsl:template>
<xsl:template match="Hierarchy" mode="Hierarchy">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="following-sibling::Hierarchy[1]" mode="Hierarchy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<root>
<Element1>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</Element1>
<AnotherElement>
<YetAnotherElement>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</YetAnotherElement>
</AnotherElement>
</root>