我正在尝试编写一个xpath,以选择所有包含以数字结尾并用大括号括起来的文本的元素。这些数字可以是任何数字,也可以是任何数字。在XPath1.0中可以吗?我不能使用正则表达式,因为XPath1.0不允许使用
<div class = '12345' >
<div class = '98231'>I want this (101)</div>
<div class = '98232'>I don't want this 101</div>
<div class = '98233'>I want this (1)</div>
<div class = '98234'>I don't want this (10A1)</div>
</div>
请您参考如下方法:
在XPath-1.0中,您不能使用RegEx。
但是您可以使用fn:number()函数使用fn:boolean函数检查给定的字符串是否为有效数字:
/div/div[boolean(number(substring-before(substring-after(.,'('),')')))]
显然,这仅在字符串中没有其他方括号的情况下有效。并且它不会检查括号中的数字是否在字符串的末尾。要添加此进一步的限制,请将表达式更改为
/div/div[not(boolean(string(normalize-space(substring-after(.,')'))))) and boolean(number(substring-before(substring-after(.,'('),')')))]
这会添加一个进一步的谓词,以检查
)之后是否存在除空格以外的其他字符。括号之间的前导空格和尾随空格由
fn:number()功能自动删除。

