3

Lets say I have the following XML (heavily simplified sample of my actual XML):

<myxml>
    <cities>
        <city>Amsterdam</city>  
        <city>London</city>
        <city>Paris</city>
    </cities>
    <hotelLocations>
        <hotelLocation>Amsterdam</hotelLocation>
        <hotelLocation>Berlin</hotelLocation>
    </hotelLocations>   
</myxml>

Now I would like to know if the values in the hotelLocation do actually exist as cities. I am trying to do that in just one XPath statement:

//hotelLocation=//city

However, this will give "true" back if one of the hotelLocations matches, instead I only want it to give true if all of the hotelLocations exist in the cities entities.

Any idea if this is possible with one XPath statement at all?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
ErikL
  • 2,031
  • 6
  • 34
  • 57

1 Answers1

4

instead I only want it to give true as all of the hotellocations exist in the cities entities. Any idea if this is possible with one xpath statement at all?

Yes, the following XPath expression will do what you request:

not(//hotelLocation[not(. = //city)])

Read like this: There should be no hotelLocation elements whose string value does not exist among the city elements in this document. This is logically equivalent making the requested statement: All cities given by hotelLocation elements must also exist in city elements in this document.

kjhughes
  • 106,133
  • 27
  • 181
  • 240