如果没有其他节点具有特定值,XPATH将查找节点

时间:2013-04-27 09:26:40

标签: xml xpath xml-parsing dtd

我需要退还所有的书,只要这本书没有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中

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

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

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