0
votes

I'm trying to select the title value where the attribute value is greater than 1:

path="//book/price[@value >1]

I'm not exactly sure how to select only the titles where the attribute value for the element price is greater than one. I'm an XML beginner and I'm doing a few exercises from a tutorial.

My priorities are

  1. learning XML
  2. super XML

XML:

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price value="1">49.99</price>
</book>
<book>
 <title lang="en">Learning XML</title>
 <price value="2">29.95</price>
</book>
<book>
  <title lang="en">super xml</title>
  <price value="3">39.95</price>
</book>
</bookstore>

Update

The first query was perfect could you show me how to obtain it from a nested element?

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <possibleprice>
     <price value="1">49.99</price>
  </possibleprice>
</book>
<book>
  <title lang="en">Learning XML</title>
  <possibleprice>
     <price value="2">29.95</price>
  </possibleprice>
</book>
<book>
  <title lang="en">super xml</title>
  <possibleprice>
    <price value="3">39.95</price>
  </possibleprice>
</book>
</bookstore>
1
Your edit changed the question substantively and is a bit unfair to matthias_h, who answered the original question correctly. (It also introduced non-well-formed XML into the mix, which will have to be fixed before being able to be used with XPath.) You should reverted your edit, accept matthias_h's answer, and ask a new question instead. Thanks.kjhughes
I understand will do, didn't know I couldn't do that apologiesPaul Brunache
@kjhughes Thanks, I've added the first version of the OP to the answer so it's valid. ArkhamRejek: I don't mind updating OP, but when updating, you could just do as follows: keep the OP, and below the original, write "Update" or something on top of further questions so it's not too irritating / requiring to read all answers and comments to understand. I'll update my answer to cover the second question. But kjhughes is correct in giving the advice to ask a new question. If someone else would answer the 2nd question, it would be a bit unfair because it's not possible for you to accept both.matthias_h
I added the update, I'm sorry about that still learning how everything works. I've updated the question do you mind helping me out or should I post another question since I already gave you the best answer?Paul Brunache
I don't mind and updated my answer, don't worry. Not necessary to post another question for this.matthias_h

1 Answers

3
votes

For example:

For the first version of the OP:

Input:

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price value="1">49.99</price>
</book>
<book>
 <title lang="en">Learning XML</title>
 <price value="2">29.95</price>
</book>
<book>
  <title lang="en">super xml</title>
  <price value="3">39.95</price>
</book>
</bookstore>

XPath:

//book/price[@value > 1]/preceding-sibling::title

Result:

<title lang="en">Learning XML</title>
<title lang="en">super xml</title>

For the updated second XML version of OP, as different example:

XPath:

//book/possibleprice/price[@value > 1]/ancestor::book/title

Same result as above.

To answer the question in the comment if it would be necessary to use preceding-sibling twice for the 2nd version: no. preceding-sibling targets the preceding-sibling of the current node. The current node in this case is price and has no siblings, but the parent possibleprice. To get the same result in a different way as given above, it would be possible to get the parent of price and then the preceding-sibling of possibleprice:

//book/possibleprice/price[@value > 1]/parent::possibleprice/preceding-sibling::title

which results in the same.

As mentioned that OP is a beginner in XML, maybe the following can be useful to illustrate XPath axes: http://www.xmlplease.com/axis