Alex
Alex

Reputation: 2011

How to use XPath to find the closest ancestor in a ancestor-or-self nodelist

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

Upvotes: 8

Views: 7435

Answers (1)

Istao
Istao

Reputation: 7585

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".

Upvotes: 9

Related Questions