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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:14:21+00:00 2026-05-23T06:14:21+00:00

Probably a stupid question but so far I can’t figure this out… I have

  • 0

Probably a stupid question but so far I can’t figure this out…

I have an XHTML document as a string. It’s in $temp So far so good. I want to do two things. I want to select all meta tags in the body (they are there because of their use in conjunction with microdata) and then delete them. After deleting the microdata properties that is.

    $xml=new DOMDocument();
    $xml->loadXML($temp);
    $xpath = new DOMXPath($xml);
    $attr = $xpath->query("//@itemscope|//@itemprop|//@itemtype|//@itemid|//@itemref");
    foreach ($attr as $entry)
        $entry->parentNode->removeAttribute($entry->nodeName);

That works. But I can’t manage to select any nodes with Xpath.

$xpath = new DOMXPath($xml); // thought I had to update this after changing the XML
echo $xpath->query("//body")->length; // => 0
echo $xml->getElementsByTagName("body")->length; // => 1

So Question No. 1: How do I select nodes with Xpath. Why doesn’t this work?

This works to get the node list though:

$node = $xml->getElementsByTagName("body")->item(0)->getElementsByTagName("meta");

I figured to remove the nodes I’d use this: (similar to removing the attributes above)

foreach ($node as $entry)
{
    $entry->parentNode->removeChild($entry);
}

But the nodes remain.

So there is Question No. 2: How to remove nodes from an XML file.

Specifically meta nodes anywhere in any body node.

Thanks.

UPDATE

Let me add an HTML test case:

$temp='<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
    <head>
        <meta charset="utf-8"/>
    </head>
    <body id="dok" itemscope="itemscope" itemtype="http://schema.org/WebPage" >
        <div><div><div><meta itemprop="dummy" content="something"/></div></div></div>
        <span><meta itemprop="dummy2" content="something2"/></span>
    </body>
</html>';

With the above the xPath trying to select the body give me a length of 0 and I can’t remove all meta tags from the body…

UPDATE

This works with the loadXML() method:

$xpath = new DOMXPath($xml);
$xpath->registerNamespace("x","http://www.w3.org/1999/xhtml");
echo $xpath->query("//x:body")->length;

SOLUTION without namespaces

It was about the xmlns="http://www.w3.org/1999/xhtml" namespace in the root html tag all along. //body selects any body tag that is NOT part of any namespace. Since we did specify a default namespace and body is part of that namespace //bodywon’t select it. I have no idea under what name to access the namespace already intrinsic to the XHTML without declaring it under a name but if we strip it off before creating the XML all is fine. After we’re done we can add it back in..

    $temp =  str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$temp);
    $xml=new DOMDocument();
    $xml->loadXML($temp);
    $xpath = new DOMXPath($xml);    
    $attr = $xpath->query("//@itemscope|//@itemprop|//@itemtype|//@itemid|//@itemref");
    foreach ($attr as $entry)
        $entry->parentNode->removeAttribute($entry->nodeName);
    $node = $xpath->query("//body//meta");
    foreach ($node as $entry)
    {
        $entry->parentNode->removeChild($entry);
    }   
    $temp=$xml->saveXML();
    $temp =  str_replace('<html','<html xmlns="http://www.w3.org/1999/xhtml"',$temp);

that way //body//meta works just as expected…

  • 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-23T06:14:22+00:00Added an answer on May 23, 2026 at 6:14 am

    This piece of code does the job for me:

    $temp='<!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
        <head>
            <meta charset="utf-8"/>
        </head>
        <body id="dok" itemscope="itemscope" itemtype="http://schema.org/WebPage" >
            <div><div><div><meta itemprop="dummy" content="something"/></div></div></div>
            <span><meta itemprop="dummy2" content="something2"/></span>
        </body>
    </html>';
    
    
    $xml=new DOMDocument();
    $xml->loadHtml($temp);
    $xpath = new DOMXPath($xml); // thought I had to update this after changing the XML
    $path = "//body//meta";
    
    echo $xpath->query($path)->length, "\n"; # 2
    
    foreach ($xpath->query($path) as $entry)
    {
        $entry->parentNode->removeChild($entry);
    }
    
    echo $xpath->query($path)->length, "\n"; # 0
    

    I think the two key points are:

    1. Load the document as HTML – I can not explain it properly but I think that XML is introducing namespaces and those should be reflected for the xpath. But I’m not familiar with namespace that well to really explain it. Loading as HTML however makes the queries to work “as expected” which is technically spoken not the right expection.
    2. //body//meta – The xpath must reflect that there can be more elements between the body and the meta elements. Hence the // between body and meta.

    Namespaces and XML

    Thanks to the explanation by Dimitri, I could now better understand the namespace issue I only smelled and could update the code to a loadXML() compatible version (only the modified lines):

    $xml->loadXml($temp);
    $xpath = new DOMXPath($xml);
    $xpath->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
    $path = "//xhtml:body//xhtml:meta";
    

    This loads the document as XML. Then it registers the namespace URI from the document with the name xhtml for the xpath object.

    The xpath query then was modified to reflect the namespace properly for the elements expressions.

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

Sidebar

Related Questions

This is probably a really stupid question but I can't figure this out. I
This is probably a stupid question, but I just can't figure out how to
This is probably a stupid question, but as far as I can tell there's
This is probably a very stupid math question, but i can't seem to figure
this is probably a stupid question but I can't seem to find the answer.
This may sounds like a stupid question but can't find anything on google, probably
I have probably stupid question but how Can I create Many-To-Many relationship with create
This is probably a stupid question, but I can't seem to do it. I
I have a simple and probably stupid question, but I can't really find any
This is probably a stupid question but I can not seem to either remember

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.