XML parser namespace error

  • September 9, 2009
  • 1 Comment

Today I tried to create the following XML-structure in Ax:

1
2
<?xml version="1.0" encoding="UTF-8"?>
<personen xmlns:xdb="http://xmlns.oracle.com/xdb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Personeel.xsd">

I wrote the following to do so:

1
2
3
4
xmlDoc = WGKFunctions::createXMLDocumentWithDeclaration(#version, #encoding,  #standalone);
root =  xmlDoc.createElement(this.rootNode());
...
root.setAttribute("xsi:noNamespaceSchemaLocation",  "Personeel.xsd");

But I got the following result:

1
2
<?xml version="1.0" encoding="UTF-8"?>
<personen  xmlns:xdb="http://xmlns.oracle.com/xdb"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  noNamespaceSchemaLocation="Personeel.xsd">

As you see the ‘xsi:noNamespaceSchemaLocation‘ is replaced by ‘noNamespaceSchemaLocation‘.

After some research, I found the following link: http://blogs.msdn.com/emeadaxsupport/archive/2009/06/12/xml-parser-namespace-error.aspx

You are using Microsoft Dynamics AX to create an XML Document. The code you are using to create the XML file was created for the previous version of the product and is now upgraded to Microsoft Dynamics AX 4.0 SP1.
Using the method
setAttribute(‘namespaceprefix:attribname’,’attribvalue’);
does not add the “namespaceprefix” to the final XML node.

The attribute name in the final XML node will be added without namespaceprefix:
<node attribname="attribvalue" />
This is because the XML Framework was changed starting from Dynamics AX 4.0 and offers now a method that takes care about namespaces and prefixes itself.

So the solution is to use the following method instead:

setAttribute2(‘attribname’,’namespace’,’attribvalue’);
Please note that the “namespace” – not the “namespace prefix” has to be entered as second parameter. The XML Framework will resolve the namespace automatically to the correct namespace prefix and add it to the final XML node:

<node namespaceprefix:attribname=”attribvalue” />

So I modified my code to:

1
root.setAttribute2("noNamespaceSchemaLocation",  "http://www.w3.org/2001/XMLSchema-instance", "Personeel.xsd");

and the correct XML was generated.