【发布时间】:2019-04-17 10:14:17
【问题描述】:
我目前正在尝试创建一个纯 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'],但总是一个空节点列表。
【问题讨论】:
-
fn:lower-case()是 XPath 2.0+ 函数。 -
@Alejandro 我如何知道我正在运行的 XPath 版本?另外,如果肯定不支持它,它只会返回 false 对吗?
-
如果没有为您的执行上下文定义函数,XPath 引擎应该报告错误。在 XPath 执行上下文中,无法知道支持的语言版本。如果 XPath 2.0 函数失败,那么您有一个 XPath 1.0 引擎。
-
它在php.net/manual/en/domxpath.query.php 上说如果查询无效,它将返回false,但是如果我使用不受支持的函数,您是说php 实际上会抛出错误吗?因为我已经尝试过 lower-case() 并且它没有抛出错误,所以只返回了 0 个匹配项
标签: php html xpath domdocument