【问题标题】:XPATH find a node if no other node has a sertin value如果没有其他节点具有特定值,则 XPATH 查找节点
【发布时间】:2013-04-27 09:26:39
【问题描述】:

只有在没有价格为 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 中

【问题讨论】:

  • 请确保发布有效 XML,尤其是在发布 DTD 时...

标签: xml xpath xml-parsing dtd


【解决方案1】:

当没有价格为 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]

【讨论】:

    【解决方案2】:

    这似乎是我认为最简单的方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      相关资源
      最近更新 更多