Create XML DOM Element while keeping case sensitivity Create XML DOM Element while keeping case sensitivity xml xml

Create XML DOM Element while keeping case sensitivity


You need to use createElementNS()/setAttributeNS() and provide the namespace, not only the alias/prefix. The example uses urn:v as namespace.

var xmlns_v = "urn:v";var newCustprop = document.createElementNS(xmlns_v, "v:custProps");var newcp = document.createElementNS(xmlns_v, "v:cp");newcp.setAttributeNS(xmlns_v, "v:nameU", "Cost");newCustprop.appendChild(newcp);var xml = (new XMLSerializer).serializeToString(newCustprop);

xml:

<v:custProps xmlns:v="urn:v"><v:cp v:nameU="Cost"/></v:custProps>


It's not recommended to use document.createElement for qualified names. See if the document.createElementNS can better serve your purposes.