【问题标题】:XPath query does not return answerXPath 查询不返回答案
【发布时间】:2016-05-28 08:26:28
【问题描述】:

我尝试用 HTML/PHP/XML 构建一个简单的聊天机器人。 “大脑”是用 XML 构建的。将通过调用 searchBrain.php 的 AJAX 提交一个 html 表单。问题是,当提交表单时,searchBrain 没有查询我的brain.xml 文件。有人可以帮帮我吗?

大脑.xml

<?xml version="1.0" encoding="UTF-8"?>
<brain>
    <neuron>
        <id>1</id>
        <input>wie ben jij?</input>
        <code></code>
        <output>Ik ben Skye</output>
        <keywords>wie,ben,jij,Wie,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>2</id>
        <input>hoe gaat het?</input>
        <code></code>
        <output>Met mij gaat het goed. Hoe gaat het me u?</output>
        <keywords>hoe,gaat,het,Hoe,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>3</id>
        <input>wat ben jij?</input>
        <code></code>
        <output>Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
        <keywords>wat,ben,jij,Wat,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>4</id>
        <input>hoe zie jij er uit?</input>
        <code></code>
        <output></output>
        <keywords>hoe,zie,jij,er,uit,Hoe,?</keywords>
        <image><!-- insert binary.jpg from imgLib --></image>
    </neuron>
    <neuron>
        <id>5</id>
        <input>stel jezelf even voor</input>
        <code></code>
        <output>Hoi ik ben Skye, ik ben online gegaan op 17-02-2016.Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
        <keywords>stel,jezelf,even,voor</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>6</id>
        <input>Welke dag is het vandaag?</input>
        <code><!-- PHP code om huidige dag weer te geven --></code>
        <output>Vandaag is het </output>
        <keywords>welke,dag,is,het,vandaag,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>7</id>
        <input>wanneer ben je jarig?</input>
        <code></code>
        <output>Ik ben online gegaan op 17 Februari, dus ik ben jarig op 17 Februari :-)</output>
        <keywords>wanneer,ben,je,jarig,?</keywords>
        <image><!-- jarig.jpeg from imgLib --></image>
    </neuron>
</brain>

searchBrain.php

<?php 

    //Create DOMDocument based on XML
    $dom = new DOMDocument();

    // We don't want to bother with white spaces
    $dom->preserveWhiteSpace = false;

    //Load brain.xml
    $dom->Load('brain.xml');

    //Get POST value
    $sentence = $_POST['sentence'];

    //Lowercase the XML so non case sensitive search is possible
    $xml = strtolower($xml);

    //Create XPath based on the DOM Document to search it
    $xpath = new DOMXpath($dom);

    //Define keywords for search
    $searchKeywords = array($sentence);

    //Iterate all of them to make them into valid XPath
    $searchKeywords = array_map(
    function($keyword){
        //Replace any single quotes with an escaped single quote
        $keyword = str_replace('\'','\\\'',$keyword);
        return 'contains(.,\''.$keyword.'\')';
    },
    $searchKeywords
    );

    //Implode all the keywords using and, could 
    //be changed to be an "or" condition if needed
    $searchKeywords = implode(' and ',$searchKeywords);

    //The search keywords now look like contains(.,'good') and
    //contains(.,'hello')

    //Search for any neuron tag that contains the text
    //submitted by the from
    $nodes = $xpath->query('//keywords['.$searchKeywords.']');

    //Iterate all nodes
    foreach($nodes as $node){
        //Find the output node an print its content
        var_dump($xpath->query('output',$node)->item[3]->textContent);
    }

?>

还有index.php

<!DOCTYPE html>
<head>
    <title>Skye</title>
    <script src="jquery-2.1.4.js"></script>

    <script>
    $(document).ready(function() {
        $("#Skye").submit(function(e){
            var url = "searchBrain.php";

            $.ajax({
                type: "POST",
                url: url,
                data: $("#Skye").serialize(),
                success: function(response){
                    $("#result").html(response); 
                    //alert(response);
                }
            });
            e.preventDefault();
        });
    });
    </script>

    <link rel="stylesheet" type="text/css" href="gui/css/skye.css" />
</head>
<body>

    <h1>Skye</h1>

    <div id="result"></div>

    <form id="Skye" name="Skye" method="POST" >
        <input type="text" id="sentence" name="sentence" autofocus="autofocus" />
        <input type="submit" id="display" value="Stuur..." />
    </form>



</body>
</html>

【问题讨论】:

  • 我不认为这是您的问题的原因,但未定义变量:xml - $xml = strtolower($xml);
  • btw XPath 1 中的引号没有转义。这是解决问题的函数示例stackoverflow.com/a/27440520/2265374
  • @MattinWashington 谢谢我错过了那个。哇,谢谢!我会调查的。
  • 无关,但在这里你有一个无忧无虑的方法:fsockopen.com/php-programming/…

标签: php html ajax xml xpath


【解决方案1】:

我发现您的查询有问题。如果您正在寻找完全匹配并且想要父节点,您需要这个:

$nodes = $xpath->query('//neuron/keywords[contains(text(), "wanneer") and contains(text(), "gaat")]/..');

所以你可以用上面的替换implode(' and ', $searchKeywords)

您也可以像下面的评论员那样简写上述语法:

$nodes = $xpath->query('//neuron[keywords[contains(., "wanneer") or contains(., "gaat")]]');

它有什么作用?

它会查看每个神经元,然后搜索 &lt;keywords&gt; 的文本值是否具有您要查找的内容,/.. 就像一个基本上返回父级的文件路径。很漂亮。现在你可以得到&lt;output&gt;标签了。

//Iterate all nodes
foreach($nodes as $node){
    //Find the output node an print its content
    var_dump($xpath->query('output',$node)->item(0)->textContent);
}

这是我测试过的完整代码。您可以将其复制粘贴到测试文件中以查看结果并进行修改,以便将其应用到您的脚本中。

<?php

$xml = xml_str();
$dom = new DOMDocument();
$dom->loadXML($xml);

$xpath= new DOMXPath($dom);
$nodes = $xpath->query('//neuron/keywords[contains(text(), "wanneer") or contains(text(), "gaat")]/..');

foreach ($nodes as $node) {
    var_dump($xpath->query('output', $node)->item(0)->textContent);
}

function xml_str()
{
    return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<brain>
    <neuron>
        <id>1</id>
        <input>wie ben jij?</input>
        <code></code>
        <output>Ik ben Skye</output>
        <keywords>wie,ben,jij,Wie,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>2</id>
        <input>hoe gaat het?</input>
        <code></code>
        <output>Met mij gaat het goed. Hoe gaat het me u?</output>
        <keywords>hoe,gaat,het,Hoe,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>3</id>
        <input>wat ben jij?</input>
        <code></code>
        <output>Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
        <keywords>wat,ben,jij,Wat,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>4</id>
        <input>hoe zie jij er uit?</input>
        <code></code>
        <output></output>
        <keywords>hoe,zie,jij,er,uit,Hoe,?</keywords>
        <image><!-- insert binary.jpg from imgLib --></image>
    </neuron>
    <neuron>
        <id>5</id>
        <input>stel jezelf even voor</input>
        <code></code>
        <output>Hoi ik ben Skye, ik ben online gegaan op 17-02-2016.Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
        <keywords>stel,jezelf,even,voor</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>6</id>
        <input>Welke dag is het vandaag?</input>
        <code><!-- PHP code om huidige dag weer te geven --></code>
        <output>Vandaag is het </output>
        <keywords>welke,dag,is,het,vandaag,?</keywords>
        <image></image>
    </neuron>
    <neuron>
        <id>7</id>
        <input>wanneer ben je jarig?</input>
        <code></code>
        <output>Ik ben online gegaan op 17 Februari, dus ik ben jarig op 17 Februari :-)</output>
        <keywords>wanneer,ben,je,jarig,?</keywords>
        <image><!-- jarig.jpeg from imgLib --></image>
    </neuron>
</brain>
XML;
}
?>

【讨论】:

  • 应该能够通过使用嵌套谓词和使用.而不是text()来稍微简化xpath://neuron[keywords[contains(., "wanneer") or contains(., "gaat")]]
  • 好电话!更新了!
猜你喜欢
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多