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