How to use XPath to find the closest ancestor in prepositions or ancestors

I have a structure with repeating elements like this:

<a>
  <b>
    <a>
    </a>
  </b>
  <a>
    <b>
       <a>
         <c att="val" />
       </a>
    </b>
  </a>
</a>

When using c-node is $ 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 bring the node closer to the tree, as to the deepest in the branches or to the highest level.

I cannot use the maximum XPath 2 function, like this, unfortunately:

<xsl:value-of select="($currentNode/ancestor-or-self::a)[max(@level)]" />

Note that the closest a-node is not always exactly above the context, somewhere there ...

Any suggestions appreciated!

Relationship Alex

+5
source share
1 answer

I think you get an ordered set of nodes, from parent to ancestor.

Try $currentNode/ancestor-or-self::a[1]to get the parent element c att = "val".

+6
source

All Articles