Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath expression to find elements whose tag name contains 'Name'

I am a newcomer to XPath.

I am looking for a way to get all elements whose tag name contains a particular string.

For example, if I have XML like below, I want to get all the elements whose tag name contains the word 'Name'. i.e., I want to fetch the following elements: <SquareName>, <RectangleName>, and <ParallelogramName>.

I tried some combinations of name(), contains() etc., but it did not work. Please suggest.

<Objects>  <Four-Sided>    <Square>       <SquareName>ABCD</SquareName>       <Length>4</Length>       <Height>4</Height>       <Colour>Blue</Colour>    </Square>    <Rectangle>       <RectangleName>EFGH</RectangleName>       <Length>10</Length>       <Height>6</Height>       <Colour>Brown</Colour>    </Rectangle>    <Parallelogram>       <ParallelogramName>WXYZ</ParallelogramName>       <Length>12</Length>       <Height>4</Height>       <Colour>Black</Colour>    </Parallelogram> </Four-Sided> </Objects> 
like image 432
user2082317 Avatar asked Feb 18 '13 07:02

user2082317


People also ask

How find XPath text contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

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.

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.


1 Answers

For an XPath solution:

//*[contains(local-name(), 'Name')] 
like image 70
Jens Erat Avatar answered Sep 24 '22 16:09

Jens Erat