Jack cole
Jack cole

Reputation: 447

XPath queries - boolean conditions

I have this XML file , and I want to find these queries :

  1. All the books that contain a picture
  2. All the books with more than one picture
  3. All the authors who wrote a book with "Larry Niven"

Here is XML :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE inventory SYSTEM "books.dtd">
<inventory>
    <book num="b1">
        <title>Snow Crash</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <price>14.95</price>
        <chapter>
            <title>Snow Crash - Chapter A</title>
            <paragraph>
                This is the <emph>first</emph> paragraph.
                <image file="firstParagraphImage.gif"/>
                afetr image...
            </paragraph>
            <paragraph>
                This is the <emph>second</emph> paragraph.
                <image file="secondParagraphImage.gif"/>
                afetr image...
            </paragraph>
        </chapter>
        <chapter>
            <title>Snow Crash - Chapter B</title>
            <section>
                <title>Chapter B - section 1</title>
                <paragraph>
                    This is the <emph>first</emph> paragraph of section 1 in chapter B.
                    <image file="Chapter_B_firstParagraphImage.gif"/>
                    afetr image...
                </paragraph>
                <paragraph>
                    This is the <emph>second</emph> paragraph of section 1 in chapter B.
                    <image file="Chapter_B_secondParagraphImage.gif"/>
                    afetr image...
                </paragraph>
            </section>
        </chapter>
        <chapter>
            <title>Chapter C</title>
            <paragraph>
                This chapter has no images and only one paragraph
            </paragraph>
        </chapter>
    </book>
    <book num="b2">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <price>5.99</price>
        <chapter>
            <title>Burning Tower - Chapter A</title>
        </chapter>
        <chapter>
            <title>Burning Tower - Chapter B</title>
            <paragraph>
                This is the <emph>second</emph> paragraph of chapter B in the 2nd book.
                <image file="Burning_Tower_Chapter_B_secondParagraphImage.gif"/>
                afetr image...
            </paragraph>
        </chapter>
    </book>
    <book num="b3">
        <title>Zodiac</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <price>7.50</price>
        <chapter>
            <title>Zodiac - Chapter A</title>
        </chapter>
    </book>
    <!-- more books... -->
</inventory>

And here are my answers :

1. /inventory/book[count(image)=1]/title
2. /inventory/book[count(image)>=1/title
3. /inventory/book[author=”Larry Niven”][count(author)>1]/author

But when I test these answers with Java XPath code , It doesn't work .

Where are the mistakes ? thanks

Upvotes: 0

Views: 523

Answers (1)

yamen
yamen

Reputation: 15618

Your first two have issues relating to where image is in the document - need to search from current node and search all children (not just direct children):

/inventory/book[count(.//image) = 1]/title
/inventory/book[count(.//image) >= 1]/title

Your last one has a problem with quotes (need to use single quotes), and also duplicates Larry Niven. Here is the fix:

/inventory/book[author='Larry Niven'][count(author) > 1]/author[. != 'Larry Niven']

Note you can use and instead:

/inventory/book[author='Larry Niven' and count(author) > 1]/author[. != 'Larry Niven']

Upvotes: 2

Related Questions