Producing XML with Namespaces
Producing an XML with (multiple) namespaces in AMT is possible using the Add and AddNamespaceDeclaration functions, this page shows an example of how to correctly use these functions.
Code example
begin_definitions
const
digital_signature_schema : stringvalue'http://www.w3.org/2000/09/xmldsig#'
invoice_schema : string value 'http://www.example.org/invoices/2009/v3.2/Invoice'
media : string value 'http://media.web/xml'
var
xml_file : xml
xml_root : xmlnode
xml_node : xmlnode
path : string
Booleans
end_definitions
routine main
begin_routine
xml_file.SETXMLDECLARATION ('1.0', 'utf-8')
xml_root := xml_file.Add ('fe:Invoice', invoice_schema)
xml_root.AddNamespaceDeclaration ('ds', digital_signature_schema)
xml_root.AddNamespaceDeclaration ('mm', media)
// the specified namespace matches the prefix and it has been declared on the parent node, => declaration is not repeated
xml_node := xml_root.Add ('ds:TestNode1', digital_signature_schema)
xml_node.value := 'Dummy1'
// the specified namespace matches the prefix and it has been declared on the parent node, => declaration is not repeated
xml_node := xml_root.Add ('fe:TestNode2', invoice_schema)
xml_node.value := 'Dummy2'
// the specified namespace does not match the prefix, => it is redeclared with the new prefix
xml_node := xml_root.Add ('fe:TestNode3', digital_signature_schema)
xml_node.value := 'Dummy3'
// the specified namespace is not known in the (parent) node, => it does not fit, no node is generated
xml_node := xml_root.Add ('mm:TestNode4', 'some_other_non_matching_namespace')
xml_node.value := 'Dummy4'
// the specified namespace is known in the (parent) node but the prefix is not, => it is redeclared with the new prefix
xml_node := xml_root.Add ('rr:TestNode5', media)
xml_node.value := 'Dummy5'
// namespace media is still linked to prefix mm on this level
xml_node := xml_root.Add ('mm:TestNode6', media)
xml_node.value := 'Dummy6'
// the redeclaration of fe only applies to the new child node, here fe is still linked to invoice_schema
xml_node := xml_root.Add ('fe:TestNode7', invoice_schema)
xml_node.value := 'Dummy7'
path := 'C:\AMT\Files\Extracts\Invoice.xml'
xml_file.save (path)
sme(getlastresult)
end_routine
const
digital_signature_schema : stringvalue'http://www.w3.org/2000/09/xmldsig#'
invoice_schema : string value 'http://www.example.org/invoices/2009/v3.2/Invoice'
media : string value 'http://media.web/xml'
var
xml_file : xml
xml_root : xmlnode
xml_node : xmlnode
path : string
Booleans
end_definitions
routine main
begin_routine
xml_file.SETXMLDECLARATION ('1.0', 'utf-8')
xml_root := xml_file.Add ('fe:Invoice', invoice_schema)
xml_root.AddNamespaceDeclaration ('ds', digital_signature_schema)
xml_root.AddNamespaceDeclaration ('mm', media)
// the specified namespace matches the prefix and it has been declared on the parent node, => declaration is not repeated
xml_node := xml_root.Add ('ds:TestNode1', digital_signature_schema)
xml_node.value := 'Dummy1'
// the specified namespace matches the prefix and it has been declared on the parent node, => declaration is not repeated
xml_node := xml_root.Add ('fe:TestNode2', invoice_schema)
xml_node.value := 'Dummy2'
// the specified namespace does not match the prefix, => it is redeclared with the new prefix
xml_node := xml_root.Add ('fe:TestNode3', digital_signature_schema)
xml_node.value := 'Dummy3'
// the specified namespace is not known in the (parent) node, => it does not fit, no node is generated
xml_node := xml_root.Add ('mm:TestNode4', 'some_other_non_matching_namespace')
xml_node.value := 'Dummy4'
// the specified namespace is known in the (parent) node but the prefix is not, => it is redeclared with the new prefix
xml_node := xml_root.Add ('rr:TestNode5', media)
xml_node.value := 'Dummy5'
// namespace media is still linked to prefix mm on this level
xml_node := xml_root.Add ('mm:TestNode6', media)
xml_node.value := 'Dummy6'
// the redeclaration of fe only applies to the new child node, here fe is still linked to invoice_schema
xml_node := xml_root.Add ('fe:TestNode7', invoice_schema)
xml_node.value := 'Dummy7'
path := 'C:\AMT\Files\Extracts\Invoice.xml'
xml_file.save (path)
sme(getlastresult)
end_routine
This results in the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<fe:Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:mm="http://media.web/xml" xmlns:fe="http://www.example.org/invoices/2009/v3.2/Invoice">
<ds:TestNode1>Dummy1</ds:TestNode1>
<fe:TestNode2>Dummy2</fe:TestNode2>
<fe:TestNode3 xmlns:fe="http://www.w3.org/2000/09/xmldsig#">Dummy3</fe:TestNode3>
<rr:TestNode5 xmlns:rr="http://media.web/xml">Dummy5</rr:TestNode5>
<mm:TestNode6>Dummy6</mm:TestNode6>
<fe:TestNode7>Dummy7</fe:TestNode7>
</fe:Invoice>
<fe:Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:mm="http://media.web/xml" xmlns:fe="http://www.example.org/invoices/2009/v3.2/Invoice">
<ds:TestNode1>Dummy1</ds:TestNode1>
<fe:TestNode2>Dummy2</fe:TestNode2>
<fe:TestNode3 xmlns:fe="http://www.w3.org/2000/09/xmldsig#">Dummy3</fe:TestNode3>
<rr:TestNode5 xmlns:rr="http://media.web/xml">Dummy5</rr:TestNode5>
<mm:TestNode6>Dummy6</mm:TestNode6>
<fe:TestNode7>Dummy7</fe:TestNode7>
</fe:Invoice>