Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt: Is there any way to reference the root node?

Tags:

xslt

xslt-1.0

Is there any way to get access to the ROOT node from within another context?

The example above is just to show my intention. Getting access to ROOT_NODE without using '../../..' since changes in xml could break that type of selector.

XSLT

<div class="column">
  <xsl:for-each select="languages/server/elem">
    <!-- Context is ELEM node -->
    <div>
      <!-- How can I get access to the ROOT_NODE ?-->
      <span class="text"><xsl:value-of select="ROOT_NODE/@title"/></span>
      <!-- Print ELEM text -->
      <span class="text"><xsl:value-of select="current()"/></span>
    </div>
  </xsl:for-each>
</div>
like image 702
kitimenpolku Avatar asked Mar 06 '23 07:03

kitimenpolku


1 Answers

The expression "/" selects the document node at the root of the tree containing the context node. (All trees in 1.0 are rooted at document nodes.)

In XSLT 2.0, root() selects the root of the tree containing the context node whether or not the root is a document node.

To get the root of the principal source document even when the context node is a node in a different tree, bind a global variable

<xsl:variable name="principal-root" select="/"/>

which you can refer to anywhere as $principal-root.

Oh, and as Mads Hansen points out, if by "root node" you actually mean the outermost element node, as distinct from the document node, then you would typically use "/*".

like image 156
Michael Kay Avatar answered Mar 13 '23 08:03

Michael Kay