Thursday, 6 November 2014

Adding custom namespace to xml element in javascript



Similar question, that did not provide actual answer for me How to add additional xmlns namespace attributes to XML in javascript .


I'm trying to add custom namespace to xml element, i was using pure jquery and it worked fine in chrome. I was using this jquery statement to get element string in jquery



$(schemaXml).find('Field').prop('outerHTML')


Unfortunately it does not work with IE.


I tried adding namespace before parsing and IE just toss it out (at least from element itself).


Here is the code sample



schemaXml = DOMParser().parseFromString(oldSchemaXmlStr, "text/xml")
console.log("Got XML from field:");
console.log(oldSchemaXmlStr);
console.log("Parsed it as:");
console.log((XMLSerializer()).serializeToString(schemaXml.getElementsByTagName('Field')[0]));
$(schemaXml).find('Field').attr('sntMB:Functionality', "Atachments");
console.log("Added sntMB:Functionality to XML:");
console.log((XMLSerializer()).serializeToString(schemaXml.getElementsByTagName('Field')[0]));


And this is the output in IE console



Got XML from field:
<Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Type="Text" Name="Title" ShowInNewForm="FALSE" ShowInFileDlg="FALSE" DisplayName="Title" Sealed="TRUE" SourceID="http://ift.tt/1k7JiLs" StaticName="Title" ColName="nvarchar7" Description="" xmlns:sntMB="SnT.MagicBox" />
Parsed it as:
<Field ColName="nvarchar7" StaticName="Title" SourceID="http://ift.tt/1k7JiLs" Sealed="TRUE" DisplayName="Title" ShowInFileDlg="FALSE" ShowInNewForm="FALSE" Name="Title" Type="Text" ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Description="" />
Added sntMB:Functionality to XML:
<Field ColName="nvarchar7" StaticName="Title" SourceID="http://ift.tt/1k7JiLs" Sealed="TRUE" DisplayName="Title" ShowInFileDlg="FALSE" ShowInNewForm="FALSE" Name="Title" Type="Text" ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" xmlns:NS1="" NS1:sntMB:Functionality="Attachments" Description="" />

No comments:

Post a Comment