Is it possible to select elements of xml tree that end with a given string? Not the elements that contain an attribute that ends with a string, but the elements themselves?
-
Yes, it's possible. Have a look at [this SO answer](https://stackoverflow.com/a/11857166/1305969), which explains an XPath-1.0 version of the XPath-2.0 function `ends-with`. You can use `ends-with` on any element - which answers your question. – zx485 Sep 28 '18 at 20:38
2 Answers
As mentioned in my comment, you can use the XPath-2.0 function ends-with
to solve this. Its signature is
ends-with
fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
fn:ends-with( $arg1 as xs:string?,
$arg2 as xs:string?,
$collation as xs:string) as xs:booleanSummary:
Returns an xs:boolean indicating whether or not the value of $arg1 ends with a sequence of collation units that provides a minimal match to the collation units of $arg2 according to the collation that is used.
So you can use the following expression to
select elements of xml tree that end with a given string
document-wide
//*[ends-with(.,'given string')]
To realize this in Xpath-1.0, refer to this SO answer.

- 28,498
- 28
- 50
- 59
For example, to select all elements that end with "es", you can search for all the elements whose name contains the substring "es" starting at the position corresponding to the length of the name minus 1:
//*[substring(name(),string-length(name())-1,2) = "es"]

- 231,213
- 25
- 204
- 289
-
OP didn't mention that should stick to XPath 1.0 solution, why not to suggest using `ends-with` function? – Andersson Sep 28 '18 at 21:01