How to Use multiple conditions in Xpath?

18,448

Solution 1

I got an answer.

//book[@style='novel' and ./author/award/text()='Pulitzer']//first-name

Solution 2

Use:

/*/book[@style='novel']/author[award = 'Pulitzer']/first-name

This selects any first-name element whose author parent has a award child with string value of 'Pulitzer' and whose (of the author) parent is a book whose style attribute has value "novel" and whose parent is the top element of the XML document.

Solution 3

A similar question in the same context. How can I do the vice-versa ? Let's suppose I want to find the id of all those books whose price is greater than 20 ? I know I am being a nudge, but really want to clear my understanding.

Here is the needed XPATH :

//book/price[text() > 20]/..
Share:
18,448
Akshay
Author by

Akshay

Updated on June 13, 2022

Comments

  • Akshay
    Akshay about 2 years

    New to Xpath. Was trying in to use XML task in SSIS to load some values. Using Microsoft' XML inventory mentioned below.

    How can I load first-name value in bookstore/books where style is novel and award = 'Pulitzer'? //book[@style='novel' and ./author/award/text()='Pulitzer'] is what I am trying. It gives the whole element. Where should I modify to just get the first-name value?

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="myfile.xsl" ?>
    <bookstore specialty="novel">
      <book style="autobiography">
        <author>
          <first-name>Joe</first-name>
          <last-name>Bob</last-name>
          <award>Trenton Literary Review Honorable Mention</award>
        </author>
        <price>12</price>
      </book>
      <book style="textbook">
        <author>
          <first-name>Mary</first-name>
          <last-name>Bob</last-name>
          <publication>Selected Short Stories of
            <first-name>Mary</first-name>
            <last-name>Bob</last-name>
          </publication>
        </author>
        <editor>
          <first-name>Britney</first-name>
          <last-name>Bob</last-name>
        </editor>
        <price>55</price>
      </book>
      <magazine style="glossy" frequency="monthly">
        <price>2.50</price>
        <subscription price="24" per="year"/>
      </magazine>
      <book style="novel" id="myfave">
        <author>
          <first-name>Toni</first-name>
          <last-name>Bob</last-name>
          <degree from="Trenton U">B.A.</degree>
          <degree from="Harvard">Ph.D.</degree>
          <award>P</award>
          <publication>Still in Trenton</publication>
          <publication>Trenton Forever</publication>
        </author>
        <price intl="Canada" exchange="0.7">6.50</price>
        <excerpt>
          <p>It was a dark and stormy night.</p>
          <p>But then all nights in Trenton seem dark and
          stormy to someone who has gone through what
          <emph>I</emph> have.</p>
          <definition-list>
            <term>Trenton</term>
            <definition>misery</definition>
          </definition-list>
        </excerpt>
      </book>
      <my:book xmlns:my="uri:mynamespace" style="leather" price="29.50">
        <my:title>Who's Who in Trenton</my:title>
        <my:author>Robert Bob</my:author>
      </my:book>
    </bookstore>
    
  • Jens Erat
    Jens Erat about 11 years
    This is totally fine, there isn't anything to enhance.
  • Martin Honnen
    Martin Honnen about 11 years
    I would not use text() unless I am dealing with mixed content (e.g. an XHTML p element containing <p>The <b>quick</b> fox jumped over the lazy dog.</p>) and I explicitly need to select a text child or descendant (e.g. /p/text()[1] to select the text node with The ). Otherwise I would simply compare the element. So in your case //book[@style = 'novel' and author/award = 'Pulitzer']//first-name suffices, you don't gain anything with using author/award/text().
  • Akshay
    Akshay about 11 years
    Thank you Jens and Martin for sharing your views.
  • Arup Rakshit
    Arup Rakshit about 11 years
    Here I don't get any such and operator, any reference where you got this?