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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:15:37+00:00 2026-05-25T10:15:37+00:00

I have a malformed HTML that I can’t change. Running a XPath Query doesn’t

  • 0

I have a malformed HTML that I can’t change. Running a XPath Query doesn’t return the nodes at all:

$el = $xpath->query("//a[@class='product']/table"); // can get a tag with "//a[@class='product']"
print_r($el->length); // 0

Malformed HTML:

<a class="product" href="#">
    <table width="385" cellspacing="0" cellpadding="5" style="border:1px; border-bottom-color:#E2E2E2; border-bottom-style:solid;">
        <tr>
            <td width="55">
                <img src="http://foobar.com:8080/img/1212.jpg" height="50" width="50">
            </td>
        <td width="195">Cod.27731<br>Product Name</td>
            <td width="60" align="center"><a href="?pageContent=items&price=fab&prodcod=27731">Details</a></td>
            <td width="80" nowrap>
                <div style="color:#FF0000;"><strong>$ 35.23</strong></div>
        </td>
        </tr>
    </table>
</a>

I can get the a element but I can’t get its child (the table)…

  • 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-25T10:15:38+00:00Added an answer on May 25, 2026 at 10:15 am

    Since libxml will change the HTML to close the a element before the table, you have to query for the following-sibling table instead, e.g.

    $dom = new DOMDocument;
    $dom->loadHtml($html);
    $xpath = new DOMXpath($dom);
    $el = $xpath->query("//a[@class='product']/following-sibling::table");
    echo $dom->saveHtml($el->item(0));
    

    or traversing from the a element

    $dom = new DOMDocument;
    $dom->loadHtml($html);
    $xpath = new DOMXpath($dom);
    $table = $xpath->query("//a[@class='product']")->item(0)->nextSibling;
    echo $dom->saveHtml($table);
    

    Note that passing an argument to saveHTML requires at least PHP 5.3.6

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

Sidebar

Related Questions

I'm trying to build a Sanitize transformer that accepts potentially malformed HTML input with
I have a large malformed test HTML document which I need to get the
I have a user form with a textarea that allows users to submit html
Possible Duplicate: Decode HTML entities in Python string? I have a malformed string in
well i have the following problem. the html i have is malformed and i
I am constructing a large HTML document from fragments supplied by users that have
I have following code in html: <div> <div style=float:left;margin:0.5em> <span class=title>Label1</span><br/> <input type=text name=name1
I have a site that is calling a great deal of HTML from a
I have to deal with malformed Html and Html tags inside Html attributes: <p
Some malformed and incomplete HTML pages have no charset information assigned to them, and

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.