XPath 2.0
This is straight-forward in XPath 2.0, where this expression,
//*[matches(name(), '^cup.*\d$')]
will select all elements whose name starts with cup
and ends with a digit, as requested.
XPath 1.0
Since XPath 1.0 lacks regex, ends-with()
, and functions to test if a string is a numbers, your request gets to be more complicated with XPath 1.0. Here's one possible working solution:
//*[starts-with(name(), 'cup')
and number(substring(name(),string-length(name())))
= number(substring(name(),string-length(name())))]
Note that the second clause is a clever way by Dimitre Novatchev to test
in XPath 1.0 whether a string is a number.
Here's a shorter way to check for ending in a digit in XPath 1.0:
//*[starts-with(name(), 'cup')
and not(translate(substring(name(),string-length(name())), '0123456789', ''))]