DA-- DA-- - 3 months ago 53
JSON Question

How to use XPath/XSLT fn:json-to-xml

I need to convert a JSON string to a XML string. The tags do contain attributes. From the answer in this topic I started using XSLT.

There exists a function fn:json-to-xml. I understand that it should convert the JSON into an XML without attributes(which I format using XSLT).

How do I use this function?

Because it's implemented in XSLT I would guess in the .xsl file, but I can't find any examples.

Many thanks in advance!

Answer

Here is a simple example taken from the XSLT 3.0 spec https://www.w3.org/TR/xslt-30/#func-json-to-xml, the sample input can be any string in the JSON format, below I use an XML document containing a data element with JSON:

<root>
    <data>{
        "desc"    : "Distances between several cities, in kilometers.",
        "updated" : "2014-02-04T18:50:45",
        "uptodate": true,
        "author"  : null,
        "cities"  : {
        "Brussels": [
        {"to": "London",    "distance": 322},
        {"to": "Paris",     "distance": 265},
        {"to": "Amsterdam", "distance": 173}
        ],
        "London": [
        {"to": "Brussels",  "distance": 322},
        {"to": "Paris",     "distance": 344},
        {"to": "Amsterdam", "distance": 358}
        ],
        "Paris": [
        {"to": "Brussels",  "distance": 265},
        {"to": "London",    "distance": 344},
        {"to": "Amsterdam", "distance": 431}
        ],
        "Amsterdam": [
        {"to": "Brussels",  "distance": 173},
        {"to": "London",    "distance": 358},
        {"to": "Paris",     "distance": 431}
        ]
        }
        }</data>
</root>

The template for the data element then simply calls json-to-xml(.) and the whole stylesheet to demonstrate the result outputs the returned XML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:output indent="yes"/>

    <xsl:template match="data">
        <xsl:copy-of select="json-to-xml(.)"/>
    </xsl:template>

</xsl:stylesheet>

The output with Saxon 9.7 HE is

        <map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="desc">Distances between several cities, in kilometers.</string>
   <string key="updated">2014-02-04T18:50:45</string>
   <boolean key="uptodate">true</boolean>
   <null key="author"/>
   <map key="cities">
      <array key="Brussels">
         <map>
            <string key="to">London</string>
            <number key="distance">322</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">265</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">173</number>
         </map>
      </array>
      <array key="London">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">322</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">344</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">358</number>
         </map>
      </array>
      <array key="Paris">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">265</number>
         </map>
         <map>
            <string key="to">London</string>
            <number key="distance">344</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">431</number>
         </map>
      </array>
      <array key="Amsterdam">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">173</number>
         </map>
         <map>
            <string key="to">London</string>
            <number key="distance">358</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">431</number>
         </map>
      </array>
   </map>
</map>

So as with any other function taking an input as as xs:string you can of course pass in any string value you have in your XSLT or XPath code or you can pass in a node which is then atomized to a string first.

Comments