简体   繁体   English

如何使Xpath搜索不区分大小写

[英]How do i make Xpath search case insensitive

I'm currently making a xpath search, I've got the the search working but I need to make it case insensitive. 我目前正在进行xpath搜索,搜索已经开始工作,但是我需要使其不区分大小写。 The xml file I'm using is 1.0 which from my research means I've got to use some thing called a translate function but I'm unsure of how to do this. 我正在使用的xml文件是1.0,根据我的研究,这意味着我必须使用一种称为“转换函数”的东西,但是我不确定如何执行此操作。

Here is my search file : 这是我的搜索文件:

$holidayDoc = simplexml_load_file('holidays.xml');      

// fetch data from form
$txtSearch = $_GET['txtSearch'];

$qry = "//channel/item[contains(.,\"$txtSearch\")]";


$holidays = $holidayDoc->xpath($qry);   // do the xpath query 
// now loop through all the students

echo "Showing title search results for $txtSearch";

foreach ($holidays as $holiday) 
{

 echo "<p><a href=\"{$holiday->link}\">{$holiday->title}</a></p>
    <p><small>$holiday->pubDate</small></p>";

Any help would be greatly appreciated thanks. 任何帮助将不胜感激谢谢。

XPath 1.0 : XPath 1.0:

$qry = "//channel/item[contains(
 translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),  
 translate($search, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]"

XPath 2.0 : XPath 2.0:

$qry = "//channel/item[lower-case(.) = lower-case($search)]"

Both replace all upper case to lower case. 两者都将所有大写字母替换为小写字母。

The currently accepted answer is flawed -- because nothing guarantees that the second argument of contains() is already converted to lower case . 当前接受的答案是有缺陷的-因为没有什么可以保证contains()的第二个参数已经转换为小写形式

Also, it uses '$search' -- and this is literally the string "$search" -- not the variable $search . 而且,它使用'$search' search'-实际上是字符串"$search" -而不是变量 $search

Here is a correct solution : 这是一个正确的解决方案

//channel/item
   [contains(translate(., 
                       'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 
                       'abcdefghjiklmnopqrstuvwxyz'),
             translate($txtSearch, 
                       'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 
                       'abcdefghjiklmnopqrstuvwxyz')
             )
   ]

The corresponding XPath 2.0 expression : 相应的XPath 2.0表达式

//channel/item[contains(lower-case(.), lower-case($txtSearch))]

Update : 更新

Based on this solution, @alain.janinm has corrected his answer. 基于此解决方案, @alain.janinm已更正了他的答案。

Technically, to do a case-blind comparison you should use a case-blind collation, unless your text happens to be English; 从技术上讲,要进行大小写盲目比较,应该使用大小写盲目排序规则,除非您的文字碰巧是英语。 normalizing both operands to upper case or to lower case does not give the correct result in all circumstances. 在所有情况下,将两个操作数规范化为大写或小写并不能给出正确的结果。 Unfortunately collation names (in XPath 2.0) aren't standardised so you have to look in your product documentation to see what collations are available. 不幸的是,归类名称(在XPath 2.0中)未标准化,因此您必须查看产品文档以查看可用的归类。

A case insensitive search can be using using the matches function like so 不区分大小写的搜索可以像这样使用matches函数使用

$qry = "//string[text() [matches(.,'^stringImTryingToFind$','i')]]"

the //string[text() [matches(.,'^OK$','i')]] section of the query uses regular expressions (REGEX) to determine a match. 查询的//string[text() [matches(.,'^OK$','i')]]部分使用正则表达式(REGEX)确定匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM