Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve parent node using XQuery?

Tags:

I am using XML and XQuerie. I usually use an XPath expression relative to a parent node to retrieve its child node. But, I am not sure how to do the opposite meaning if I have a child node, how do I retrieve its parent node.

<node id="50>   <childnode1 childid="51" />   <childnode2 childid="52" /> </node> 

If I have the node <childnode1 childid="51" />, how do I retrieve its parent: <node id="50>

like image 599
sony Avatar asked Sep 14 '10 20:09

sony


People also ask

How can I get parent node in 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.


1 Answers

Short answer:

.. 

This selects the parent of the current (context) node.

Longer and more general answers:

//node()[childnode1/@childid="51"] 

This selects any node in the document that has a child element named childnode1, that has an attibute childid, whose value is '51'.

One should try to avoid an expression that contains the // abbreviation, because this may be very inefficient. Use '//' only when the structure of the XML document isn't known in advance.

Best answer:

ExpressionSelectingTheChildNode/.. 
like image 136
Dimitre Novatchev Avatar answered Sep 19 '22 19:09

Dimitre Novatchev