-2

只有当没有价格为 14.95 的书时,我才需要退回所有书......

DTD

<!ELEMENT inventory (book)+>
<!ELEMENT book (title, author+, publisher+, price, chapter*)>
<!ATTLIST book num ID #REQUIRED>
<!ELEMENT chapter (title, (paragraph* | section*))>
<!ELEMENT section (title?, paragraph*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price currency CDATA #FIXED "usd">
<!ELEMENT paragraph (#PCDATA | emph | image)*>
<!ELEMENT emph (#PCDATA)>
<!ELEMENT image EMPTY >
<!ATTLIST image
          file
          CDATA #REQUIRED
          height CDATA #IMPLIED
          width CDATA #IMPLIED >

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="firstParaImage.gif"/>
            After image...
         </paragraph>
         <paragraph>
            This is the
            <emph>second</emph>
            paragraph.
            <image file="secondParaImage.gif"/>
            After image...
         </paragraph>
      </section>
   </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>
</inventory>

我知道如何找到所有价格与 14.95 不同的书。但我现在不知道如何在 XPTH 中执行 if 语句,分配仅在 XPTH 中

4

2 回答 2

1

当没有 price = 14.96 的书籍时,这将返回所有书籍:

(//book)[not(//book[price = 14.95])]

这将返回所有价格 = 14.95 的书籍,如果没有则返回所有书籍:

//book[price = 14.95 or not(//book[price = 14.95])]

如果您的 XPath 引擎优化不佳,这可能会更快:

(//book)[not(//book[price = 14.95])] | //book[price = 14.95]
于 2013-04-27T09:54:30.683 回答
0

在我看来,这似乎是最简单的方法:

/*[not(book/price='14.95')]/book
于 2013-04-28T01:35:44.157 回答