How to Use multiple conditions in Xpath?
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]/..
Akshay
Updated on June 13, 2022Comments
-
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 about 11 yearsThis is totally fine, there isn't anything to enhance.
-
Martin Honnen about 11 yearsI would not use
text()
unless I am dealing with mixed content (e.g. an XHTMLp
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 withThe
). 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 usingauthor/award/text()
. -
Akshay about 11 yearsThank you Jens and Martin for sharing your views.
-
Arup Rakshit about 11 years
Here
I don't get any suchand
operator, any reference where you got this?