xml xpath中不区分大小写的匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2893551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
case-insensitive matching in xpath?
提问by Ethan
For example, for the xml below
例如对于下面的xml
<CATALOG>
<CD title="Empire Burlesque"/>
<CD title="empire burlesque"/>
<CD title="EMPIRE BURLESQUE"/>
<CD title="EmPiRe BuRLeSQuE"/>
<CD title="Others"/>
<CATALOG>
How to match the first 4 records with xpath like //CD[@title='empire burlesque']. Is there xpath function to do this? Other solutions like PHP function are also accepted.
如何将前 4 条记录与 xpath 匹配,例如//CD[@title='empire burlesque']. 是否有 xpath 函数来执行此操作?也接受其他解决方案,如 PHP 函数。
回答by Matthew Flaschen
XPath 2 has a lower-case (and upper-case) string function. That's not quite the same as case-insensitive, but hopefully it will be close enough:
XPath 2 具有小写(和大写)字符串函数。这与不区分大小写不完全相同,但希望它足够接近:
//CD[lower-case(@title)='empire burlesque']
If you are using XPath 1, there is a hack using translate.
如果您使用的是 XPath 1,则可以使用 translate进行破解。
回答by Mads Hansen
matches()is an XPATH 2.0 function that allows for case-insensitive regex matching.
match()是一个 XPATH 2.0 函数,它允许不区分大小写的正则表达式匹配。
One of the flagsis ifor case-insensitive matching.
其中的标志是i不区分大小写的匹配。
The following XPATH using the matches()function with the case-insensitive flag:
以下 XPATH 使用带有不区分大小写标志的matches()函数:
//CD[matches(@title,'empire burlesque','i')]
回答by Bhuvanesh Mani
This does not work in Chrome Developer tools to locate a element, i am looking to locate the 'Submit' button in the screen
这在 Chrome 开发者工具中无法定位元素,我希望找到屏幕中的“提交”按钮
//input[matches(@value,'submit','i')]
However, using 'translate' to replace all caps to small works as below
但是,使用“翻译”将所有大写字母替换为小作品,如下所示
//input[translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'submit']
Update: I just found the reason why 'matches' doesnt work. I am using Chrome with xpath 1.0 which wont understand the syntax 'matches'. It should be xpath 2.0
更新:我刚刚找到了“匹配”不起作用的原因。我正在使用带有 xpath 1.0 的 Chrome,它无法理解语法“匹配”。它应该是 xpath 2.0
回答by Tomalak
One possible PHP solution:
一种可能的 PHP 解决方案:
// load XML to SimpleXML
$x = simplexml_load_string($xmlstr);
// index it by title once
$index = array();
foreach ($x->CD as &$cd) {
$title = strtolower((string)$cd['title']);
if (!array_key_exists($title, $index)) $index[$title] = array();
$index[$title][] = &$cd;
}
// query the index
$result = $index[strtolower("EMPIRE BURLESQUE")];
回答by jimp
You mentioned that PHP solutions were acceptable, and PHP does offer a way to accomplish this even though it only supports XPath v1.0. You can extend the XPath support to allow PHP function calls.
您提到 PHP 解决方案是可以接受的,而且 PHP 确实提供了一种方法来实现这一点,即使它只支持 XPath v1.0。您可以扩展 XPath 支持以允许 PHP 函数调用。
$xpathObj = new DOMXPath($docObj);
$xpathObj->registerNamespace('php','http://php.net/xpath'); // (required)
$xpathObj->registerPhpFunctions("strtolower"); // (leave empty to allow *any* PHP function)
$xpathObj->query('//CD[php:functionString("strtolower",@title) = "empire burlesque"]');
See the PHP registerPhpFunctionsdocumentation for more examples. It basically demonstrates that "php:function" is for boolean evaluation and "php:functionString" is for string evaluation.
有关更多示例,请参阅PHP registerPhpFunctions文档。它基本上表明“php:function”用于布尔评估,而“php:functionString”用于字符串评估。

