File: Data.xml
<?xml version="1.0"?>
<results group="A">
<match>
<date>10-Jun-98</date>
<team score="2">Brazil</team>
<team score="1">Scotland</team>
</match>
<match>
<date>10-Jun-98</date>
<team score="2">Morocco</team>
<team score="2">Norway</team>
</match>
<match>
<date>16-Jun-98</date>
<team score="1">Scotland</team>
<team score="1">Norway</team>
</match>
</results>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:comment>
<xsl:value-of select="name()" />
<xsl:for-each select="ancestor::*">
<xsl:sort select="position()" order="descending" />
<xsl:text> within </xsl:text>
<xsl:value-of select="name()" />
</xsl:for-each>
</xsl:comment>
<xsl:apply-templates />
</xsl:template>
</xsl:transform>
Output:
<?xml version="1.0" encoding="UTF-8"?><!--results-->
<!--match within results-->
<!--date within match within results-->10-Jun-98
<!--team within match within results-->Brazil
<!--team within match within results-->Scotland
<!--match within results-->
<!--date within match within results-->10-Jun-98
<!--team within match within results-->Morocco
<!--team within match within results-->Norway
<!--match within results-->
<!--date within match within results-->16-Jun-98
<!--team within match within results-->Scotland
<!--team within match within results-->Norway