Xpath Testing That String Ends with Substring

XPath testing that string ends with substring?

XPath 2.0

//div[ends-with(@tagname, 'Destination')]

XPath 1.0

//div[substring(@tagname, string-length(@tagname) 
- string-length('Destination') + 1) = 'Destination']

Xpath ends-with does not work

The ends-with function is part of xpath 2.0 but browsers (you indicate you're testing with chrome) generally only support 1.0. So you'll have to implement it yourself with a combination of string-length, substring and equals

substring(@id, string-length(@id) - string-length('register') +1) = 'register'

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

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)
]

Need XPath ends-with function

Your XPath is missing the step of checking return value of substring() whether or not it is equals to the expected string ending value "z". So the correct XPath would be :

//country[substring(@name, string-length(@name) - string-length('z') +1) = 'z']/@name

And since -string-length('z') +1 equals to 0, that part can be omitted to get a simpler XPath, similar to the one posted by @Kirill Polishchuk in the other answer.

How to match string that ends with a number using XPath

If you need XPath 1.0 solution, try below:

//tagA[boolean(number(substring-after(@attA, "VAL"))) or number(substring-after(@attA, "VAL")) = 0]

If @attA cannot be "VAL0", then just

//tagA[boolean(number(substring-after(@attA, "VAL")))]


Related Topics



Leave a reply



Submit