【发布时间】:2012-05-11 07:55:57
【问题描述】:
使用 XPath,我想“匹配整个单词”(用户选项,就像在 VS 搜索中一样)。
似乎contains 和matches 函数的工作方式相似,但匹配允许使用i 之类的标志以不区分大小写。
换句话说,我通过这两个 XPath 查询得到了相同的结果:
<pets>
<dog name="Rupert" color="grey"/>
<dog name="Ralph" color="brown"/>
<cat name="Marvin the Cat" color="white"/>
<cat name="Garfield the Cat" color="orange"/>
<cat name="Cat" color="grey"/>
<cat name="Fluffy" color="black"/>
</pets>
Matches XPath: //cat[descendant-or-self::*[@*[matches(.,'Cat')]]]
returns:
<cat name="Marvin the Cat" color="white"/>
<cat name="Garfield the Cat" color="orange"/>
<cat name="Cat" color="grey"/>
Contains XPath: //cat[descendant-or-self::*[@*[contains(.,'Cat')]]]
returns:
<cat name="Marvin the Cat" color="white"/>
<cat name="Garfield the Cat" color="orange"/>
<cat name="Cat" color="grey"/>
但我想使用matches 只返回匹配“Cat”整个单词的结果:
<cat name="Cat" color="grey"/>
如何调整匹配查询以匹配整个单词?
编辑: 我忘了说我还需要使用matches函数,因为我需要不区分大小写的标志。
【问题讨论】:
标签: regex search xpath xpath-2.0