2

我有以下 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>

如何编写以下 XmlPath 1.0:

1)如果所有书的价格不等于14.95,则退回所有书?

2)退还所有价格高于其后书籍价格的书籍。

提前致谢 。

4

2 回答 2

3

试试这些:

1)如果所有书的价格不等于14.95,则退回所有书?

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

2) 将其价格大于其后书籍价格的书籍全部归还。

 /inventory/book[price>following-sibling::*[1]/price]/title
于 2012-05-21T16:43:01.603 回答
2

如何编写以下 XmlPath 1.0:

1)如果所有书的价格不等于14.95,则退回所有书?

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

“/inventory/book”为您提供“inventory”节点的所有名为“books”的子元素。而“price/text() != 14.95”只是意味着对于每本书,检查一个名为“price”的子元素是否不同于 14.95。为了方便您的应用程序,您可以使用“//book”代替“/inventory/book”。"//book" 请求当前节点中所有名为 "book" 的后代元素(在本例中为根元素)

2)退还所有价格高于其后书籍价格的书籍。

/inventory/book[price >  following-sibling::book[position() = 1]/price]
于 2012-05-21T17:10:29.047 回答