Output the full path of an element
Facts - XSLT
Thursday, 29 November 2007 22:04

The following XML Transform outputs HTML table with a single column. Each row in the table contains the full path of each element in the input xml file:

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
 
<xsl:template match="/">
  <html>
    <table>
      <xsl:apply-templates/>
    </table>
  </html>
</xsl:template>
 
<xsl:template match="*">
  <!-- only elements exist in customerFacingOrder, no attributes -->
  <tr>
    <td>
      <xsl:apply-templates select="." mode="get-full-path"/>
    </td>
  </tr>
  <xsl:apply-templates select="./*"/>
</xsl:template>
 
<xsl:template match="node()" mode="get-full-path">
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:value-of select="name()" /><xsl:text>/</xsl:text>
  </xsl:for-each>
 
</xsl:template>
 
</xsl:stylesheet>