XPath: Get root node of a node-set from a specified node
Asked Answered
C

1

5

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

Using "/" won't work for me because that only refers to the input document root. Also I would like it to work without a context and to use it for a general node-set that may be created dynamically during processing.

For example...

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

Thanks for the help.

Catalogue answered 29/2, 2012 at 21:20 Comment(0)
R
8

In XPath 1.0 use:

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

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

In XPath 2.0 use:

 root(.)
Rajiv answered 29/2, 2012 at 21:28 Comment(1)
Thank you Dimitre, I figured there was a simple solution for Xpath 2.0, just couldn't find it in the docs. And +1 for the Xpath 1.0 solution.Catalogue

© 2022 - 2024 — McMap. All rights reserved.