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

  • Home
  • SEARCH
  • 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 8711397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:52:01+00:00 2026-06-13T04:52:01+00:00

I just spent quite some time trying to remove an attribute with namespace from

  • 0

I just spent quite some time trying to remove an attribute with namespace from DOMNode and it is simply not working at all.

The xml is generated from database and looks like this:

<dictionary>

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <table>answers</table>
  <entity>Answer</entity>
</row>

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <table>file_trans</table>
  <entity>FileTrans</entity>
</row>

...
</dictionary>

The attribute name I am trying to remove is obviously “xmlns:xsi”. The postgres db adds it automatically and I was unable to remove it there, so I am trying to do the job with php.

I load the xml as DOMDocument and then I do a foreach cycle through all the row elements:

$xml = new DOMDocument();
$xml->loadXML($tablesInXml['xmlelement'], LIBXML_NOBLANKS);

foreach($xml->documentElement->childNodes as $row) {
  $row->removeAttribute('xsi');  // not working
  $row->removeAttribute('xmlns:xsi'); // not working
...

I even tried to scout the DOMNode property attributes and it contains no attributes at all and shows length of 0.

Is this a bug in php 5.3? Does anybody know what else can I do?

Thanks for any answer

  • 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-13T04:52:03+00:00Added an answer on June 13, 2026 at 4:52 am

    You cannot do this in a trivial way with DOMDocument. Those are not really "attributes" (they are not visible as attributes in the DOM and are not part of the XML Infoset). They are rather namespace declarations and have no existence outside of the xml serialization. Most importantly, they are not represented in the DOM in any way.

    libxml2 (the underlying xml library for DOMDocument) keeps track of these "namespace nodes" internally but does not expose a public interface to them. Thus if you clone or import a node, the xml namespace declaration will follow even though you can’t see it.

    It seems there was a bug in older versions of PHP where you could delete these nodes using removeAttributeNS, but this has been fixed. See the comment on the PHP documentation for this method.

    My opinion: you should not try to get rid of these nodes. It’s not worth your time and it doesn’t hurt anything to leave them.

    However, if you really want to get rid of them, you have to use another approach. One way you can do it is with manually deep-copying the entire DOM tree to a new DOM document. If you use createElementNS and setAttributeNS when copying (instead of using importNode or cloneNode), the hidden namespace nodes won’t be created in your copy. I’m not going to write the code for you to do this because it will be tedious.

    This stackoverflow answer suggests an xslt solution. I’m not sure if it will work since XSLT 1.0 doesn’t expose these namespace declaration "nodes" either.

    UPDATE

    If you are ok with other things being done to your XML besides just removing the redundant XML nodes, you can try XML canonicalization. The purpose of Canonical XML is to ensure that the same XML infoset always generates the same XML output string. (This is useful for things like comparing XML files or creating checksum hashes.) But it also does things like never use self-closing tags.

    Try it and see:

    $d = new DOMDocument();
    $d->loadXML($xml, LIBXML_NOBLANKS);
    echo $d->C14N(true, false);
    

    Documentation:

    • DOMNode::C14N()
    • DOMNode::C14NFile()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just spent quite some time trying to resolve a virtual attribute issue in
I have spent quite some time going through similar questions here and have not
Have spend quite some time to learn Sencha touch, just like an evaluation about
I spent quite some time researching for a library that allows me to compare
I've spent quite some time searching to see if there was a similar issue.
I just spent way too long trying to diagnose why, in the following snippet
My Eclipse crashed, and a working set which I'd just spent an hour setting
I've spent the past two days just trying to enable the sending of email
I've spent a lot of time working in Django, and have grokked the framework
I have spent some time learning about socket programming in Java and have managed

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.