Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting parent's parent from current node in Xpath 2.0

Tags:

xml

xslt

xpath

I always seem to have trouble with xpath axis expressions...

In some expressions I have used ../ to refer to the parent node, but is that not valid for test expressions? Or is my syntax just wrong?

<xsl:when test="../../[@status='current']">

My goal is to apply an attribute inside the xsl:when IF the parent's parent has a status attribute with a value of 'current'.

EDIT: self::parent/parent[@status='current'] is a valid xpath expression and may be what I want, can anyone confirm? I might not be going far enough.

like image 382
meder omuraliev Avatar asked Aug 06 '12 18:08

meder omuraliev


People also ask

How do you access the parent of a node with XPath?

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.

How do I traverse back to parent in XPath?

IN the XPath we can traverse backward using the double dots(..). We can traverse backward as many levels as we want like ../../… For example, if we want to find the parent of any child div selector then we can use it as.

What is current node in XPath?

Current node is the node that the XPath processor is looking at when it begins evaluation of a query. In other words, the current node is the first context node that the XPath processor uses when it starts to execute the query. During evaluation of a query, the current node does not change.

What is difference between ancestor and parent in XPath?

The difference between parent:: and ancestor:: axis is conveyed by their names: A parent is the immediately direct ancestor. So, yes /a/b/c/d/ancestor::*[1] would be the same as /a/b/c/d/parent::* .


1 Answers

The problem is in /[. You can change it to

../../self::*[@status='current']
like image 131
choroba Avatar answered Oct 03 '22 20:10

choroba