XPath: Get parent node from child node

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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! 🎉✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my