不区分大小写的xpath contains()可能吗? [英] case insensitive xpath contains() possible?

查看:188
本文介绍了不区分大小写的xpath contains()可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行我的DOM的所有文本节点并检查nodeValue是否包含某个字符串。

  / html / body // text()[contains(。,'test')] 

这是区分大小写的。但是,我还要抓住测试 TEST oder TesT 。这是可能的XPath(在JavaScript中)吗?

解决方案

这是针对XPath 1.0的。如果您的环境支持XPath 2.0,请参阅此处






是的。可能,但不漂亮。

  / html / body // text()[
contains(
翻译(。,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),
'test'

]

如果可以,请使用其他方式标记您感兴趣的文本部分,例如将其封装在< span> 中某个班级。



如果无法做到这一点,您可以让JavaScript帮助您构建适当的XPath表达式:



< pre class =lang-js prettyprint-override> function xpathPrepare(xpath,searchString){
return xpath.replace($ u,searchString.toUpperCase())
.replace($ l,searchString.toLowerCase())
.replace($ s,searchString.toLowerCase());
}

xp = xpathPrepare(// text()[contains(。('$'','$ l'),'$ s')],测试);
// - > // text()[contains(。,'TEST','test'),'test')]

(帽子提示 @ KirillPolishchuk的答案 - 当然你只需翻译那些字符''实际上搜索 for)


I'm running over all textnodes of my DOM and check if the nodeValue contains a certain string.

/html/body//text()[contains(.,'test')]

This is case sensitive. However, I also want to catch Test, TEST oder TesT. Is that possible with XPath (in JavaScript)?

解决方案

This is for XPath 1.0. If your environment supports XPath 2.0, see here.


Yes. Possible, but not beautiful.

/html/body//text()[
  contains(
    translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
    'test'
  )
]

If you can, mark the portions of text that interest you with some other means, like enclosing them in a <span> that has a certain class.

If that's not possible, you can have JavaScript help you with building an appropriate XPath expression:

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 - of course you only need to translate those characters you're actually searching for)

这篇关于不区分大小写的xpath contains()可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆