XSL function to find parent node name

If you want to find the node name of the current element's parent, use local-name(parent::*):
  • The local-name function returns the name of its argument (without the namespace) or the name of the current element if it's called without an argument;
  • The parent:: axis selects the current element's parent;
  • The parent::* matches any node that is the parent of the current element.
For example, given the following input data …
<book>
  <chapter>
    <section>
      <title>My section</title>
      <para>My paragraph</para>
    </section>
  </chapter>
</book>
… this XSL template will output “My parent is: section
<xsl:template match="para">
  My parent is: <xsl:value-of select="local-name(parent::*)" />
</xsl:template>

This post is part of You've asked for it series of articles.

No comments:

Post a Comment