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.
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: