# JSON Schemas with Semantic Annotations Supporting Data Translation
## 6 Translating JSON to XML and XML to JSON
### Translating JSON to XML  
This algorithm supports the translation of JSON instances to XML instances based on the XSLT 3.0 built-in function JSON-to-XML [52]. Since this built-in method does not directly return XML documents compliant with the generated XML Schema (Section 5), we rewrite the outcome by applying an additional algorithm to it. An example output of the XSLT 3.0 built-in function is shown in Listing 7. ```
1<?xmlversion=\"1.0\"encoding=\"UTF-8\">
2<mapxml=\"[[http://www.w3.org/2005/xpath-functions](http://www.w3.org/2005/xpath-functions)]([http://www.w3.org/2005/xpath-functions](http://www.w3.org/2005/xpath-functions))\">
3<mapkey=\"datavalues\">
4<stringkey=\"units\">Cell</string>
5<numberkey=\"OutSensTemp\">23</number>
6</map>
7</map>
```  
Listing 7: JSON-to-XML function result for Listing 1. The algorithm for rewriting the output of JSON-to-XML to a valid schema instance is shown in Listing 8. Since the goal of the translator is to be implemented in XSLT, the algorithm is oriented towards that logic. The JSON document is translated into XML format (as in the example presented in Listing 7) and stored in \"XMLnode\", as shown in line 1. This XML node will be the object of translation for this algorithm. Since it is an XSLT translator and the output method was chosen to be text for facilitating a dynamic translation, XML start tags and end tags are written on the go. Primarily, a root element start tag is written without any verification of the document and a builder function for the XML is called with the translated XML mapping as a parameter (lines 2 to 4). The createXML function is a recursive function that calls itself for every new node that has not been parsed. The algorithm checks if the current node is an array. If it is not an array, a verification is performed to ensure that the current node is a valid one for translation (line 9). One example of a disposable node that can be in this category is the node on line 2 of Listing 7; this node is declaring a namespace that will not be used, so it has no useful content for our translation; therefore, the algorithm ignores it and calls the function with the child node (line 19). As soon as it is a valid node (with a \"key\" attribute), the process of translating an XML node is started: a start tag is written using as name the value of the \"key\" attribute ofthe node (line 10): if the node has child elements, then the function is called once again for creating the child elements; if not, it is assumed that the node has a node value and its value is written as plain text after the start tag (line 11 to 16). After writing the node value or all child elements of the currentNode, the end tag is written (line 17). If the current node is an array, it can contain elements or complex type elements. The former is created from line 22 and the latter is from line 28. After finishing an XML element, if there are sibling elements to the currentNode, the function calls itself for its sibling node (line 36 to line 38). ```
1 LOAD txXMLode theresultfromJSON-to-XMLfunction
2 CREATEemptyXML
3 WRITErootelementstartag
4 CALLcreateXML(XML.