XPath: Get parent node from child node
XPath: Get parent node from child node 💡🔎
So, you're working with XML data and you want to get the parent node of a specific child node using XPath? 🤔 Don't worry, we've got you covered! In this blog post, we'll address this common issue and provide you with some easy solutions. Let's dive in! 🏊♀️🏊♂️
The Problem 🧩
Let's take a look at the context. You have the following XML data structure:
<?xml version="1.0" encoding="utf-8"?>
<d:data xmlns:d="defiant-namespace" d:mi="23">
<store d:mi="22">
<book price="12.99" d:price="Number" d:mi="4">
<title d:constr="String" d:mi="1">Sword of Honour</title>
<category d:constr="String" d:mi="2">fiction</category>
<author d:constr="String" d:mi="3">Evelyn Waugh</author>
</book>
<book price="8.99" d:price="Number" d:mi="9">
<title d:constr="String" d:mi="5">Moby Dick</title>
<category d:constr="String" d:mi="6">fiction</category>
<author d:constr="String" d:mi="7">Herman Melville</author>
<isbn d:constr="String" d:mi="8">0-553-21311-3</isbn>
</book>
<book price="8.95" d:price="Number" d:mi="13">
<title d:constr="String" d:mi="10">50</title>
<category d:constr="String" d:mi="11">reference</category>
<author d:constr="String" d:mi="12">Nigel Rees</author>
</book>
<book price="22.99" d:price="Number" d:mi="18">
<title d:constr="String" d:mi="14">The Lord of the Rings</title>
<category d:constr="String" d:mi="15">fiction</category>
<author d:constr="String" d:mi="16">J. R. R. Tolkien</author>
<isbn d:constr="String" d:mi="17">0-395-19395-8</isbn>
</book>
<bicycle price="19.95" d:price="Number" d:mi="21">
<brand d:constr="String" d:mi="19">Cannondale</brand>
<color d:constr="String" d:mi="20">red</color>
</bicycle>
</store>
</d:data>
You need to retrieve the parent node of the child node with the value "50" inside the <title>
tag. In this example, the parent node is the <book>
node that contains the desired child node.
The Solution 🛠️
To achieve this using XPath, you can leverage the parent::
axis. Here's the XPath query you can use:
//*[title="50"]/parent::*
In this query, //*[title="50"]
selects the node with the child node that has a value of "50" inside the <title>
tag. The parent::*
part retrieves the parent node of the selected node.
So, in the given XML data structure, this XPath query will return the <book>
node that contains the child node with the value "50" inside the <title>
tag, which is the desired parent node.
Putting It All Together ⚙️
Let's combine the XPath query and the XML data structure to see the result:
//*[title="50"]/parent::*
Result:
<book price="8.95" d:price="Number" d:mi="13">
<title d:constr="String" d:mi="10">50</title>
<category d:constr="String" d:mi="11">reference</category>
<author d:constr="String" d:mi="12">Nigel Rees</author>
</book>
As you can see, the result is the <book>
node, which is the parent of the child node with the value "50" inside the <title>
tag.
Conclusion and Action Steps 🎉🚀
Congratulations! Now you know how to retrieve the parent node from a child node using XPath. This can be handy when working with complex XML data structures. Remember to use the parent::
axis in your XPath query to retrieve the desired parent node.
Try it out with your own XML data and see the magic happen! If you have any further questions or want to learn more about XPath or XML, leave a comment below. We'd love to hear from you! 😊
Now it's your turn! Share your experience in the comments and let us know how this solution worked for you. Don't forget to share this post with your friends and colleagues who might find it helpful. Happy XPathing! 🎉✨