Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use multiple conditions in Xpath?

Tags:

xpath

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>
like image 614
Akshay Avatar asked Apr 20 '13 11:04

Akshay


People also ask

How do I combine two conditions in XPath?

In XPath expression single attribute will identify the multiple conditions then we can use more than one attribute in the expression of the path for locating a single element. For writing multiple conditions we can apply the AND and OR conditions.

Can we use and condition in XPath?

Using OR & AND In the below XPath expression, it identifies the elements whose single or both conditions are true. Highlight both elements as 'First Name' element having attribute 'id' and 'Last Name' element having attribute 'name'. In AND expression, two conditions are used.

What is dynamic XPath in Selenium?

What Is Dynamic XPath In Selenium? XPath, also known as XML Path, is one of Selenium WebDriver's most commonly used locators for navigating through a page's HTML structure. It can be used to locate any element in a web page using HTML DOM structure in HTML and XML documents.


2 Answers

I got an answer.

//book[@style='novel' and ./author/award/text()='Pulitzer']//first-name
like image 134
Akshay Avatar answered Jan 02 '23 20:01

Akshay


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.

like image 44
Dimitre Novatchev Avatar answered Jan 02 '23 19:01

Dimitre Novatchev