Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XPath, How do I select a node based on its text content and value of an attribute?

Tags:

xml

xpath

xquery

Given this XML:

<DocText> <WithQuads>     <Page pageNumber="3">         <Word>             July             <Quad>                 <P1 X="84" Y="711.25" />                 <P2 X="102.062" Y="711.25" />                 <P3 X="102.062" Y="723.658" />                 <P4 X="84.0" Y="723.658" />             </Quad>         </Word>         <Word>         </Word>         <Word>             30,             <Quad>                 <P1 X="104.812" Y="711.25" />                 <P2 X="118.562" Y="711.25" />                 <P3 X="118.562" Y="723.658" />                 <P4 X="104.812" Y="723.658" />             </Quad>         </Word>     </Page> </WithQuads> 

I'd like to find the nodes that have text of 'July' and a Quad/P1/X attribute Greater than 90. Thus, in this case, it should not return any matches. However, if I use GT (>) or LT (<), I get a match on the first Word element. If I use eq (=), I get no match.

So:

//Word[text()='July' and //P1[@X < 90]] 

will return true, as will

//Word[text()='July' and //P1[@X > 90]] 

How do I constrain this properly on the P1@X attribute?

In addition, imagine I have multiple Page elements, for different page numbers. How would I additionally constrain the above search to find Nodes with text()='July', P1@X < 90, and Page@pageNumber=3?

like image 366
marc esher Avatar asked Dec 30 '09 21:12

marc esher


People also ask

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.

How do I select text in XPath?

Do note that /html/text() doesn't select all text nodes in the document -- only the text nodes that are children (not descendents) of the top, html element. You probably want /html//text() . Some knowledge and understanding of XPath is typically required in order to construct XPath expressions.

Can we use text () in XPath?

5) XPath Text() FunctionThe XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.


1 Answers

Generally I would consider the use of an unprefixed // as a bad smell in an XPath.

Try this:-

/DocText/WithQuads/Page/Word[text()='July' and Quad/P1/@X > 90] 

Your problem is that you use the //P1[@X < 90] which starts back at the beginning of the document and starts hunting any P1 hence it will always be true. Similarly //P1[@X > 90] is always true.

like image 122
AnthonyWJones Avatar answered Sep 18 '22 13:09

AnthonyWJones