XPath: get root node from node -set from specified node

Is it possible to write an XPath expression that gets the root of a node node within a node-set, only with a reference to the node?

Using "/" will not work for me, because it only refers to the root of the input document. I would also like it to work without context and use it for a generic node-set that can be created dynamically during processing.

For instance...

<xsl:function name="my:getRoot"> <xsl:param name="n" /> <xsl:variable name="rootnode" select="some_solution($n)"/> </xsl:function> 

Thanks for the help.

+4
source share
1 answer

In XPath 1.0 use :

 ancestor-or-self::node()[last()] 

This selects the farthest of the ancestors of the current node - which is its document node.

In XPath 2.0 use :

  root(.) 
+7
source

Source: https://habr.com/ru/post/1399087/


All Articles