很简单,有一个 HTML 文件,还有一个带有变量 id 的 div
<div id="abc_1"><div>
id 的整数部分是可变的,因此它可以是 abc_892、abc_553 ...等
最好的查询是什么?
//div[starts-with(@id, "abc_")]
The currently accepted answer selects such unwanted elements as:
<div id="abc_xyz"/>
But only such div
elements must be accepted, whose id
not only starts with "abc_"
but the substring following the _
is a representation of an integer.
Use this XPath expression:
//div
[@id[starts-with(., 'abc_')
and
floor(substring-after(.,'_'))
=
number(substring-after(.,'_'))
]
]
This selects any div
element that has an id
attribute whose string value starts with the string "abc_"
and the substring after the -
is a valid representation of an integer.
Explanation:
Here we are using the fact that in XPath 1.0 this XPath expression:
floor($x) = number($x)
evaluates to true()
exactly when $x
is an integer.
This can be proven easily:
If $x
is an integer the above expression evaluates to true()
by definition.
If the above expression evaluates to true()
, this means that neither of the two sides of the equality are NaN
, because by definition NaN
isn't equal to any value (including itself). But then this means that $x
is a number (number($x)
isnt NaN
) and by definition, a number $x
that is equal to the integer floor($x)
is an integer.
Alternative solution:
//div
[@id[starts-with(., 'abc_')
and
'abc_' = translate(., '0123456789', '')
]
]