xml namespace problem

BlackcatBlackcat Member Posts: 28
I have a problem adding a namespace (xmlns) to a tag in my XML DOM Document.

Let my try to explain. Before I add the namespace, all my tags are looking fine and dandy:

<tag1>...
<tag2>...
<tag3>...

But as soon as I add the namespace to tag1, all its child tags automatically get the empty namespace xmlns="" added to them, so now my XML DOM Document looks like this:

<tag1 xmlns="validURL">...
<tag2 xmlns="">...
<tag3 xmlns="">...

And i want it to look like this

<tag1 xmlns="validURL">...
<tag2 >...
<tag3 >...


Can anyone please help me ????
:?

Comments

  • ara3nara3n Member Posts: 9,256
    add it as a attribute element type to the xml port.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gfrgfr Member Posts: 10
    I have the same problem on NAV 3.70 which is not working with XML ports. I writed some code to generate an XML file but I'm facing the same issue. Did you have a solution for this?

    Thanks
  • ta5ta5 Member Posts: 1,164
    gfr wrote:
    I have the same problem on NAV 3.70 which is not working with XML ports. I writed some code to generate an XML file but I'm facing the same issue. Did you have a solution for this?

    Thanks

    Please post your code here, I'll try to help.
    Thomas
  • gfrgfr Member Posts: 10
    This is a piece of my code. I have problem when using the AddAttribute function (codeunit 6224). On all tags of same level it comes with xmlns="" as you described in your request.


    XMLDocOut.loadXML := '<AuditFile/>';
    XMLCurrNode := XMLDocOut.documentElement;
    XMLFirstNode := XMLCurrNode;

    DocNameSpace := '';
    WITH XMLDOMManagement DO BEGIN
    XMLDOMManagement.SetNormalCase;

    IF AddAttribute(XMLCurrNode,'xmlns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    EXIT;
    IF AddAttribute(XMLCurrNode,'xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance') >0 THEN
    EXIT;

    IF AddElement(XMLCurrNode,'Header','',DocNameSpace,XMLNewChild) > 0 THEN
    EXIT;

    //New child
    XMLCurrNode := XMLNewChild;

    IF AddAttribute(XMLCurrNode,'xmlns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    EXIT;

    IF XMLDOMManagement.AddElement(XMLCurrNode,'AuditFileVersion',VersionNo,DocNameSpace,XMLNewChild) > 0 THEN
    EXIT;

    IF AddElement(XMLCurrNode,'CompanyID',CompanyInfo."Registration Authority" + '/' + CompanyInfo."Registration No."
    ,DocNameSpace,XMLNewChild) > 0 THEN
    EXIT;
    IF AddElement(XMLCurrNode,'TaxRegistrationNumber',CompanyInfo."VAT Registration No.",DocNameSpace,XMLNewChild) > 0 THEN
    EXIT;
    IF AddElement(XMLCurrNode,'TaxAccountingBasis',TaxAccountBasisTxt,DocNameSpace,XMLNewChild) > 0 THEN
    EXIT;

    Thanks
  • ta5ta5 Member Posts: 1,164
    I had also problems using functions from cu6224.
    For the namespaces-problem I have created a function called "AddElement2"
    AddElement2(VAR XMLNode : Automation "'Microsoft XML, v6.0'.DOMDocument60";NodeName : Text[250];NodeText : Text[250];VAR CreatedXMLNode
    // adapted from Codeunit 6224
    
    NewChildNode := XMLNode.ownerDocument.createElement(NodeName);
    
    IF NodeText <> '' THEN
      NewChildNode.text := NodeText;
    
    XMLNode.appendChild(NewChildNode);
    CreatedXMLNode := NewChildNode;
    
    I call the AddElement2 function as follows
    AddElement2(CurrNode,'SOAP-ENV:Body','',NewChild);
    
    The Element "SOAP-ENV:Body" will be created this way. Don't forget to declare the prefixed namespace in advance using
    xmlMgt.AddAttribute(CurrNode,'xmlns:SOAP-ENV','http://schemas.xmlsoap.org/soap/envelope/');
    

    Hope this helps
    Thomas
  • gfrgfr Member Posts: 10
    Hi Thomas,

    Thanks for your help. I tried it but it doesn't help or I make something wrong. I always have xmlns="" in the file.

    <?xml version="1.0" encoding="windows-1252" ?>
    <AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <Header xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">
    <AuditFileVersion xmlns="">1.00_01</AuditFileVersion>
    <CompanyID xmlns="">/HRG/RCG </CompanyID>
    <TaxRegistrationNumber xmlns="">123456789</TaxRegistrationNumber>
    <TaxAccountingBasis xmlns="">Integrado - Contabilidade e Facturação</TaxAccountingBasis>
    <CompanyName xmlns="">CRONUS PT</CompanyName>
    <CompanyAddress xmlns="">
    <AddressDetail>Louisa street</AddressDetail>
    <City>Bxl</City>
    <PostalCode>B-1000</PostalCode>
    <Country>Belgium</Country>
    </CompanyAddress>
    <FiscalYear xmlns="">2009</FiscalYear>
    <StartDate xmlns="">2008-07-01</StartDate>
    <EndDate xmlns="">2009-06-30</EndDate>
    <CurrencyCode xmlns="">EUR</CurrencyCode>
    <DateCreated xmlns="">2008-11-04</DateCreated>
    <ProductID xmlns="">Microsoft Business Solutions-Navision/Microsoft</ProductID>
    <ProductVersion xmlns="">PT 3.70</ProductVersion>
    </Header>
    <MasterFiles xmlns="">


    Thanks.
  • ta5ta5 Member Posts: 1,164
    Strange. Can you post your new code here?
    Btw, this is a working sample, Function AddElement2 is described in my earlier post.
    CREATE(XMLDoc);
    xmlMgt.SetNormalCase;
    xmlProcessingInst:=XMLDoc.createProcessingInstruction('xml','version="1.0" encoding="utf-8"');
    CurrNode := XMLDoc.appendChild(xmlProcessingInst);
    CurrNode := XMLDoc.createElement('SOAP-ENV:Envelope');
    CurrNode := XMLDoc.appendChild(CurrNode);
    xmlMgt.AddAttribute(CurrNode,'xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
    xmlMgt.AddAttribute(CurrNode,'xmlns:ns001','http://www.abc.com');
    xmlMgt.AddAttribute(CurrNode,'xmlns:SOAP-ENV','http://schemas.xmlsoap.org/soap/envelope/');
    
    AddElement2(CurrNode,'SOAP-ENV:Body','',NewChild);
    
    CurrNode := NewChild;
    AddElement2(CurrNode,'ns001:Add','',NewChild);
    
    CurrNode := NewChild;
    AddElement2(CurrNode,'ns001:dbl1',FORMAT(Value1),NewChild);
    AddElement2(CurrNode,'ns001:dbl2',FORMAT(Value2),NewChild);
    IF SaveRequest THEN
      XMLDoc.save('c:\temp\webserviceRequest.xml');
    
    {
    <?xml version="1.0" encoding="utf-8" ?> 
    <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns001="http://www.abc.com" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
    <ns001:Add>
    <ns001:dbl1>7</ns001:dbl1> 
    <ns001:dbl2>8</ns001:dbl2> 
    </ns001:Add>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    Hope this helps
    Thomas
  • gfrgfr Member Posts: 10
    XMLDocOut.loadXML := '<AuditFile/>';

    XMLCurrNode := XMLDocOut.documentElement;
    XMLFirstNode := XMLCurrNode;

    DocNameSpace := '';
    WITH XMLDOMManagement DO BEGIN
    XMLDOMManagement.SetNormalCase;

    IF AddAttribute(XMLCurrNode,'xmlns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    EXIT;
    IF AddAttribute(XMLCurrNode,'xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance') >0 THEN
    EXIT;

    AddElement2(XMLCurrNode,'Header','',XMLNewChild);

    //New child
    XMLCurrNode := XMLNewChild;

    IF AddAttribute(XMLCurrNode,'xmlns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    EXIT;

    DocNameSpace := '';

    AddElement2(XMLCurrNode,'AuditFileVersion',VersionNo,XMLNewChild);

    AddElement2 is using the code you sent before.
    Thanks
  • ta5ta5 Member Posts: 1,164
    True, didn't kow that :mrgreen:

    Have a look at this:

    http://www.eggheadcafe.com/aspnet_answe ... 671481.asp

    It basically says that if the parent node has a namespace, then XMLDOM adds the xmlns="" attribute by default. You can test it if you remove all namespace attributes from the parents.

    You have 3 possiblilities:
    1) Check with the consumer of your xml-File whether the empty tag is a problem, imho it should not be a problem if the consumer works according to all xml-Rules
    2) Declare a kind of default name space and use it for those tags without name space
    IF xmlDOMManagement.AddAttribute(XMLCurrNode,'xmlns:myns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    ..snip
    
    // This is not needed anymore for the Header-Node anymore, it's using myns
    //IF xmlDOMManagement.AddAttribute(XMLCurrNode,'xmlns','urn:OECD:StandardAuditFile-Tax:PT_1.00_01') >0 THEN
    

    Good luck
    Thomas
  • gfrgfr Member Posts: 10
    Ok Thanks for your help.

    Regards,
    Giovanni
Sign In or Register to comment.