Joanmacat
Joanmacat

Reputation: 1551

Xpath select two different nodes without certain node

Hello Stackoverflow community!

I have the following XML structure:

  <fruit>
    <id>1</id>
    <name>Apple</name>
    <species>Tree</species>
    <features>
      <from>Reineta</from>
      <into>Golden</into>
    </features>
  </fruit>

  <fruit>
    <id>2</id>
    <name>Orange</name>
    <species>Tree</species>
    <features>
      <from>Citric</from>
      <into>Mediterranean</into>
    </features>
  </fruit>

  <fruit>
    <id>3</id>
    <name>Peach</name>
    <species>Tree</species>
    <features>
      <from>Golden</from>
    </features>
  </fruit>

How can I get the <name> and <features> of those fruit nodes whose doesn't contain the features/into nodes?

I tried this:

//fruit/features[from][not(into)]

I only can get the <features>, but how can I get the <name> too, in the same query?

Thank you very much.

Upvotes: 0

Views: 29

Answers (1)

har07
har07

Reputation: 89305

If this is XPath 1.0, then your best bet is to use union operator (|) to combine result from XPath that return features and another XPath that return name :

//fruit/features[from and not(into)] | //fruit[features[from and not(into)]]/name

In higher version of XPath, you can use FLWOR expression to do the following (assuming fruit element can contains only one features element at a time) :

for $fruit in //fruit[features[from and not(into)]]
return ($fruit/name,$fruit/features)

demo

Upvotes: 2

Related Questions