Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to return only elements containing the text, and not its parents

Tags:

xml

xpath

In this xml, I want to match, the element containing 'match' (random2 element)

<root>  <random1>   <random2>match</random2>   <random3>nomatch</random3>  </random1> </root> 

ok, so far I have:

//[re:test(.,'match','i')] (with re in the proper namespace) 

this returns random2, random1 and root... I would like to get only "random2"

any ideas?

like image 674
julian Avatar asked Jun 08 '10 01:06

julian


People also ask

How do I select all child elements in XPath?

For the div element with an id attribute of hero //div[@id='hero'] , these XPath expression will select elements as follows: //div[@id='hero']/* will select all of its children elements. //div[@id='hero']/img will select all of its children img elements. //div[@id='hero']//* will select all of its descendent elements.

What is the meaning of '/' in XPath?

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What does * indicate in XPath?

The XPath default axis is child , so your predicate [*/android.widget.TextView[@androidXtext='Microwaves']] is equivalent to [child::*/child::android.widget.TextView[@androidXtext='Microwaves']] This predicate will select nodes with a android. widget. TextView grandchild and the specified attribute.


1 Answers

Do you want to find elements that contain "match", or that equal "match"?

This will find elements that have text nodes that equal 'match' (matches none of the elements because of leading and trailing whitespace in random2):

//*[text()='match'] 

This will find all elements that have text nodes that equal "match", after removing leading and trailing whitespace(matches random2):

//*[normalize-space(text())='match'] 

This will find all elements that contain 'match' in the text node value (matches random2 and random3):

//*[contains(text(),'match')] 

This XPATH 2.0 solution uses the matches() function and a regex pattern that looks for text nodes that contain 'match' and begin at the start of the string(i.e. ^) or a word boundary (i.e. \W) and terminated by the end of the string (i.e. $) or a word boundary. The third parameter i evaluates the regex pattern case-insensitive. (matches random2)

//*[matches(text(),'(^|\W)match($|\W)','i')] 
like image 171
Mads Hansen Avatar answered Oct 18 '22 11:10

Mads Hansen