Can XPath return only nodes that have a child of X?

35,181

Solution 1

Here it is, in all its glory

/pets/*[bar]

English: Give me all children of pets that have a child bar

Solution 2

Just in case you wanted to be more specific about the children - you can also use selectors on them.

Example:

<pets>
    <cat>
        <foo>don't care about this</foo>
    </cat>
    <dog>
        <foo>not this one either</foo>
    </dog>
    <lizard>
        <bar att="baz">lizard should be returned, because it has a child of bar</bar>
    </lizard>
    <pig>
        <bar>don't return pig - it has no att=bar </bar>
    </pig>
</pets>

Now, you only care about all pets having any child bar that has an attribute att with value baz. You can use the following xpath expression:

//pets/*[descendant::bar[@att='baz']]

Result

<lizard>
    <bar att="baz">lizard should be returned, because it has a child of bar</bar>
</lizard>
Share:
35,181
hahv
Author by

hahv

Updated on December 17, 2020

Comments

  • hahv
    hahv over 3 years

    Is it possible to use XPath to select only the nodes that have a particular child elements? For example, from this XML I only want the elements in pets that have a child of 'bar'. So the resulting dataset would contain the lizard and pig elements from this example:

    <pets>
      <cat>
        <foo>don't care about this</foo>
      </cat>
      <dog>
       <foo>not this one either</foo>
      </dog>
      <lizard>
       <bar>lizard should be returned, because it has a child of bar</bar>
      </lizard>
      <pig>
       <bar>return pig, too</bar>
      </pig>
    </pets>
    

    This Xpath gives me all pets: "/pets/*", but I only want the pets that have a child node of name 'bar'.

  • jeremfg
    jeremfg over 6 years
    How about wanting all pets who's <bar> element has specific text()? Seems like I can't figure that one out...