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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:09:22+00:00 2026-06-12T12:09:22+00:00

We have the following code that lists the xpaths where $value is found. We

  • 0

We have the following code that lists the xpaths where $value is found.

We have detected for a given URL (see on picture) a non standard tag td1 which in addition doesn’t have a closing tag. Probably the site developers have put that there intentionally, as you see in the screen shot below.

This element creates problems identifying the corect XPath for nodes.

A broken Xpath example :

/html/body/div[2]/div[2]/table/tr[2]/td/table/tr[1]/td[2]/table/tr[2]/td[2]/table[3]/tr[2]/**td1**/td[2]/span/u[1]

(as you see td1 is identified and chained in the Xpath)

We think by removing this element it helps us to build the valid XPath we are after.

A valid example is

/html/body/div[2]/div[2]/table/tr[2]/td/table/tr[1]/td[2]/table/tr[2]/td[2]/table[3]/tr[2]/td[2]/span/u[1]

How can we remove prior loading in DOMXpath? Do you have some other approach?

We would like to remove all the invalid tags which may be other than td1, as h8, diw, etc…

private function extract($url, $value) {

        $dom = new DOMDocument();

        $file = 'content.txt';
        //$current = file_get_contents($url);
        $current = CurlTool::downloadFile($url, $file);
        //file_put_contents($file, $current);

        @$dom->loadHTMLFile($current);

        //use DOMXpath to navigate the html with the DOM
        $dom_xpath = new DOMXpath($dom);

        $elements = $dom_xpath->query("//*[text()[contains(., '" . $value . "')]]");
        var_dump($elements);
        if (!is_null($elements)) {

            foreach ($elements as $element) {
                var_dump($element);
                echo "\n1.[" . $element->nodeName . "]\n";

                $nodes = $element->childNodes;
                foreach ($nodes as $node) {
                    if( ($node->nodeValue != null) && ($node->nodeValue === $value) ) {
                        echo '2.' . $node->nodeValue . "\n";
                        $xpath = preg_replace("/\/text\(\)/", "", $node->getNodePath());
                        echo '3.' . $xpath . "\n";
                    }
                }
            }
        }
    }

enter image description here

  • 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-12T12:09:23+00:00Added an answer on June 12, 2026 at 12:09 pm

    You could use XPath to find the offending nodes and remove them, while promoting its children into its place in the DOM. Then your paths will be correct.

    $dom_xpath = new DOMXpath($dom);
    $results = $dom_xpath->query('//td1'); // (or any offending element)
    foreach ($results as $invalidNode)
    {
        $parentNode = $invalidNode->parentNode;
        while ($invalidNode->childNodes)
        {
            $firstChild = $invalidNode->firstChild;
            $parentNode->insertBefore($firstChild,$invalidNode);
        }
        $parentNode->removeChild($invalidNode);
    }
    

    EDIT:

    You could also build a list of offending elements by using a list of valid elements and negating it.

    // Build list manually from the HTML spec:
    // See: http://www.w3.org/TR/html5/section-index.html#elements-1
    $validTags = array();
    
    // Convert list to XPath:
    $validTagsStr = '';
    foreach ($validTags as $tag)
    {
        if ($validTagsStr)
        {   $validTagsStr .= ' or ';    }
        $validTagsStr .= 'self::'.$tag;
    }
    $results = $dom_xpath->query('//*[not('.$validTagsStr.')');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that just lists all MBean names found in platform
I have following code that does not work due to a being a value
i have the following code that lists all the jpeg in the phone's gallery.
i have the following code that adds data to unordered lists. The troublesome code
I have the following code that populates two drop down lists, it all works
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that gets the MAC address of an interface: static

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.