与last()函数相反
与 XPath 的 last()
函数相反的是,从查询中选择“第一个”(显然没有 first()
)结果?
或者说,我该如何模仿呢?
更新
也许问题出在我使用祖先和上下文的具体示例上。
document.html
<div id="holder" special-header="We have a special header here">
<div class="element" special-header="And this is even more specific">
<p id="interested">Content, content</p>
</div>
</div>
script.php
$doc = new DOMDocument;
$doc->loadHTMLFile('document.html');
$contextNode = $document->getElementById('interested');
/* first try */
$first = $xpath->query('//ancestor::*[@special-header][1]', $contextNode);
echo $first->getAttribute('special-header'); // result == "We have a special header here" intended == "And this is even more specific"
/* second try */
$one = $xpath->query('//ancestor::*[@special-header][last()]', $contextNode); // in case it starts from "first found ancestor" and descends
echo $one->getAttribute('special-header'); // same result as before..
尝试使用 position()=1
我再次得到相同的结果。
What is the opposite of XPath's last()
function, to select the "first" (apparently, there is no first()
) result from query?
Or, how would I mimic it?
Update
Maybe the problem is with my specific example using ancestors and context.
document.html
<div id="holder" special-header="We have a special header here">
<div class="element" special-header="And this is even more specific">
<p id="interested">Content, content</p>
</div>
</div>
script.php
$doc = new DOMDocument;
$doc->loadHTMLFile('document.html');
$contextNode = $document->getElementById('interested');
/* first try */
$first = $xpath->query('//ancestor::*[@special-header][1]', $contextNode);
echo $first->getAttribute('special-header'); // result == "We have a special header here" intended == "And this is even more specific"
/* second try */
$one = $xpath->query('//ancestor::*[@special-header][last()]', $contextNode); // in case it starts from "first found ancestor" and descends
echo $one->getAttribute('special-header'); // same result as before..
Trying with position()=1
I get the same result again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
建议
1
会很奇怪吗?编辑:删除代码中的
//
前导符。Would it be strange to suggest
1
?EDIT: remove the
//
leader in your code.那么第一个项目始终是使用位置
1
选择的项目,因此您不需要为此使用函数,您可以简单地执行例如foo[1]
或>//栏[1]
.Well the first item is always the one selected with the position
1
so you don't need a function for that, you can simply do e.g.foo[1]
or//bar[1]
.问题在于表达式
//ancestor::*[@special-header]
的含义相当模糊,因为//
强制从根节点开始搜索。解决方案是删除表达式开头的
//
。顺便说一句,你的代码实际上不起作用=)。这是更正后的版本:
结果:
The problem is that the meaning of the expression
//ancestor::*[@special-header]
is rather vague, because//
forces searching from the root node.The solution is to remove the
//
at the beginning of the expressions.BTW, your code is actually not working =). Here is the corrected version:
Results in:
只需使用
1
作为第一个结果的索引。last()
的目的是让您可以从可变数量的索引中选择最后一个索引。但第一个索引始终是1
。Just use
1
as the index of the first result. The point oflast()
is so you can select the last index from a variable number of indexes. But the first index is always1
.