例如,对于下面的XML
<CATALOG>
<CD title="Empire Burlesque"/>
<CD title="empire burlesque"/>
<CD title="EMPIRE BURLESQUE"/>
<CD title="EmPiRe BuRLeSQuE"/>
<CD title="Others"/>
<CATALOG>
如何使用//CD[@title='empire burlesque']
之类的xpath匹配前4条记录。有没有xpath函数可以做到这一点?其他解决方案,如PHP函数也被接受。
发布于 2010-05-24 05:39:14
XPath 2有一个小写(和大写)字符串函数。这不完全等同于不区分大小写,但希望它足够接近:
//CD[lower-case(@title)='empire burlesque']
如果您使用的是XPath 1,则会有一个hack using translate。
发布于 2016-09-01 10:57:22
这在Chrome开发人员工具中定位元素不起作用,我想在屏幕上定位‘提交’按钮
//input[matches(@value,'submit','i')]
但是,使用'translate‘将所有大写字母替换为小写字母,如下所示
//input[translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'submit']
更新:我刚刚找到了“matches”不起作用的原因。我使用的是Chrome和xpath 1.0,它不会理解'matches‘的语法。应该是xpath 2.0
发布于 2010-05-24 06:09:56
一种可能的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")];
https://stackoverflow.com/questions/2893551
复制相似问题