搜索XML项目PHP XPath

我有一个XML文档:</ p>

 &lt; items&gt;

&lt; item&gt;
&lt; id&gt; 1&lt; / id&gt;
&lt; title&gt;标题ABC Defg&lt; / title&gt;
&lt; author&gt;作者姓名&lt; / author&gt;
&lt; description&gt;说明文字 &lt; / description&gt;
&lt; / item&gt;
...
&lt; / items&gt;
</ code> </ pre>

我想做一个serching 检查标题,作者,描述是否包含短语</ p>

我不知道如何立即执行此操作并按相关性进行排序。 但它并不像搜索“单词”和“单词”那么重要。 我使用了php代码:</ p>

 &lt;?php 
$ xml = simplexml_load_file(file.xml);

$ query = $ _ GET ['query'];
$ nodes = $ xml-&gt; xpath(“// item [contains(title,'$ query')]”);

$ count = count($ nodes);
for($ i = 1; $ i&lt; = $ count; $ i ++){

$ nodes = $ xml-&gt; xpath(“// item [contains(title,' $查询')] [$ i]于“);

foreach($ nodes as $ node){
$ title = $ node-&gt; title;
$ desc = $ node-&gt; description;
$ auth = $ node-&gt; auth;
$ id = $ node-&gt; id;
echo“id:$ id&lt; br /&gt; title:$ title&lt; br /&gt; author:$ auth&lt; br /&gt; desc:$ desc&lt; p&gt;&amp; nbsp ;&lt; / p&gt;
?&gt;
</ code> </ pre>

我知道它只搜索标题,但问题是,当我搜索Word时,它无法找到单词和我 想要得到两个:单词和单词</ p>

如果你也可以帮助我在作者标题和描述中“连接”搜索,并以某种方式对它进行排序,我会非常熟练。</ p>

编辑:</ p>

我已设法搜索所有标签(不仅指定但对我来说还可以)</ p>

所以我有这样的代码:</ p>

  $ query = strtolower(rawurldecode($ _ GET ['s'])); 
$ nodes = $ xml-&gt; xpath( “// item [包含(翻译(。,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'$ query')]”)//。 - 所有我认为
</ code> </ pre>

< 我也使用了一种验证方式 $ query </ p>
</ div>

展开原文

原文

I have an XML document:

     <items>
     <item>
     <id>1</id>
     <title>Title ABC Defg</title>
     <author>Author Name</author>
     <description>Description text </description>
     </item>
     ...
     </items>

And i would like to do a serching and check title, author, description if contains a phrase

I dont know how to do it at once and order it by relevancy. But it is not such important as searching for "Word" and "word". I used the php code:

    <?php
    $xml=simplexml_load_file(file.xml); 
    $query=$_GET['query'];
    $nodes= $xml->xpath("//item[contains(title,'$query')]");

    $count = count($nodes);
    for ($i=1;$i<=$count;$i++){

    $nodes= $xml->xpath("//item[contains(title,'$query')][$i]"); 
    foreach($nodes as $node) {
    $title = $node->title;
    $desc= $node->description;
    $auth= $node->auth;
    $id= $node->id;
    echo "id: $id<br />title: $title<br />author: $auth<br />desc: $desc<p>&nbsp;</p>
    ?>

I know it searches only titles but the problem is that when i search for Word it cant find word and i would like to get both : word and Word

If you could also help me with "connecting" searching in author title and description and to order it somehow i would really appriciate.

EDIT:

I have manage to search in all tags (not only specified but for me it is ok)

so i have code like this:

    $query=strtolower(rawurldecode($_GET['s']));
    $nodes= $xml->xpath("//item[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'$query')]") // . - all i suppose

i also use kind of validation of $query

dousu8456
dousu8456 我强烈建议不要在XPath表达式框架中替换外部字符串-您可能很容易成为XPath注入的受害者。
8 年多之前 回复
dtd58256
dtd58256 stackoverflow.com/search?q=case+insensitive+xpath可能重复
8 年多之前 回复
dousi4472
dousi4472 在xpath中可能重复不区分大小写的匹配?
8 年多之前 回复
doujuchuan9915
doujuchuan9915 有关PHP中的伪大小写不敏感查询,请参阅stackoverflow.com/questions/3238989/...
8 年多之前 回复

1个回答

So you want to know, how to select all children of <items><item> elements that contain the text search for with xpath (I leave case sensitivity out, you find that on the linked answers). First of all, all item elements:

//items/item

You already have that. To only return those that contain some text, add the predicate:

//items/item[contains(., 'XYZ')]

If you only want to search within the <title> child element:

//items/item[contains(title, 'XYZ')]

This is basically what you have already, however you make your live needlessly hard: You don't need to do that twice, you can just iterate over the matches directly:

$nodes = $xml->xpath("//items/item[contains(title, 'XYZ')]");
foreach ($nodes as $node)
{
    foreach ($node as $name => $prop) {
        printf("%s: %s
", $name, $prop);
    }
    echo "
";
}

Output:

id: 3
title: Title XYZ
author: Author Name
description: Description text 

To learn about how to escape input to xpath (which is read-only, so this is not as dangerous as a SQL injection), consider the following example:

$query = 'XYZ';
$expression = sprintf("//item[contains(title,'%s')]", $query);
$nodes = $xml->xpath($expression);

It will create the following expression:

//item[contains(title,'XYZ')]

But what happens if there is some single quote in there? It will terminate the string and therefore create an error:

$query = 'd\'oh';

Will give:

Warning: SimpleXMLElement::xpath(): Invalid expression in ...

You can prevent this by doing something as outline here, specifically assigning the value to the document and comparing against it then:

$query = 'd\'oh';
$xml['query'] = $query;
$nodes = $xml->xpath("//item[contains(title, /*/@query)]");

Old: You ask multiple questions at once:

  1. How to search with xpath case-insensitive
  2. How to find out about relevancy (so to sort it by relevance)

Relevance is undefined. What could be relevant for one could be irrelevant for others, so it's hard to answer that part of your question without a specific definition on how relevancy could be metriced.

For case-insensitivity search, duplicate questions have been already linked, so you should be able to do that. Best first dulicate in my eyes:

But here as well it remains undefined what case, lower and upper, is. You have not specified a thing, so your question can not be really answered.

Also you don't really validate your input:

$query = $_GET['query'];
$nodes = $xml->xpath("//item[contains(title,'$query')]");

It's possible to inject xpath here with the GET parameter. Take care, otherwise you won't do any search at all.

doushi2047
doushi2047 当然,你也可以删除。 我只举了一个例子,如果你想要它,有些保守:应该能够搜索,所以保持它。
8 年多之前 回复
doufusi2013
doufusi2013 “但如果那里有一些单引号怎么办?” - 只有'和'可能有危险?如果是这样我可以删除字符串中的那些
8 年多之前 回复
dousi7579
dousi7579 相关性 - 我明白了 - 我只是想关注不敏感的情况,我正在研究它 - 我想用指定的孩子搜索父节点你能告诉我更多有关验证输入的信息吗? 我该如何预防注射?
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问