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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:55:23+00:00 2026-05-19T04:55:23+00:00

I’m attempting to use SimpleXML’s addChild method of the SimpleXMLElement (actually a SimpleXMLIterator which

  • 0

I’m attempting to use SimpleXML’s addChild method of the SimpleXMLElement (actually a SimpleXMLIterator which is a subclass of SimpleXMLElement) to add child elements.

My problem is that the source document contains a mixture of elements with namespaces and without. Here’s a simple (no pun intended) example:

<?xml version="1.0" encoding="UTF-8"?>
  <ns1:a xmlns:ns1="http://www.abc.com">
</ns1:a>

The PHP code is:

$it = new SimpleXMLIterator ('./test.xml', 0, true);
$it->addChild('d', 'another!'); // adds new child element to parent's NS
$it->addChild('c', 'no namespace for me!', ''); // puts xmlns="" every time :(

//output xml in response:
header('Content-Type: text/xml');

echo $it->saveXML();

The problem – as the comment states – is that if I want to place a child element with no namespace inside of the parent element with a namespace, I get an empty XML namespace attribute every time (output of above PHP code):

<?xml version="1.0" encoding="UTF-8"?>
  <ns1:a xmlns:ns1="http://www.abc.com">
  <ns1:d>another!</ns1:d>
  <c xmlns="">no namespace for me!</c>
</ns1:a>

While both web browsers as well as XML parsers (e.g. Xerces) don’t seem to mind this superfluous markup, I find it slightly annoying that I can’t seem to tell it to stop doing this.

Anyone have a solution or am I over reacting?

:}

  • 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-19T04:55:24+00:00Added an answer on May 19, 2026 at 4:55 am

    For SimpleXML c needs a namespace. If you specify one, it gets an xmlns attribute because what you specified has not been declared before. If you don’t specify a namespace for c, it inherits the namespace from the parent node. The only option here is ns1. (This happens to d.)

    To prevent inhertance of the parent namespace and omit empty xmlns you’d need an namespace like xmlns="http://example.com" at the parent. Then $it->addChild('c', 'no ns', 'http://example.com') gives you <c>no ns</c>.

    However you cannot inject additional namespaces e.g. with addAttribute. You have to manipulate the input file before it’s parsed by SimpleXML. To me this seems even more ugly than removing all empty xmlns attributes from the output.

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

Sidebar

Related Questions

No related questions found

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.