Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use starts-with() , contains() and ends-with() in XPath to find the xml node innertext? in XPATH 1.0

<ArticleBackmatter>
   <Heading>Ethical standards and patient consent</Heading>
   <Heading>Ethical standards and patient</Heading>
   <Heading>standards and patient consent</Heading>
</ArticleBackmatter>

I want to get the inner text with start from "Ethical" , contains "and " and end with "consent" in the Heading node.

like image 656
Karuppa Samy Avatar asked Oct 06 '14 13:10

Karuppa Samy


People also ask

How do you write the XPath using contains?

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

What is text () function 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.

What Startswith () function will do using XPath?

XPath starts-with() is a function used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. In this method, the starting text of the attribute is matched to find the element whose attribute value changes dynamically.


1 Answers

One possible way:

//Heading[starts-with(., 'Ethical') and ends-with(., 'consent')]

The ends-with() function is XPath 2.0. In XPath 1.0, it can be replaced using substring() and string-length(). Here is the equivalent XPath 1.0 (wrapped for readability):

//Heading[
            starts-with(., 'Ethical') 
                and 
            'consent' = substring(., string-length(.) - string-length('consent') +1)
         ]
like image 189
har07 Avatar answered Oct 02 '22 11:10

har07