Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove XML namespaces using Javascript?

I am finding that, for my purposes, XML namespaces are simply causing much headache and are completely unnecessary. (For example, how they complicate xpath.)

Is there a simple way to remove namespaces entirely from an XML document?

(There is a related question, but it deals with removing namespace prefixes on tags, rather than namespace declarations from the document root: "Easy way to drop XML namespaces with javascript".)

Edit: Samples and more detail below:

XML:

<?xml version="1.0" ?>
<main xmlns="example.com">
  <primary>
    <enabled>true</enabled>
  </primary>
  <secondary>
    <enabled>false</enabled>
  </secondary>
</main>

JavaScript:

function useHttpResponse()
{
    if (http.readyState == 4)
    {
        if(http.status == 200)
        {
            var xml = http.responseXML;
            var evalue = getXMLValueByPath('/main/secondary/enabled', xml);
            alert(evalue);
        }
    }
}

function getXMLValueByPath(nodepath, xml)
{
    var result = xml.evaluate(nodepath, xml, null, XPathResult.STRING_TYPE, null).stringValue;
    return result;
}

The sample XML is just like the actual one I am working with, albeit much shorter. Notice that there are no prefixes on the tags for the namespace. I assume this is the null or default namespace.

The JavaScript is a snippet from my ajax functions. If I remove the xmlns="example.com" portion from the main tag, I am able to successfully get the value. As long as any namespace is present, the value becomes undefined.

Edit 2:

It may be worth mentioning that none of the declared namespaces are actually used in the XML tags (like the sample above). In the actual XML file I am working with, three namespaces are declared, but no tags are prefixed with a namespace reference. Thus, perhaps the question should be re-titled, "How to remove unused XML namespaces using Javascript?" I do not see the reason to retain a namespace if it is 1) never used and 2) complicating an otherwise simple path to a node using xpath.

like image 940
JYelton Avatar asked Dec 22 '10 00:12

JYelton


People also ask

How do I get rid of namespaces?

Remove namespaces using the Namespace Editor Determine the namespace for a node by selecting any object/node that uses the namespace. In the Namespace Editor, select the namespace you want to remove. Click Delete.

How the naming conflict in XML is resolved?

Solving the Name Conflict Using a Prefix Name conflicts in XML can easily be avoided using a name prefix. In the example above, there will be no conflict because the two <table> elements have different names.

Why are XML namespaces urls?

NET, namespaces in XML documents are designed to help prevent and resolve name collisions, especially if XML documents are from different organizations and/or domains of knowledge.

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.


2 Answers

This should remove any namespace declaration you find:

var xml = http.responseXML.replace(/<([a-zA-Z0-9 ]+)(?:xml)ns=\".*\"(.*)>/g, "<$1$2>");
like image 129
Andre Avatar answered Sep 21 '22 12:09

Andre


Inorder to replace all the xmlns attributes from an XML javascript string

you can try the following regex

xmlns=\"(.*?)\"

NB: This regex can be used to replace any attributes

var str = `<?xml version="1.0" ?>
<main xmlns="example.com">
  <primary>
    <enabled>true</enabled>
  </primary>
  <secondary>
    <enabled>false</enabled>
  </secondary>
</main>`;

str = str.replace(/xmlns=\"(.*?)\"/g, '');

console.log(str)
like image 31
SAMUEL Avatar answered Sep 20 '22 12:09

SAMUEL