Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:30:42+00:00 2026-05-27T08:30:42+00:00

What is the opposite of XPath’s last() function, to select the "first" (apparently, there

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:30:43+00:00Added an answer on May 27, 2026 at 8:30 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an opposite of .find()? Where $('.myclass').except('#myid'); Would grab all elements with .myclass
I need an exactly opposite method of timezone_name_from_abbr(): http://php.net/manual/en/function.timezone-name-from-abbr.php I have a list of
I've got the opposite problem from " How do I make git ignore mode
Is there a opposite operator option for the Listcontains function in coldfusion? I need
Can we do the opposite of the INITCAP() function? It sets the first letter
Is there an exact opposite action to join in linQ to SQL? I want
Kind of the opposite of this question . Is there a way to tell
What is the opposite of the Math.tan(double x) function of java? I know that
(This question is the opposite of this one ) How can I go from
There are many threads about going the opposite way, but I am interested in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.