XPath expression to validate attribute values โ€‹โ€‹for multiple child nodes

Given the XML below, I am looking for an XPath query to answer the following question:

Does the list of languages โ€‹โ€‹above contain all of these languages: "English", "French" and "Norwegian"?

In other words, is there a way to find out if the "Objects" node contains a given set of languages? The answer is TRUE for "English", "French" and "Norwegian", but FALSE for "Arabic", "English", "French" and "Norwegian".

<Document>
  <Entities>
    <Entity Name="Afrikaans" Id="2000" Type="Language"/>
    <Entity Name="Dansk" Id="2002" Type="Language"/>
    <Entity Name="Deutsch" Id="2003" Type="Language"/>
    <Entity Name="English" Id="2005" Type="Language"/>
    <Entity Name="Espaรฑol" Id="2006" Type="Language"/>
    <Entity Name="French" Id="2007" Type="Language"/>
    <Entity Name="Indonesian" Id="2010" Type="Language"/>
    <Entity Name="Italiano" Id="2012" Type="Language"/>
    <Entity Name="Norwegian" Id="2018" Type="Language"/>
  </Entities>
</Document>

      

+3


source to share


3 answers


If you are using XPath 2.0, the function exists()

returns true

or false

...



exists(/*/Entities[*/@Name='English' and */@Name='French' and */@Name='Norwegian'])

      

+2


source


This XPath

/Document/Entities/Entity[@Name='Norwegian'] and /Document/Entities/Entity[@Name='French'] and /Document/Entities/Entity[@Name='English']

      



will be true

if all three languages โ€‹โ€‹exist and false

otherwise

0


source


In XPath 1.0 use :

  /*/*/Entity/@Name = 'English'
and
  /*/*/Entity/@Name = 'French'
and
  /*/*/Entity/@Name = 'Norwegian'

      

In XPath 2.0, use :

 every $lang in ('English', 'French', 'Norwegian')
   satisfies
       $lang = /*/*/Entity/@Name

      

0


source







All Articles