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

  • SEARCH
  • Home
  • 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 8879837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:02:23+00:00 2026-06-14T20:02:23+00:00

What are reasons why an XPath query could only return the last match? I

  • 0

What are reasons why an XPath query could only return the last match? I am running a query on an HTML fragment which clearly has multiple <a name="..."> tags, but the XPath query will only return one element, which happens to be the last element.

function extract($html) {
    // This test shows that the retrieved HTML fragment indeed contains multiple anchor tags
    echo "<textarea>".$html."</textarea>";

    // parse the data
    $dom = new DomDocument();
    @$dom->loadHTML($html); // we use @$dom to suppress some warnings
    $xpath = new DOMXPath($dom);

    // find the html code for the post
    $query = "//a[contains(@name, 'post')]"; 
    $rows = $xpath->query($query);

    // This will return 1
    echo "Elements found: " . count($rows);

    ...
}
  • 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-06-14T20:02:25+00:00Added an answer on June 14, 2026 at 8:02 pm

    You need to probe $rows->length. The reason is that $rows is an instance of DOMNodeList (an object that holds a list of DOMNode instances). And since DOMNodeList doesn’t implement the interface Countable, it cannot be probed by count() the way you’d expect. It simply returns 1, because it is a single object, not the amount of DOMNodes it has aggregated.

    And so, the result of your query does not return only the last match. It returns them all, and you can iterate through them with foreach, like so:

    foreach( $rows as $row )
    {
        // do something with $row (instance of DOMNode)
    }
    

    … or with a for loop, like so:

    for( $i = 0, $len = $rows->length; $i < $len; $i++ )
    {
        $row = $rows->item( $i );
        // do something with $row (instance of DOMNode)
    }
    

    … etc.

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

Sidebar

Related Questions

For reasons that only the developers can understand, Firefox will create and open .url
Any reasons why this can not be standard behavior of free() ? multiple pointers
For reasons beyond my control, I need to return const char* from a function,
I want to add an XML fragment to the last element to an XML
What is the XPath to find only ONE node (whichever) having a certain attribute
We have an SOA based project which has been built from the ground up
Which is the best and fastest way to find the element using webdriver? By.XPath
I have a script which generates like 16000 html pages and saves it in
require 'nokogiri' require 'open-uri' page = Nokogiri::HTML(open(http://wwp.greenwichmeantime.com/)) puts page.xpath(.//*[@id='offset']/span[1]).text this should output the gmt
For an XSLT template, I need an XPath expression targeting elements which have a

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.