1

I want to convert JSON to XML using XSLT. But not able to achieve the expected output. Below is the JSON request:

{
    "Store": [
        {
            "Book": "Cartoons",
            "ID": "ABC"
        }
    ]
}

The XSLT which I tried:

 <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"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:emp="http://www.semanticalllc.com/ns/employees#"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:j="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="xs math xd h emp"
    version="3.0"
    expand-text="yes">

<xsl:template match="/">
    <xsl:copy>
        <xsl:apply-templates select="json-to-xml(.)/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[@key]" xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
    <xsl:element name="{@key}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

But I'm getting empty Response.

nikki
  • 29
  • 5
  • I suppose your json needs to be in a xml-file or you pass the json as a root-parameter: See: https://stackoverflow.com/q/58113843/3710053 – Siebe Jongebloed Apr 29 '21 at 08:09
  • I have gone though the link and tried. But still not working for me. Is it possible to convert using XSLT 2.0 or 1.0 version? – nikki Apr 29 '21 at 10:14
  • everything is possible...."Where there's a will, there's a way." But for now I suggest to get thinks working with 3.0. So please update your xslt/xml to your latest try. – Siebe Jongebloed Apr 29 '21 at 10:35
  • I have updated the XSLT which I tried with the link you have given. Same issue. Not working. – nikki Apr 29 '21 at 10:47

2 Answers2

0

You need to pass the JSON as a parameter or read it from a file, the input to your XSLT is either XML or you can start with a named template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:param name="json" as="xs:string" expand-text="no">{
    "Store": [
        {
            "Book": "Cartoons",
            "ID": "ABC"
        }
    ]
}</xsl:param>

  <xsl:output indent="yes"/>

  <xsl:template match="/" name="xsl:initial-template">
      <xsl:sequence select="json-to-xml($json)"/>
  </xsl:template>
  
</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/93wniTJ

The result of the function for your sample is

<map xmlns="http://www.w3.org/2005/xpath-functions">
   <array key="Store">
      <map>
         <string key="Book">Cartoons</string>
         <string key="ID">ABC</string>
      </map>
   </array>
</map>

but of course you can run it through further templates to transform it to a different format.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

The pattern match="/" matches a document node (the root of an XML tree). It won't match your input if the input is JSON.

XSLT 3.0 isn't actually that good at processing JSON using template rules: it can be done, but it isn't very convenient. It's usually more convenient to use functions. You can supply the JSON input as the value of an xsl:param and process it in code from your xsl:initial-template; or if you're feeling more ambitious you could actually initiate the XSLT processing by invoking a public xsl:function that takes the JSON input as a parameter.

The traditional match="/" entry to a stylesheet only works for the traditional use of XSLT to process an XML input document.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Hi Michael, I'm finding difficulty in converting json to Jsonx without using Convert-query-params. Can you please help me how to use gatewayscript to convert Json to Jsonx or XML. I have gone through few IBM Forums and getting errors while using Gatewayscript. – nikki Apr 29 '21 at 13:34
  • That needs a separate question -- and probably a different person to answer it, since I know nothing of Json or Gatewayscript. – Michael Kay Apr 29 '21 at 14:14