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
How to Horizontally Center an Unordered List of Unknown Width
How to Embed a Google Drive Folder in a Website
How to Style Individual Parts of an Input Placeholder
How Does Selenium Click on Elements That Are 50% on Screen and 50% Not on Screen
How to Make a Chevron Arrow Using CSS
Setting a Width and Height on an a Tag
Low Latency (< 2S) Live Video Streaming HTML5 Solutions
Why Cannot Change Checkbox Color Whatever I Do
Using HTML in Jquery UI Autocomplete
Is It Correct to Use Alt Tag for an Anchor Link
Is It Considered Bad Practice to Use Absolute Positioning
How to Change the Default Index Page in Apache
Conditional HTML Attributes Using Razor MVC3
Highlight the Navigation Menu for the Current Page
Height Percentage Not Working in CSS
Node.Js - How to Send Data from HTML to Express