PHP DOMXPath-> query()/-> evaluate()不匹配内部文本

时间:2019-04-17 10:14:18

标签: php html xpath domdocument

我目前正在尝试创建一个纯PHP遍历系统-这是因为我正在为某些人做一个即兴项目,但是他们想要尽可能少的JS(即:无)和理想的纯PHP。

我有一个看起来像这样的菜单:

ul {
  list-style-type: none;
}

nav > ul.sidebar-list ul.sub {
  display: none;
}

nav > ul.sidebar-list ul.sub.active {
  display: block;
}
<nav class="sidebar" aria-labelledby="primary-navigation">
  <ul class="sidebar-list">

    <!--each element has a sub-menu which is initially hidden by css when the page is loaded. Via php the appropriate path the current page and top-level links will be visible only-->
    <a href="#"><li>Home</li></a>
    <!--sub-items-->
    <ul class="sub active">
      <a href="#"><li>Barn</li></a>
      <a href="#"><li>Activities</li></a>
      <ul class="sub active">
        <a href="#"><li>News</li></a>
        <a href="#"><li>Movements</li></a>
        <a href="#"><li>Reviews</li></a>
        <a href="#"><li>About Us</li></a>
        <a href="#"><li>Terms of Use</li></a>
      </ul>
    </ul>
    <a href="#"><li>Events</li></a>
    <ul class="sub">
      <a href="#"><li>Overview</li></a>
      <a href="#"><li>Farming</li></a>
      <a href="#"><li>Practises</li></a>
      <a href="#"><li>Links</li></a>
      <ul class="sub">
        <a href="#"><li>Another Farm</li></a>
        <a href="#"><li>24m</li></a>
      </ul>
    </ul>
  </ul>
</nav>

为了尝试将页面的标题内文与菜单项内文匹配(可能不是最好的处理方式,但我仍在学习php),我运行:

$menu = new DOMDocument();
assert($menu->loadHTMLFile($menu_path), "Loading nav.html (menu file) failed");
//show content to log of the html document
error_log("HTML file: \n\n".$menu->textContent);

//set up a query to find an element matching the title string found
$xpath = new DOMXPath($menu);

$menu_query = "//a/li[matches(text(), '$title_text', 'i')]";
$elements = $xpath->query($menu_query);
error_log($elements ? ("Result of xpath query is: ".print_r($elements, TRUE)): "The xpath query for searching the menu is incorrect and will not find you anything!\ntype of return: ".gettype($elements));

我在以下位置获得了正确的回报:https://www.freeformatter.com/xpath-tester.html,但是在脚本中我没有。我尝试了多种不同的文本匹配组合,例如://x:a/x:li[lower-case(text())='$title_text'],但始终是一个空节点列表。

1 个答案:

答案 0 :(得分:1)

PHP使用XPath 1.0。 matches是XPath 2.0的一个功能,因此如果您要查找警告,则会在错误日志中看到警告。

PHP Warning:  DOMXPath::query(): xmlXPathCompOpEval: function matches not found in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. DOMXPath->query() php shell code:1

简单的区分大小写的匹配可以通过相等检查来完成。

$title_text = "Farming";
$menu_query = "//a/li[. = '$title_text']";

但是不区分大小写的搜索涉及将字符从大写转换为小写:

$title_text = "FaRmInG";
$title_text = strtolower($title_text);
$menu_query = "//a/li[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = '$title_text']";

无论哪种情况,我们最终都可以通过以下方式迭代NodeList:

$html = <<< HTML
<nav class="sidebar" aria-labelledby="primary-navigation">
  <ul class="sidebar-list">

    <!--each element has a sub-menu which is initially hidden by css when the page is loaded. Via php the appropriate path the current page and top-level links will be visible only-->
    <a href="#"><li>Home</li></a>
    <!--sub-items-->
    <ul class="sub active">
      <a href="#"><li>Barn</li></a>
      <a href="#"><li>Activities</li></a>
      <ul class="sub active">
        <a href="#"><li>News</li></a>
        <a href="#"><li>Movements</li></a>
        <a href="#"><li>Reviews</li></a>
        <a href="#"><li>About Us</li></a>
        <a href="#"><li>Terms of Use</li></a>
      </ul>
    </ul>
    <a href="#"><li>Events</li></a>
    <ul class="sub">
      <a href="#"><li>Overview</li></a>
      <a href="#"><li>Farming</li></a>
      <a href="#"><li>Practises</li></a>
      <a href="#"><li>Links</li></a>
      <ul class="sub">
        <a href="#"><li>Another Farm</li></a>
        <a href="#"><li>24m</li></a>
      </ul>
    </ul>
  </ul>
</nav>
HTML;
$menu = new DOMDocument();
$menu->loadHTML($html);
$xpath = new DOMXPath($menu);
$elements = $xpath->query($menu_query);
foreach ($elements as $element) {
    print_r($element);
}