AL icon indicating copy to clipboard operation
AL copied to clipboard

Namespace Creation Error

Open ABaludin opened this issue 8 years ago • 3 comments

Hello! I need to create next XML document :

<?xml version="1.0" encoding="UTF-8"?> <signed xmlns="urn:x-obml:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CMSDETACHED"></signed>

So I create next code:

   `xmlDoc := XmlDocument.Create;
    xmlDec := xmlDeclaration.Create('1.0','UTF-8','');
    xmlDoc.SetDeclaration(xmlDec);

    XmlElm := XmlElement.Create('signed');
    xmlAtt := XmlAttribute.Create('xmlns', 'urn:x-obml:1.0');
    xmlElm.Add(xmlAtt);
    xmlAtt := XmlAttribute.Create('xsi', 'xmlns', 'http://www.w3.org/2001/XMLSchema-instance');
    xmlElm.Add(xmlAtt);
    xmlAtt := XmlAttribute.Create('type', 'xsi', 'CMSDETACHED');
    xmlElm.Add(xmlAtt);

    xmlDoc.Add(xmlElm);
    xmlDoc.WriteTo(XMLText);

    exit(XMLText);`

But on xmlDoc.WriteTo(XMLText); row i've got next exception:

The following exception was encountered when processing XML data: 'The prefix '' cannot be redefined from '' to 'urn:x-obml:1.0' within the same start element tag.' at line 0 and position 0.

What's the problem?

Changing to xmlAtt := XmlAttribute.CreateNamespaceDeclaration('xmlns', 'urn:x-obml:1.0');

goes to exception

Exception of type 'Microsoft.Dynamics.Nav.Types.Exceptions.NavNCLArgumentException' was thrown.

and docs told that

If the prefix is xmlns, then this parameter must be http://www.w3.org/2000/xmlns/; otherwise an exception is thrown.

but i have to have xmlns="urn:x-obml:1.0" because of it's API requirement.

ABaludin avatar Apr 02 '18 10:04 ABaludin

Hi @ABaludin I was too fast to declare it a bug, it is possible as is.

procedure GenerateXML() xText: Text
var
    xmlDoc : XmlDocument;
    xmlElm : XmlElement;
    uri : text;
begin
    xmlDoc := XmlDocument.Create;
    xmlDoc.SetDeclaration(xmlDeclaration.Create('1.0', 'UTF-8', ''));

    uri := 'http://http://www.w3.org/2001/XMLSchema-instance.com';
    XmlElm := XmlElement.Create('signed', 'urn:x-obml:1.0');
    xmlElm.Add(XmlAttribute.CreateNamespaceDeclaration('xsi', uri));
    xmlElm.Add(XmlAttribute.Create('type', uri, 'CMSDETACHED'));

    xmlDoc.Add(xmlElm);
    xmlDoc.WriteTo(xText);
end;

thpeder avatar Apr 03 '18 14:04 thpeder

Hi @thpeder

After an execution i've got next document:

<?xml version="1.0" encoding="utf-8"?> <signed xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance.com" xsi:type="CMSDETACHED" xmlns="urn:x-obml:1.0"> <signature xmlns=""></signature> </signed>

so can you help me with 2 questions:

  1. How to change attributes and namespaces order, because i need nex structure of : <signed xmlns="urn:x-obml:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CMSDETACHED">

  2. Internal element must not have xmlns="" namespace. How to avoid its creation?

thanks in advance!

ABaludin avatar Apr 17 '18 10:04 ABaludin

  1. I don't think it is possible to control the order of the attributes since the spec states that it is not significant.

  2. You need to pass in the same namespace as you did for the parent element. In your case 'urn:x-obml:1.0'

thpeder avatar Apr 17 '18 12:04 thpeder