Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: Xml serialization with multiple xmlns per tag  (Read 4966 times)
Pedro R.
Posts: 3


« on: March 08, 2021, 01:37:23 pm »

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
  1. import xml
  2.  
  3. type TContent record
  4.    data string
  5. end record
  6.  
  7. type TDoc record
  8.    content TContent ATTRIBUTE(XMLNamespace="http://sgth.es/XSD/v1.0/content")
  9. end record
  10.  
  11. main
  12.    define
  13.        doc TDoc ATTRIBUTE(XMLName="doc", XMLNamespace="http://sgth.es/XSD/v1.0/doc"),
  14.        dom_doc xml.DomDocument,
  15.        node xml.DomNode
  16.  
  17.    let doc.content.data = "kkkkk"
  18.    let dom_doc = xml.DomDocument.Create()
  19.    let node = dom_doc.createDocumentFragment()
  20.  
  21.    call xml.Serializer.VariableToDom(doc, node)
  22.    call dom_doc.appendDocumentNode(node)
  23.  
  24.    display dom_doc.saveToString()
  25. end main

Produces this output:
Code
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <fjs1:doc xmlns:fjs1="http://sgth.es/XSD/v1.0/doc">
  3.    <fjs2:content xmlns:fjs2="http://sgth.es/XSD/v1.0/content">
  4.        <fjs2:data>kkkkk</fjs2:data>
  5.    </fjs2:content>
  6. </fjs1:doc>

Is it possible to get this output?:
Code
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <fjs1:doc xmlns:fjs1="http://sgth.es/XSD/v1.0/doc" xmlns:fjs2="http://sgth.es/XSD/v1.0/content">
  3.    <fjs2:content>
  4.        <fjs2:data>kkkkk</fjs2:data>
  5.    </fjs2:content>
  6. </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.
Posts: 7


« Reply #1 on: March 09, 2021, 08:32:29 am »

Hello Pedro,

try the following code:

Code
  1. import xml
  2.  
  3. constant NS_DOC     = "http://sgth.es/XSD/v1.0/doc",
  4.         NS_CONTENT = "http://sgth.es/XSD/v1.0/content"
  5.  
  6. type TContent record
  7.       data string
  8. end record
  9.  
  10. type TDoc record
  11.       content TContent ATTRIBUTE(XMLNamespace="http://sgth.es/XSD/v1.0/content")
  12. end record
  13.  
  14. main
  15.  define
  16.       doc TDoc ATTRIBUTE(XMLName="doc", XMLNamespace="http://sgth.es/XSD/v1.0/doc"),
  17.       dom_doc xml.DomDocument,
  18.       root_node,
  19.       node_content,
  20.       node_data xml.DomNode
  21.  
  22.   let doc.content.data = "kkkkk"
  23.  
  24.   let dom_doc = xml.DomDocument.CreateDocumentNS("fjs1", "doc", NS_DOC)
  25.   let root_node = dom_doc.getDocumentElement()
  26.   call dom_doc.declareNamespace(root_node, "fjs2", NS_CONTENT)
  27.  
  28.   let node_content = dom_doc.createElementNS("fjs2", "content", NS_CONTENT)
  29.   let node_data = dom_doc.createElementNS("fjs2", "data", NS_CONTENT)
  30.   call node_data.appendChild(dom_doc.createTextNode(doc.content.data))
  31.   call node_content.appendChild(node_data)
  32.   call root_node.appendChild(node_content)
  33.  
  34.   display dom_doc.saveToString()
  35. end main

This should generate the desired output.

Kind regards
Roland
Pedro R.
Posts: 3


« Reply #2 on: March 09, 2021, 09:01:42 am »

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.
Posts: 7


« Reply #3 on: March 09, 2021, 09:26:45 am »

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.
Posts: 1


« Reply #4 on: March 19, 2021, 04:40:59 pm »

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.
Posts: 1


« Reply #5 on: March 24, 2021, 12:24:44 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!
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines