I want to get the "Package" nodes which have a "System" grandchild with an "mtm" attribute, and the value of the "mtm" attribute is "2055". For the below example document, only the first Package node should be returned.
I use
"/Database/Package[/SystemCompatibility/System[@mtm='2055']]"
but it does not work. What is wrong with this expression?
<?xml version="1.0" encoding="UTF-8"?>
<Database version="300">
<Package id="6imb05ww" description="ThinkPad Modem Adapter">
<SystemCompatibility>
<System mtm="8742" os="Windows XP" oslang="en" />
<System mtm="2055" os="Windows XP" oslang="jp" />
</SystemCompatibility>
</Package>
<Package id="6imb06ww" description="ThinkPad Modem Adapter">
<SystemCompatibility>
<System mtm="3046" os="Windows XP" oslang="en" />
</SystemCompatibility>
</Package>
</Database>
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.
A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.
Definition of XPath attribute. For finding an XPath node in an XML document, use the XPath Attribute expression location path. We can use XPath to generate attribute expressions to locate nodes in an XML document.
Remove the /
before SystemCompatibility
/Database/Package[SystemCompatibility/System[@mtm='2055']]
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