使用XPath,我想“匹配整个单词”(用户选项,就像在VS搜索中一样).
似乎函数包含和匹配工作类似,虽然匹配允许像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"/>
但是我想使用匹配来返回仅匹配“Cat”全字的结果:
<cat name="Cat" color="grey"/>
如何调整匹配查询以使其与整个单词匹配?
编辑:
我忘了提到我还需要使用matches函数,因为我需要case insensitivity标志.
解决方法
使用^和$字符作为锚点怎么样?
//cat[descendant-or-self::*[@*[matches(.,'^Cat$')]]]
从RegEx Syntax in XQuery 1.0 and XPath 2.0开始:
Two Meta-characters,^ and $ are added. By default,the Meta-character
^ matches the start of the entire string,while $ matches the end of the entire string.