-2

i need to return all of the book only if ther is no book with the price of 14.95....

the 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 for example the xml is not constraint to be this one

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>

i konw how to find all of the book with price difrent from 14.95.. but i dont now how to do the if statment in XPTH the assigment is in XPTH only

mosheovadi1
  • 73
  • 12

2 Answers2

1

This returns all books when there is none with price = 14.96:

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

This returns all books with price = 14.95 or all if there is none:

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

If your XPath engine is optimizing badly, this could be faster:

(//book)[not(//book[price = 14.95])] | //book[price = 14.95]
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
0

This seems to be the most simple way in my opinion:

/*[not(book/price='14.95')]/book
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95