URL87
URL87

Reputation: 11022

Xml path with condition

i have the follow 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>

how to write the follow XmlPath 1.0:

1) return all the books provided all the book's prices are not equal 14.95 ?

2) return all the books that thier price is bigger then the price of the book after them .

thank s in advance .

Upvotes: 2

Views: 499

Answers (2)

arthur
arthur

Reputation: 3325

how to write the follow XmlPath 1.0:

1) return all the books provided all the book's prices are not equal 14.95 ?

/inventory/book[price/text() != 14.95] 

"/inventory/book" gives you all the children elements named "books" of the "inventory" Node. and "price/text() != 14.95" simply means for each book, check whether one their child element named "price" is different than 14.95. instead of "/inventory/book", you can rather use "//book" for the convenience of your application. "//book" ask for all descendant elements named "book" from your current node (in this case the root element)

2) return all the books that thier price is bigger then the price of the book after them .

/inventory/book[price >  following-sibling::book[position() = 1]/price]

Upvotes: 2

dirkgently
dirkgently

Reputation: 111278

Try these:

1) return all the books provided all the book's prices are not equal 14.95 ?

 /inventory/book[price != 14.95]/title

2) return all the books that their price is bigger than the price of the book after them.

 /inventory/book[price>following-sibling::*[1]/price]/title

Upvotes: 3

Related Questions