xpath.location

xpath.location — Calculate the XPath child-sequence to the current node

Description

The xpath.location template calculates the absolute path from the root of the tree to the current element node.

<xsl:template name="xpath.location">
  <xsl:param name="node" select="."></xsl:param>
  <xsl:param name="path" select="''"></xsl:param>

  <xsl:variable name="next.path">
    <xsl:value-of select="local-name($node)"></xsl:value-of>
    <xsl:if test="$path != ''">/</xsl:if>
    <xsl:value-of select="$path"></xsl:value-of>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$node/parent::*">
      <xsl:call-template name="xpath.location">
        <xsl:with-param name="node" select="$node/parent::*"></xsl:with-param>
        <xsl:with-param name="path" select="$next.path"></xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>/</xsl:text>
      <xsl:value-of select="$next.path"></xsl:value-of>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>