1
votes

I'm looking at the w3c bookstore xpath example here.

Based on that xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <bookstore>

    <book category="COOKING">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
    </book>

    <book category="CHILDREN">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>

    <book category="WEB">
      <title lang="en">XQuery Kick Start</title>
      <author>James McGovern</author>
      <author>Per Bothner</author>
      <author>Kurt Cagle</author>
      <author>James Linn</author>
      <author>Vaidyanathan Nagarajan</author>
      <year>2003</year>
      <price>49.99</price>
    </book>

    <book category="WEB">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
    </book>

</bookstore> 

I am trying to do this with XPath:

"Does this XML docuement have a book that has an author 'James McGovern' and a price of '49.99' AND does this XML document also have a book that has an author 'Erik T. Ray' and a price of '39.95'

That human statement is true, what is the XPath equivalent? Is it possible to run expressions across sibling nodes, and if so, how?

To clarify, in other languages, one might do something like this:

(author1 = 'James McGovern' and price1 = '49.99') and (author2 = 'Erik T. Ray' and price2 = '39.95')

Where author1\price1 are children of: <book category="WEB"> with title: <title lang="en">XQuery Kick Start</title>

And author2\price2 are children of: <book category="WEB"> with title: <title lang="en">Learning XML</title>

2
the ] and [ makes no sense for me. What are you expecting from that?hek2mgl
I know, this is a hard problem to describe. I've posted an edit to clarify...hopefully.javamonkey79
I'm sure you mean or in this casehek2mgl
No, I mean and but of the cousin book node.javamonkey79
(author = 'James McGovern' and price = '49.99') and (author = 'Erik T. Ray' and price = '39.95') is logically impossible. How should the price for example being 49.99 and 39.95 at the same time?hek2mgl

2 Answers

1
votes

Update:

After your last edit, I finally got what you are trying exactly :). If you want to make sure that both nodes are present, you can use the following query:

boolean(/bookstore[book[price = "39.95" and author = "Erik T. Ray"] and book[price = "49.99" and author = "James McGovern"]])
1
votes

Performing comparison for each book is all that is required so the updated XPath is:

boolean(//book[author='James McGovern' and price='49.99']) and boolean(//book[author='Erik T. Ray' and price='39.95'])

or if there is adocument with multiple bookstores and you want to find all bookstores with both books you can do:

/bookstore[book[author='James McGovern' and price='49.99'] and book[author='Erik T. Ray' and price='39.95']]