Question

In a server-side implementation of javascript (Spidermonkey), I'm trying to copy a chunk of XML returned from a webservice and remove the namespace attribute.

I'd simply like the copied XML to have the namespace references removed to keep the new xml structure cleaner, as my new XML is simpler and has no need for the namespace.

I've tried setNamespace(), setNamespace(new Namespace()), removeNamespace(), etc - but I cannot keep the xmlns attribute from appearing in the copied XML.

Here is an example:

var ns   = new Namespace('', 'urn://foo.com/bar');  

var xml = <root>
            <addrInfo>
              <custAddr xmlns="urn://foo.com/bar">
                <addr>123 Main</addr>
                <city>My Town</city>
                <stateProv>XX</stateProv>
                <postalCode>12345</postalCode>
              </custAddr>
            </addrInfo>
         </root>;

xml.addrInfo.ns::custAddr.setNamespace(new Namespace());

var newXml = xml.addrInfo.custAddr;

console.log('namespace: ' + newXml.namespace());    

console.log(newXml);

If you'll notice, once I call setNamespace(), I am then able to reference the custAddr node with no namespace reference.. so it acts like it was removed.. even calling namespace() on the newXml variable produces an empty string.. but what I don't understand is why the copied XML from custAddr into newXml still has the xmlns attribute in the xml:

namespace: 
<custAddr xmlns="urn://foo.com/bar">
  <addr>123 Main</addr>
  <city>My Town</city>
  <stateProv>XX</stateProv>
  <postalCode>12345</postalCode>
</custAddr>

I just want a clean custAddr element with no xmlns attribute.

I realize I could do some string manipulation/regex/etc and get rid of the attribute that way, but if there's a proper e4x way of removing it, I'd like to know how.

Any help would be appreciated.

Was it helpful?

Solution

I finally gave up and just did it with regex. It seems like all E4X implementations are quirky at best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top