Home > OS >  XPath 1.0: Find all elements that are mentioned inside a different element
XPath 1.0: Find all elements that are mentioned inside a different element

Time:01-28

For example, I have this:

<setting>
  <name>text1</name>
</setting>
<setting>
  <name>text2</name>
</setting>
<setting>
  <name>text3</name>
</setting>

<thing>
  <name>name</name>
  <settings>
    <li>text1</li>
    <li>text3</li>
  </settings>
</thing>

I need to select all "setting" nodes that are mentioned inside "thing/settings/li". In the example the xpath should return setting[1] and setting[3].

CodePudding user response:

You can not do this directly with XPath 1.0.
What you can do with XPath 1.0 and some additional code is to get the list of values inside thing/settings/li with

//thing/settings/li

And then to find all the setting nodes with those values with something like

//setting/name[text()='value']

Where value will be a parameter passed there form the previously found list.
How to pass the parameter depends on language you are using etc.

CodePudding user response:

Wrap your XML in a single root element so that it is well-formed. Then...

This XPath,

//setting[name=//thing/settings/li]

will select all settings whose name is among those listed in //thing/settings/li:

<setting>
  <name>text1</name>
</setting>
<setting>
  <name>text3</name>
</setting>

as requested.

  •  Tags:  
  • Related