XPath для когда такого элемента не существует? - PullRequest
1 голос
/ 19 апреля 2020

У меня есть эта задача с XML о книгах. Меня попросили вернуть все предоставленные книги при условии, что ни одна книга не оценена по цене 7,5

XML

<?xml version="1.0" encoding="UTF-8"?>
  <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>
      </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_firstParaImage.gif"/>
            After image...
          </paragraph>
          <paragraph>
            This is the
            <emph>second</emph>
            paragraph of section 1 in chapter B.
            <image file="Chapter_B_secondParagraphImage.gif"/>
            After 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_secondParaImage.gif"/>
          After 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>
  </inventory>

Поскольку у меня есть книга по цене 7.5, я не должен получать книги.

Когда я попробовал этот запрос, я получил все книги, которые не оценены 7.5, но это не то, что я должен получить , Чего мне не хватает?

//book[price!=7.50]/title/text()

<?xml version="1.0" encoding="UTF-8"?>
<result>
Snow Crash
Burning Tower
</result>

1 Ответ

1 голос
/ 19 апреля 2020

Этот XPath,

/inventory[not(book/price=7.50)]/book/title/text()

вернет title текст всех book с при условии, что book в inventory не имеет price 7.50.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...