xpath匹配不起作用

时间:2014-01-27 15:04:25

标签: php xpath

我正在尝试从此结果中获取"//td[text()='message']/following-sibling::*/text()"的消息字段内容(来自curl):

<BODY bgcolor=#dddddd>
<TABLE bgcolor=#dddddd border=1>
<TR>
<TD valign="top"><B>Something</B></TD>
<TD>ca</TD>
</TR>
<TR>
<TD valign="top"><B>Some list</B></TD>
<TD>
<TABLE>
<TR>
<TD>CA</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD valign="top"><B>message</B></TD>
<TD>CA already existed.</TD>
</TR>
</TABLE>
</BODY>
<br>

但它确实可以使用seens,有趣的是使用与python相同的表达式,我可以让它工作。那么,我怎样才能获得消息字段的内容?

PS:我正在使用这个在线测试工具:http://www.xpathtester.com/test

编辑:这是我的实际PHP代码:

<?php

function get_url_data($acl)
{
     // curl request
     $xml_content = http_request($acl);
     echo $xml_content ;
     $dom = new DOMDocument();
     @$dom->loadXML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query("//td[text()='message']/following-sibling::*/text()");
     return $content_title;
}
if(isset($_POST)==true && empty($_POST)==false){ 

//Convert content of text area into an array
$data = explode("\n", str_replace("\r", "", $_POST['sendme']));
}
foreach  ($data as $name => $value){
    $content = get_url_data($value);
    foreach ($content as $value)
    {
        echo $value->nodeValue . "<br/>";
    }

    echo "<br>";
}
?>

1 个答案:

答案 0 :(得分:0)

我能够使用它:

<?php
if(isset($_POST)==true && empty($_POST)==false){ 

//Convert content of text area into an array
$data = explode("\n", str_replace("\r", "", $_POST['sendme']));
}
foreach  ($data as $name => $value){
    $content = create_acl($value);
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = false;
    @$doc->loadHTML($content);
    $xpath = new DOMXpath($doc);
    $filtered = $xpath->query("//td[text()='message']/following-sibling::*/text()");
    foreach ($filtered as $e) {
      echo $e->nodeValue;
    }

    echo "<br>";
}
?>