haggis78
haggis78

Reputation: 73

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

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.

Upvotes: 3

Views: 535

Answers (1)

JLRishe
JLRishe

Reputation: 101700

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

Upvotes: 3

Related Questions