Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath: Select parent nodes which have a subnode with an attribute

Tags:

xpath

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>
like image 757
kennyzx Avatar asked Nov 02 '11 16:11

kennyzx


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 you find the XPath of a parent element?

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.

What is attribute in XPath?

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.


1 Answers

Remove the / before SystemCompatibility

/Database/Package[SystemCompatibility/System[@mtm='2055']]
like image 171
John Giotta Avatar answered Oct 23 '22 13:10

John Giotta