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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:42:13+00:00 2026-05-19T01:42:13+00:00

I have a several tables nested within a table that I am parsing using

  • 0

I have a several tables nested within a table that I am parsing using php xpath.

I’m using a series of xpaths because I’m breaking up the code into conceptual units across several methods calls, and this structure has been working perfectly in other scenarios without nested tables.

Here’s the code:

// create a host DOM document
$dom = new DOMDocument();

// load the html string into the dom
$dom->loadHTML($html_string);

// make an xpath object out of the dom
$xpath = new DOMXpath($dom);

// run query to extract the rows from the master table
$context_nodes = $xpath->query('//table[@id="id1"]/tr[position()>1]');

// parse data from the individual tables nested in each master table row
foreach($context_nodes as $context_node){
    $interesting_nodes[] = $xpath->query('table[2]/tr[td[2]]', $context_node);
}

The resulting $interesting_nodes array contains empty DOMNodeLists.

The $context_nodes DOMNodeList contains valid data. The html content of each $context_node looks like this:

<td>
    <table></table>
    <table>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</td>

I tried the following simplified $intesting_nodes query to match any table:

$intesting_nodes[] = $xpath->query('table', $context_node);

But that still produces the same empty DOMNodeLists.

And now the interesting part

When I try an $interesting_nodes query like so:

$interesting_nodes[] = $xpath->query('*[2]/*[*[2]]', $context_node);

Then everything works perfectly; but if I replace any “*” with the corresponding “table”, “tr”, or “td” tags, then the query breaks once again.

Does anyone else have experience with this behavior and relative xpath queries in php?

I would very much like to be able to use a more exact query, and would prefer to be able to keep the query relative like it is rather than making it absolute.

  • 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-19T01:42:13+00:00Added an answer on May 19, 2026 at 1:42 am

    I figured it out. 🙂

    The php xpath implementation does not know what to do with table internal nodes (ie: tr, td) if the master table tags are not present.

    My outer td tags were causing unexpected results from the xpath query.

    Modified the $context_nodes query to:

    $context_nodes = $xpath->query('//table[@id="id1"]/tr[position()>1]/td');
    

    And we’re good.

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

Sidebar

Related Questions

I have created a Python module that creates and populates several SQLite tables. Now,
I have a long running insert transaction that inserts data into several related tables.
I have a complex UI with several nested tables, a repeater control and several
I have several tables in a database. One table (tbl_transactions) has thousands of orphaned
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
This issue is driving me mad. I have several tables defined, and CRUD stored
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have an HTML table with several columns and I need to implement a
I have a SQL Mobile database with one table. It has several columns with
While trying to use LINQ to SQL I encountered several problems. I have table

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.