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 6880715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:35+00:00 2026-05-27T05:00:35+00:00

Answering a question , I noticed strange libxml behavior on the following expression: //ancestor::*[@id][1]

  • 0

Answering a question, I noticed strange libxml behavior on the following expression:

//ancestor::*[@id][1]

for a given context node. I am trying to understand what the expression actually means.

Here is a snippet in PHP and result of its invocation:

$html = <<<HTML
<div id="div1">
    <div id="div2">
        <p id="p1">Content</p>
    </div>
    <div id="div3">
        <p id="p2">Content</p>
    </div>
</div>
HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);

$contextNode = $doc->getElementById('p1');
$list = $xpath->query('//ancestor::*[@id][1]', $contextNode);

printf("NodeList length: %d\n", $list -> length);
foreach ($list as $node) {
    printf ("item/@id -> %s\n", $node -> getAttribute('id'));
}

Result:

NodeList length: 5
item/@id -> div1
item/@id -> div2
item/@id -> p1
item/@id -> div3
item/@id -> p2
  • 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-27T05:00:36+00:00Added an answer on May 27, 2026 at 5:00 am

    //ancestor::*[@id][1] is a short form for /descendant-or-self::node()/ancestor::*[@id][1] so the context node is only relevant for determining its root or document node /, then in the first step descendand-or-self::node() a node-set is formed of the document node and all its descendant nodes of all kinds (element nodes, text nodes, comment nodes, processing instruction nodes), then the next step for each of those nodes determines ancestor::*[@id][1], that is of all ancestor elements having an id attribute the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
While answering this question I noticed that I got the following dialog while atempting
Whilst doing exam revision I am having trouble answering the following question from the
While answering another question, I thought of the following example: void *p; unsigned x
While answering a question on SO yesterday, I noticed that if an object is
Before answering this question, understand that I am not asking how to create my
After answering this question I was trying to find is_complete template in Boost library
In answering a question on stackoverflow, I noticed that GHCi (interactive) is assigning a
After answering this question I put together the following C# code just for fun:
When answering this question I made some research which really confuses me. I noticed

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.