这是针对 XPath 1.0 的。如果您的环境支持 XPath 2.0,请参阅here https://stackoverflow.com/a/23388974/18771.
是的。可能,但并不美丽。
/html/body//text()[
contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
'test'
)
]
这适用于预先已知字母表的搜索字符串。添加您希望看到的任何重音字符。
如果可以的话,用其他方式标记您感兴趣的文本,例如将其括在<span>
在构建 HTML 时具有特定的类。使用 XPath 比元素文本中的子字符串更容易定位此类内容。
如果这不是一个选项,您可以让 JavaScript(或用于执行 XPath 的任何其他宿主语言)帮助您构建动态 XPath 表达式:
function xpathPrepare(xpath, searchString) {
return xpath.replace("$u", searchString.toUpperCase())
.replace("$l", searchString.toLowerCase())
.replace("$s", searchString.toLowerCase());
}
xp = xpathPrepare("//text()[contains(translate(., '$u', '$l'), '$s')]", "Test");
// -> "//text()[contains(translate(., 'TEST', 'test'), 'test')]"
(Hat tip to @KirillPolishchuk's answer https://stackoverflow.com/a/8474552/18771 - of course you only need to translate those characters you're actually searching for.)
这种方法适用于任何搜索字符串,不需要事先了解字母表,这是一个很大的优点。
当搜索字符串可以包含单引号时,上述两种方法都会失败,在这种情况下,事情会变得更复杂 https://stackoverflow.com/a/44033074/18771.