与last()函数相反

发布于 2024-12-19 07:48:21 字数 1153 浏览 0 评论 0原文

与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萤火眠眠 2024-12-26 07:48:21

建议 1 会很奇怪吗?

编辑:删除代码中的 // 前导符。

Would it be strange to suggest 1?

EDIT: remove the // leader in your code.

青朷 2024-12-26 07:48:21

那么第一个项目始终是使用位置 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].

渔村楼浪 2024-12-26 07:48:21

问题在于表达式 //ancestor::*[@special-header] 的含义相当模糊,因为 // 强制从根节点开始搜索。

解决方案是删除表达式开头的 //

顺便说一句,你的代码实际上不起作用=)。这是更正后的版本:

$html = <<<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>
HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$contextNode = $doc->getElementById('interested');
$xpath = new DOMXpath($doc);

$first = $xpath->query('ancestor::*[@special-header][1]',
                       $contextNode) -> item(0);
echo $first->getAttribute('special-header') . "\n";

$one = $xpath->query('ancestor::*[@special-header][last()]',
                     $contextNode)-> item(0);
echo $one->getAttribute('special-header') . "\n"; 

结果:

C:\>\php\php.exe dom_games.php
And this is even more specific
We have a special header here

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:

$html = <<<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>
HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$contextNode = $doc->getElementById('interested');
$xpath = new DOMXpath($doc);

$first = $xpath->query('ancestor::*[@special-header][1]',
                       $contextNode) -> item(0);
echo $first->getAttribute('special-header') . "\n";

$one = $xpath->query('ancestor::*[@special-header][last()]',
                     $contextNode)-> item(0);
echo $one->getAttribute('special-header') . "\n"; 

Results in:

C:\>\php\php.exe dom_games.php
And this is even more specific
We have a special header here
残疾 2024-12-26 07:48:21

只需使用 1 作为第一个结果的索引。 last() 的目的是让您可以从可变数量的索引中选择最后一个索引。但第一个索引始终是1

Just use 1 as the index of the first result. The point of last() is so you can select the last index from a variable number of indexes. But the first index is always 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文