Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Xpath to locate parent node where a child node does not contain an attribute?

Tags:

xml

xpath

Using Xpath, how can I locate a parent element where none of multiple child elements contains a specific attribute value among a list of attribute values?

Here's a sample of my xml:

<app>
  <rdg wit="#R #W #I #S #C #O #D">existunt,</rdg>
  <rdg wit="#J">existant,</rdg>
</app>

My XML has hundreds of these elements that should all have the same set of eight attribute values (#R, etc.) distributed variously among two or more elements. But a few of the are missing an attribute value on the list, and I need to locate those nodes.

So I'm trying to find, say, all the <app> elements where none of the child <rdg> elements contains #R.

I can get the ones that do contain #R with //app/rdg[contains(@wit,"#R")] and I know there's a not() function, but I haven't figured out how to get these to work together.

like image 308
haggis78 Avatar asked Jan 24 '26 12:01

haggis78


1 Answers

You can accomplish this as follows:

//app[not(rdg/@wit[contains(., '#R')])]

Alternatively:

//app[not(rdg[contains(@wit, '#R')])]

This slightly convoluted approach is necessary because the apps contain multiple rdgs, so we're basically doing (to take the second example):

  • Select all apps where...
  • There is not a rdg child where...
  • The wit attribute contains #R
like image 177
JLRishe Avatar answered Jan 27 '26 06:01

JLRishe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!