Problem with XML namespaces

EmerikEmerik Member Posts: 50
edited 2015-09-18 in Navision Attain
Hi,
I'm trying to create some XML files from purchase orders. These are to be validated by a workflow system. Thus, the header tag contains references to xml schemas, and the tags inside are appropriately declared with namespace prefixes.

The problem is, that the schemas are not available at creation of the xml file. The xml file is first validated inside the external workflow system. So, the namespace references give sense inside the external workflow system, but does not outside, as in Navision, where the file is created.

I am using the "'Microsoft XML, v.3.0'.DOMDocument" to create the xml file.

There are 2 scenarios (both are bad, the first worse than the second, though)
1. The tags are create with a blank namespace value. Result: The object compiles just fine, but refuses to create the file, exiting with the error:
This message is for C/AL programmers:

The call to member createNode failed. msxml3.dll returned the following message: Reference to undeclared namespace prefix: 'icc'.

2. The tags are created with the corresponding namespace reference. Result: The file is created, with all tags filled with namespace references. This means unnecessarily large files, since the reference are already in the header.

Isn't there any way to avoid having to repeat the namespace reference for every tag?

Code example
CREATE(XMLPurchaseOrder);

// Initialiser
  DocNameSpace := '';
  Prefix := 'icc';

  strHeader := '<?xml version="1.0" encoding="UTF-8" ?><PurchaseOrderInformation/>';

XMLPurchaseOrder.loadXML:=strHeader;
XMLCurrNode := XMLPurchaseOrder.documentElement; // >> POI


WITH XMLDOMManagement DO BEGIN
  AddAttribute(XMLCurrNode,'xmlns','urn:purchaseorderinformation:200511');
  AddAttribute(XMLCurrNode,'xmlns:util','http://www.proceedo.net/xslt/lib/Util');
  AddAttribute(XMLCurrNode,'xmlns:ifc','urn:components:financialaccount:200511');
  AddAttribute(XMLCurrNode,'xmlns:ibc','urn:components:business:200511');
  AddAttribute(XMLCurrNode,'xmlns:icc','urn:components:core:200511');

  AddElement(XMLCurrNode,'References','',DocNameSpace,XMLNewChild,Prefix);
    XMLCurrNode := XMLNewChild;  // >> POI/Refs
    AddElement(XMLCurrNode,'Reference',PurchaseOrder."No.",DocNameSpace,XMLNewChild,Prefix);
    AddAttribute(XMLNewChild,'scheme','urn:ibistic:order:reference:buyer');
    XMLCurrNode := XMLCurrNode.parentNode; // >> POI

  Prefix := '';
  AddElement(XMLCurrNode,'IssueDate',DateToText(PurchaseOrder."Order Date"),DocNameSpace,XMLNewChild,Prefix);
  AddElement(XMLCurrNode,'Type','Standard',DocNameSpace,XMLNewChild,Prefix);
  IF PurchaseOrder.Status = PurchaseOrder.Status::Open THEN
    txtStatus := 'Open'
  ELSE
    txtStatus := 'Closed';
  AddElement(XMLCurrNode,'Status',txtStatus,DocNameSpace,XMLNewChild,Prefix);
  AddElement(XMLCurrNode,'LastUpdated',DateTimeToText(CURRENTDATETIME),
  DocNameSpace,XMLNewChild,Prefix);
  Prefix := 'icc';
  AddElement(XMLCurrNode,'Amount','',DocNameSpace,XMLNewChild,Prefix);
  AddAttribute(XMLNewChild,'currency',PurchaseOrder."Currency Code");
    XMLCurrNode := XMLNewChild; // >> POI/Amount
    AddElement(XMLCurrNode,'AmountInclusiveTax',FORMAT(PurchaseOrder."Amount Including VAT"),DocNameSpace,XMLNewChild,Prefix);
    AddElement(XMLCurrNode,'AmountExclusiveTax',FORMAT(PurchaseOrder.Amount),DocNameSpace,XMLNewChild,Prefix);
    AddElement(XMLCurrNode,'TaxAmount',FORMAT(PurchaseOrder."Amount Including VAT"-PurchaseOrder.Amount),
    DocNameSpace,XMLNewChild,Prefix);
    XMLCurrNode := XMLCurrNode.parentNode; // >> POI
...
END;

XMLPurchaseOrder.save(ExportDir);

Comments

  • yldstryldstr Member Posts: 3
    Is the problem solved?

    I have problem like this.

    Can you hep me ?
  • Peter_KuiperPeter_Kuiper Member Posts: 19
    I had the same problem, and found the solution as follows:

    The trick is to declare the namespaces with a text constants (to make life easier...), and use the text constant instead of the prefix as last parameter in all Element-function calls where a prefix is used.
    ProcessingInstruction(XMLDoc,'1.0','utf-8','');
    
      AddElement(XMLDoc,aRootElement,'SSU_WHS_Interface_Data','','');
      XMLDoc.appendChild(aRootElement);
      addAttribute(XMLDoc,aRootElement,'xmlns', __Namespace );
      addAttribute(XMLDoc,aRootElement,'xmlns:ssu', __NamespaceSSU );
    
      AddElement(XMLDoc,aSessionInfo,'ssu:SessionInfo','', __NamespaceSSU);
    
Sign In or Register to comment.