Xpath filters elements with a specific element

I'm not sure about the terminology, so I'm having trouble finding leads.
Basically, I have something like

<book>
   <genre>sci-fi</genre>
   <genre>happy</genre>
</book>
<book>
   <genre>romance</genre>
</book>

      

And I want to request a list of books that don't have sci-fi as one of the genres. I tried something simple like

//book[//book/genre != "sci-fi"]

      

With the idea that it will return all books that do not have a genre element with the value "sci-fi", but it will still return the first one because one of its genres is not "scientific",

Also, how would I formulate this question so that people understand what I'm talking about?

+3


source to share


1 answer


The square bracket term is a predicate. You are looking for book elements that do not have a child called genre with the text "sci-fi". This will fit all books in at least one genre that is not sci-fi.

//book[genre != "sci-fi"]

      

The following will do what you want:



//book[not(genre = 'sci-fi')]

      

And note that you can add additional paths to the end of this, so if your book had an item title

, you could get all the titles with:

//book[not(genre = 'sci-fi')]/title

      

+7


source







All Articles