Xml serialization with multiple xmlns per tag

Started by Pedro R., March 08, 2021, 01:37:23 PM

Previous topic - Next topic

Pedro R.

Hi !

We've been developing with fjs BDL for more than twenty years, but I think this is our first time here, so hello to everybody !

This program:

Code (genero) Select
import xml

type TContent record
    data string
end record

type TDoc record
    content TContent ATTRIBUTE(XMLNamespace="http://sgth.es/XSD/v1.0/content")
end record

main
    define
        doc TDoc ATTRIBUTE(XMLName="doc", XMLNamespace="http://sgth.es/XSD/v1.0/doc"),
        dom_doc xml.DomDocument,
        node xml.DomNode

    let doc.content.data = "kkkkk"
    let dom_doc = xml.DomDocument.Create()
    let node = dom_doc.createDocumentFragment()

    call xml.Serializer.VariableToDom(doc, node)
    call dom_doc.appendDocumentNode(node)

    display dom_doc.saveToString()
end main


Produces this output:
Code (xml) Select
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fjs1:doc xmlns:fjs1="http://sgth.es/XSD/v1.0/doc">
    <fjs2:content xmlns:fjs2="http://sgth.es/XSD/v1.0/content">
        <fjs2:data>kkkkk</fjs2:data>
    </fjs2:content>
</fjs1:doc>


Is it possible to get this output?:
Code (xml) Select
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fjs1:doc xmlns:fjs1="http://sgth.es/XSD/v1.0/doc" xmlns:fjs2="http://sgth.es/XSD/v1.0/content">
    <fjs2:content>
        <fjs2:data>kkkkk</fjs2:data>
    </fjs2:content>
</fjs1:doc>


We need to build an xml that must conforms to a very rigid schema, and this multi namespace declaration in root tag is mandatory.

Thanks !

Roland W.

Hello Pedro,

try the following code:

Code (genero) Select

import xml

constant NS_DOC     = "http://sgth.es/XSD/v1.0/doc",
         NS_CONTENT = "http://sgth.es/XSD/v1.0/content"

type TContent record
       data string
end record

type TDoc record
       content TContent ATTRIBUTE(XMLNamespace="http://sgth.es/XSD/v1.0/content")
end record

main
  define
       doc TDoc ATTRIBUTE(XMLName="doc", XMLNamespace="http://sgth.es/XSD/v1.0/doc"),
       dom_doc xml.DomDocument,
       root_node,
       node_content,
       node_data xml.DomNode

   let doc.content.data = "kkkkk"

   let dom_doc = xml.DomDocument.CreateDocumentNS("fjs1", "doc", NS_DOC)
   let root_node = dom_doc.getDocumentElement()
   call dom_doc.declareNamespace(root_node, "fjs2", NS_CONTENT)

   let node_content = dom_doc.createElementNS("fjs2", "content", NS_CONTENT)
   let node_data = dom_doc.createElementNS("fjs2", "data", NS_CONTENT)
   call node_data.appendChild(dom_doc.createTextNode(doc.content.data))
   call node_content.appendChild(node_data)
   call root_node.appendChild(node_content)

   display dom_doc.saveToString()
end main


This should generate the desired output.

Kind regards
Roland

Pedro R.

Thanks Roland for your sugestion.

Seems to me that it's the call to declareNamespace what does the magic. This way is a bit more "manual" than the serialized version, but it'll do.

Thanks again !

Roland W.

Hello Pedro,

I've also had problems to figure out how to deal with namespaces when currently trying to create XML files for the german XRechnung which is used for electronic invoicing. Maybe there's an easier way and someone has a better approach.

Kind regards
Roland

Rick M.

What about hiring a software developer? Some software developers earn a certificate while some work as full-time employees while working on an independent contract basis.

Nikola T.

Quote from:  . on March 19, 2021, 04:40:59 PM
What about hiring a software developer from https://mlsdev.com? Some software developers earn a certificate while some work as full-time employees while working on an independent contract basis.
Seems an interesting idea!

John S.