I have a structure with recurring elements like this:
<a>
<b>
<a>
</a>
</b>
<a>
<b>
<a>
<c att="val" />
</a>
</b>
</a>
</a>
Asuming the c-node is the $currentNode, when I use XPath
<xsl:value-of select="($currentNode/ancestor-or-self::a)" />
I get an unordered list of nodes matching the expression. What I need is to always get the node closest up the tree, as in deepest in the branches or the highest @level.
I cannot use XPath 2 max-function like this unfortunately:
<xsl:value-of select="($currentNode/ancestor-or-self::a)[max(@level)]" />
Notice that the closest a-node not always is exactly above the context, just somewhere up there...
Any suggestions appreciated!
Regards Alex
Definition of XPath Ancestor. The ancestor axis chooses all of the current node's ancestor elements (parent, grandparent, great-grandparents, and so on). The root node is always present on this axis (unless the current node is the root node).
Hey Hemant, we can use the double dot (“..”) to access the parent of any node using the XPath. For example – The locator //span[@id=”first”]/.. will return the parent of the span element matching id value as 'first.
I think you get an ordered set of nodes, from parent to ancestor.
Try $currentNode/ancestor-or-self::a[1]
to get parent of c att="val".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With