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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:09:25+00:00 2026-05-17T16:09:25+00:00

Say I have a source document like this: <element> <subelement xmlns:someprefix=mynamespace/> </element> The xmlns:someprefix

  • 0

Say I have a source document like this:

<element>
  <subelement xmlns:someprefix="mynamespace"/>
</element>

The xmlns:someprefix is obviously not needed here and doesn’t do anything since that prefix is not being used in that element (or in my case, anywhere in the document).

In PHP, after I’ve loaded this into a DOM tree with DOMDocument->loadXML(), I’d like to be able to detect that such a namespace declaration exists, and remove it.

I know that I can read it with hasAttribute() and even remove it with removeAttributeNS() (strangely) but only if I know its prefix. It doesn’t appear in DOMNode->attributes at all, as the thing I’m trying to find is not considered an attribute. I cannot see any way of detecting that it is there without knowing the prefix, other than serialising it back to an XML string and running a regex or something.

How can I do it? Any way to query which namespaces (ie xmlns:something) have been declared in an element?

  • 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-17T16:09:26+00:00Added an answer on May 17, 2026 at 4:09 pm

    How to detect:

    <?php
    $d = new DOMDocument();
    $d->loadXML('
    <element>
      <subelement xmlns:someprefix="http://mynamespace/asd">
      </subelement>
    </element>');
    $sxe = simplexml_import_dom($d);
    $namespaces = $sxe->getDocNamespaces(true);
    $x = new DOMXpath($d);
    foreach($namespaces as $prefix => $url){
            $count = $x->evaluate("count(//*[namespace-uri()='".$url."' or @*[namespace-uri()='".$url."']])");
            echo $prefix.' ( '.$url.' ): used '.$count.' times'.PHP_EOL;
    }
    

    How to remove: pfff, about your only option that I know of is to use xml_parse_into_struct() (as this is not libxml2 reliant afaik), and looping through the resulting array with XML Writer functions, skipping namespace declarations which are not used. Not a fun passtime, so I’ll leave the implementation up to you. Another option could be XSL according to this question, but I doubt it is of much use. My best effort seems to succeed, but moves ‘top-level’/rootnode namespaces to children, resulting in even more clutter.

    edit: this seems to work:

    Given XML (added some namespace clutter):

    <element xmlns:yetanotherprefix="http://mynamespace/yet">
      <subelement
            xmlns:someprefix="http://mynamespace/foo"
            xmlns:otherprefix="http://mynamespace/bar"
            foo="bar"
            yetanotherprefix:bax="foz">
            <otherprefix:bar>
                    <yetanotherprefix:element/>
                    <otherprefix:element/>
            </otherprefix:bar>
            <otherprefix:bar>
                    <yetanotherprefix:element/>
                    <otherprefix:element/>
            </otherprefix:bar>
            <yetanotherprefix:baz/>
      </subelement>
    

    With xsl (namespaces & not() clause based on previous $used array, so you’ll still need that afaik.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:yetanotherprefix="http://mynamespace/yet"
        xmlns:otherprefix="http://mynamespace/bar"> 
        <xsl:template match="/">
            <xsl:apply-templates select="/*"/>
        </xsl:template>
        <xsl:template match="*">
            <xsl:element name="{name(.)}">
                    <xsl:apply-templates select="./@*"/>
                    <xsl:copy-of select="namespace::*[not(name()='someprefix')]"/>
                    <xsl:apply-templates select="./node()"/>
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="@*">
            <xsl:copy/>
        </xsl:template>
    </xsl:stylesheet>
    

    Results in:

    <?xml version="1.0"?>
    <element xmlns:yetanotherprefix="http://mynamespace/yet">
      <subelement xmlns:otherprefix="http://mynamespace/bar" foo="bar" yetanotherprefix:bax="foz">
            <otherprefix:bar>
                    <yetanotherprefix:element/>
                    <otherprefix:element/>
            </otherprefix:bar>
            <otherprefix:bar>
                    <yetanotherprefix:element/>
                    <otherprefix:element/>
            </otherprefix:bar>
            <yetanotherprefix:baz/>
      </subelement>
    </element>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not even sure if it's possible but say I have some XML: <source>
Let's say I have a source file with many preprocessor directives. Is it possible
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have a class named Frog, it looks like: public class Frog {
Let's say I have this dir structure: /project1 /SomeFolder /obj /bin /project2 /obj /bin
I'd like some advice on building in known errors. Let's say I have a
Say I have my sources in my src/ tree (and possibly in my test/
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say you have an application divided into 3-tiers: GUI, business logic, and data access.

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.