11

How would you convert JSON to XML?

Consider:

<sampleTag>
{
  "Order": {
    "InvestmentAccount": { "AccountNumber": "10" },
    "Parcel": {      
      "Limit": "0",
      "ExpiryDate": "1900-01-01T00:00:00",
      "Asset": [
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }]
    },

    "OrderDate": "2012-10-11T21:46:03.6489906+11:00",

  }
}
</sampleTag>

After transformation, the document is as follows:

<Order>
    <InvestmentAccount>
        <AccountNumber>10</AccountNumber>
    </InvestmentAccount>
    <Parcel>
        <Limit>0</Limit>
        <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
    </Parcel>
    <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>
6
  • 1
    I really doubt if you can do it easily with xslt which is better suited to xml processing. Consider extracting the json and converting using your programming language's libraries instead. Commented Oct 22, 2012 at 8:22
  • Why are you constrained to XSLT 1.0? What is your current XSLT processor? Commented Oct 22, 2012 at 12:07
  • This xslt I have to use in Datapower tool..It supports only xslt 1.0 Commented Oct 22, 2012 at 15:58
  • Datapower has partial support for xslt 2.0. I am not sure if it would be enough to use the json() function of FXSL, but you could try. Commented Oct 22, 2012 at 23:23
  • I tried using FXSL functions but unable to succeed. DP supports these exslt funtions() but I could anything from these functions. Commented Oct 23, 2012 at 5:50

8 Answers 8

9

My work on JSON parsing doesn't cover the full JSON grammar.

And the task of "translating" any JSON document to an XML document doesn't have a solution. There are JSON constructs, which cannot be translated to XML without defining additional conventions and introducing additional elements -- so the final XML structure isn't a true and natural representation of the original JSON object.

In XSLT 3.0 there is a function to parse any JSON object -- parse-json() -- to a map -- a new data type introduced in XSLT 3.0. Read about this here: http://www.w3.org/TR/xslt-30/#json

Sign up to request clarification or add additional context in comments.

Comments

9

Actually, it is not that hard. The way to approach it is to examine the syntax of jason, and view each production like it was a template. I was just about to write a solution, when I considered the possibility that the OP forgot to google for pre-existing solutions. I searched and lo and behold ....

UPDATE

Here is a JSon to XML converter. But it only works on a subset of json. Hopefully, the subset is broad enough for your particular needs. In particular the limitations are:

  1. The only simple type supported is string. No integer, boolean or null.
  2. Json object names must be valid xml element names.
  3. No escape codes permitted inside string values. This means that you cant transport values that include, for instance, the " character (without rolling your own encoding layer).

This XSLT 1.0 style-sheet...*

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
  xmlns:exsl="http://exslt.org/common"
  xmlns:so="http://stackoverflow.com/questions/13007280"
  exclude-result-prefixes="xsl xs json so exsl">
<xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" /> 

<xsl:variable name="quot" select="'&quot;'" />

<xsl:template match="/*">
  <xsl:variable name="t1">
    <xsl:call-template name="object">
     <xsl:with-param name="json-in" select="." />
    </xsl:call-template>
  </xsl:variable>
  <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
</xsl:template>

<xsl:template match="*" mode="copy-sans-namespace">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="copy-sans-namespace" />
  </xsl:element>
</xsl:template>

<xsl:template name="field">
  <!-- Input like: "Open": "25.15" bla -->
  <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
  <xsl:param name="json-in" />
  <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
  <xsl:variable name="remainder" select="substring-after($json-in,':')" />
  <xsl:call-template name="value">
    <xsl:with-param name="json-in" select="$remainder" />
    <xsl:with-param name="parent-ele" select="$field-name" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="fields">
  <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
  <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
  <xsl:param name="json-in" />
  <xsl:variable name="n" select="normalize-space($json-in)" />
  <xsl:choose>
    <xsl:when test="substring($n,1,1) = $quot">
    <xsl:variable name="t1">
        <xsl:call-template name="field">
          <xsl:with-param name="json-in" select="$n" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
    <xsl:variable name="t3">
      <xsl:choose>
      <xsl:when test="substring($t2,1,1)=','">
            <xsl:call-template name="fields">
              <xsl:with-param name="json-in" select="substring-after($t2,',')" />
          </xsl:call-template>
      </xsl:when>
      <xsl:when test="$t2">
        <so:extra><xsl:value-of select="$t2" /></so:extra>
      </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <so:output>
      <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
    </so:output>
    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
  </xsl:when>
    <xsl:when test="$n">
      <so:extra><xsl:value-of select="$n" /></so:extra>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="object">
  <!-- Input like: { X } bla -->
  <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
  <xsl:param name="json-in" />
  <xsl:param name="parent-ele" select="''" />
  <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
  <xsl:variable name="t2">
    <xsl:call-template name="fields">
      <xsl:with-param name="json-in" select="$t1" />
    </xsl:call-template>
  </xsl:variable>  
  <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
  <so:output>
    <xsl:choose>
    <xsl:when test="$parent-ele">
      <xsl:element name="{$parent-ele}">
        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
      </xsl:element>
    </xsl:when>
      <xsl:otherwise>    
        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
      </xsl:otherwise>    
    </xsl:choose>
  </so:output>
  <xsl:if test="$t3">
    <so:extra><xsl:value-of select="$t3" /></so:extra>
  </xsl:if>  
</xsl:template>

<xsl:template name="objects">
  <xsl:param name="json-in" />
  <xsl:param name="parent-ele" />
  <xsl:variable name="n" select="normalize-space($json-in)" />
  <xsl:choose>
    <xsl:when test="substring($n,1,1) = '{'">
    <xsl:variable name="t1">
        <xsl:call-template name="object">
          <xsl:with-param name="json-in" select="$n" />
          <xsl:with-param name="parent-ele" select="$parent-ele" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
    <xsl:variable name="t3">
      <xsl:choose>
      <xsl:when test="substring($t2,1,1)='{'">
            <xsl:call-template name="objects">
              <xsl:with-param name="json-in" select="$t2" />
              <xsl:with-param name="parent-ele" select="$parent-ele" />
          </xsl:call-template>
      </xsl:when>
      <xsl:when test="$t2">
        <so:extra><xsl:value-of select="$t2" /></so:extra>
      </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <so:output>
      <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
    </so:output>
    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
  </xsl:when>
    <xsl:when test="$n">
      <so:extra><xsl:value-of select="$n" /></so:extra>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="array">
  <!-- Input like: [ X1 X2 ] bla -->
  <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
  <xsl:param name="json-in" />
  <xsl:param name="parent-ele" />
  <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
  <xsl:variable name="t2">
    <xsl:call-template name="objects">
      <xsl:with-param name="json-in" select="$t1" />
      <xsl:with-param name="parent-ele" select="$parent-ele" />
    </xsl:call-template>
  </xsl:variable>  
  <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, ']'))" />
  <xsl:copy-of select="exsl:node-set($t2)/so:output" />
  <xsl:if test="$t3">
    <so:extra><xsl:value-of select="$t3" /></so:extra>
  </xsl:if>  
</xsl:template>

<xsl:template name="value">
  <!-- Input like either array, object or string -->
  <!-- output like either array, object or string -->
  <xsl:param name="json-in" />
  <xsl:param name="parent-ele" />
  <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
  <xsl:choose>
    <xsl:when test="$first-letter='{'">
    <xsl:call-template name="object">
        <xsl:with-param name="json-in" select="$json-in" />
        <xsl:with-param name="parent-ele" select="$parent-ele" />
    </xsl:call-template>
    </xsl:when>
    <xsl:when test="$first-letter='['">
    <xsl:call-template name="array">
        <xsl:with-param name="json-in" select="$json-in" />
        <xsl:with-param name="parent-ele" select="$parent-ele" />
    </xsl:call-template>
    </xsl:when>
    <xsl:when test="$first-letter=$quot">
    <xsl:call-template name="string">
        <xsl:with-param name="json-in" select="$json-in" />
        <xsl:with-param name="parent-ele" select="$parent-ele" />
    </xsl:call-template>
    </xsl:when>
  <xsl:otherwise>
    <so:output>ERROR</so:output>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="string">
  <!-- Input like: "X" bla -->
  <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
  <xsl:param name="json-in" />
  <xsl:param name="parent-ele" />
  <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
  <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
  <so:output>
   <xsl:element name="{$parent-ele}">
    <xsl:value-of select="$value" />
   </xsl:element>
  </so:output>
  <xsl:if test="$remainder">
    <so:extra><xsl:value-of select="$remainder" /></so:extra>
  </xsl:if>  
</xsl:template>

</xsl:stylesheet>

...applied to this input (modified from OP supplied to remove an extraneous comma)...

<sampleTag>
{
  "Order": {
    "InvestmentAccount": { "AccountNumber": "10" },
    "Parcel": {      
      "Limit": "0",
      "ExpiryDate": "1900-01-01T00:00:00",
      "Asset": [
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }]
    },
    "OrderDate": "2012-10-11T21:46:03.6489906+11:00"
  }
}
</sampleTag>

..yields...

<Order>
  <InvestmentAccount>
    <AccountNumber>10</AccountNumber>
  </InvestmentAccount>
  <Parcel>
    <Limit>0</Limit>
    <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
    <Asset>
      <Open>25.15</Open>
      <High>25.15</High>
      <Low>25.11</Low>
      <Close>25.87</Close>
    </Asset>
    <Asset>
      <Open>25.15</Open>
      <High>25.15</High>
      <Low>25.11</Low>
      <Close>25.87</Close>
    </Asset>
  </Parcel>
  <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

4 Comments

@Sean---Thanks for the link.I guess this supports only xslt2.0 version only but not the xslt v1.0...
@user1731504 I will work on an XSLT 1.0 json to xml converter, but only one that works on a limited subset of json. Hopefully, it will be enough for your needs.
@Sean -- In my json array items are comma separated. above xsl, giving me the first element only. How to tweak it for comma separated array element? eg: two asset items comma separated after }, bracket of each item.
@Kumaresan: I fixed that problem by changing the definition of variable $t2 in the template named "objects" to a <xsl:choose> element that checks to see whether the first character of exsl:node-set($t1)/so:extra is a comma. If so, strip it off before normalizing whitespace. Seems to work for us so far!
6

I tweaked Sean B. Durkin's template a bit and thought I'd share.

Updates include:

  • Support for numbers
  • Support for booleans
  • Fix for object array elements separated by a comma (per JSON spec)

Non-update changes:

  • Array elements are displayed in their own XML elements with the element name as the object key suffixed with _element

Still not supported:

  • Escaped characters (quotes) in strings

Here's the template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    xmlns:exsl="http://exslt.org/common"
    xmlns:so="http://stackoverflow.com/questions/13007280"
    exclude-result-prefixes="xsl xs json so exsl">
    <xsl:output indent="yes" encoding="UTF-8" />
    <xsl:strip-space elements="*" /> 

    <xsl:variable name="quot" select="'&quot;'" />
    <xsl:variable name="numbers" select="'0123456789'"/>
    <xsl:variable name="booleans" select="'tf'"/>

    <xsl:template match="/*">
        <xsl:variable name="t1">
            <xsl:call-template name="object">
                <xsl:with-param name="json-in" select="." />
            </xsl:call-template>
        </xsl:variable>
        <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
    </xsl:template>

    <xsl:template match="*" mode="copy-sans-namespace">
        <xsl:element name="{name()}" namespace="{namespace-uri()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="copy-sans-namespace" />
        </xsl:element>
    </xsl:template>

    <xsl:template name="field">
        <!-- Input like: "Open": "25.15" bla -->
        <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
        <xsl:variable name="remainder" select="substring-after($json-in,':')" />
        <xsl:call-template name="value">
            <xsl:with-param name="json-in" select="$remainder" />
            <xsl:with-param name="parent-ele" select="$field-name" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="fields">
        <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
        <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:variable name="n" select="normalize-space($json-in)" />
        <xsl:choose>
            <xsl:when test="substring($n,1,1) = $quot">
                <xsl:variable name="t1">
                    <xsl:call-template name="field">
                        <xsl:with-param name="json-in" select="$n" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                <xsl:variable name="t3">
                    <xsl:choose>
                        <xsl:when test="substring($t2,1,1)=','">
                            <xsl:call-template name="fields">
                                <xsl:with-param name="json-in" select="substring-after($t2,',')" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$t2">
                            <so:extra><xsl:value-of select="$t2" /></so:extra>
                        </xsl:when>
                    </xsl:choose>
                </xsl:variable>
                <so:output>
                    <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                </so:output>
                <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
            </xsl:when>
            <xsl:when test="$n">
                <so:extra><xsl:value-of select="$n" /></so:extra>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="object">
        <!-- Input like: { X } bla -->
        <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" select="''" />
        <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
        <xsl:variable name="t2">
                <xsl:call-template name="fields">
                <xsl:with-param name="json-in" select="$t1" />
            </xsl:call-template>
        </xsl:variable>  
        <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
        <so:output>
            <xsl:choose>
                <xsl:when test="$parent-ele">
                    <xsl:element name="{$parent-ele}">
                        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>    
                    <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                </xsl:otherwise>    
            </xsl:choose>
        </so:output>
        <xsl:if test="$t3">
            <so:extra><xsl:value-of select="$t3" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="objects">
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="n" select="normalize-space($json-in)" />
        <xsl:choose>
            <xsl:when test="substring($n,1,1) = '{'">
                <xsl:variable name="t1">
                    <xsl:call-template name="object">
                        <xsl:with-param name="json-in" select="$n" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                <xsl:variable name="t3">
                    <xsl:choose>
                        <xsl:when test="substring($t2,1,1)='{'">
                            <xsl:call-template name="objects">
                                <xsl:with-param name="json-in" select="$t2" />
                                <xsl:with-param name="parent-ele" select="$parent-ele" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
                            <xsl:call-template name="objects">
                                <xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
                                <xsl:with-param name="parent-ele" select="$parent-ele" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$t2">
                            <so:extra><xsl:value-of select="$t2" /></so:extra>
                        </xsl:when>
                    </xsl:choose>
                </xsl:variable>
                <so:output>
                    <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                </so:output>
                <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
            </xsl:when>
            <xsl:when test="$n">
                <so:extra><xsl:value-of select="$n" /></so:extra>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="array">
        <!-- Input like: [ X1 X2 ] bla -->
        <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
        <xsl:variable name="t2">
            <xsl:call-template name="objects">
                <xsl:with-param name="json-in" select="$t1" />
                <xsl:with-param name="parent-ele" select="$parent-ele" />
            </xsl:call-template>
        </xsl:variable>  
        <xsl:variable name="t3">
            <xsl:choose>
                <xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
                    <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="t4">
            <xsl:element name="{$parent-ele}">
                <xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
                    <xsl:variable name="self" select="."/>
                    <xsl:variable name="tempResult">
                        <xsl:element name="{concat($parent-ele,'_element')}">
                            <xsl:copy-of select="exsl:node-set($self/*)" />
                        </xsl:element>
                    </xsl:variable>
                    <xsl:copy-of select="exsl:node-set($tempResult)"/>
                </xsl:for-each>
            </xsl:element>
        </xsl:variable>
        <xsl:variable name="t5" select="exsl:node-set($t4)"/>
        <so:output>
            <xsl:copy-of select="$t5"/>
        </so:output>
        <xsl:if test="$t3">
            <so:extra><xsl:value-of select="$t3" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="value">
        <!-- Input like either array, object or string -->
        <!-- output like either array, object or string -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
        <xsl:choose>
            <xsl:when test="$first-letter='{'">
                <xsl:call-template name="object">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$first-letter='['">
                <xsl:call-template name="array">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$first-letter=$quot">
                <xsl:call-template name="string">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($numbers,$first-letter)">
                <xsl:call-template name="number">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($booleans,$first-letter)">
                <xsl:call-template name="boolean">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <so:output>ERROR</so:output>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="string">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
        <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="number">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="remainder">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>  
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>
    <xsl:template name="boolean">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="remainder">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>  
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>

</xsl:stylesheet>

So with this (adjusted) input:

<?xml version="1.0" encoding="UTF-8"?>
<sampleTag><![CDATA[
    {
        "Order": {
            "InvestmentAccount": { "AccountNumber": "10" },
            "Parcel": {      
                "Limit": 0,
                "ExpiryDate": "1900-01-01T00:00:00",
                "valid": true,
                "Asset": [
                    {        
                        "Open": 25.15,
                        "High": 25.15,
                        "Low": 25.11,
                        "Close": 25.87
                    },
                    {        
                        "Open": 25.15,
                        "High": 25.15,
                        "Low": 25.11,
                        "Close": 25.87
                    }
                ]
            },
            "OrderDate": "2012-10-11T21:46:03.6489906+11:00"
        }
    }
]]></sampleTag>

I get this output:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
   <InvestmentAccount>
      <AccountNumber>10</AccountNumber>
   </InvestmentAccount>
   <Parcel>
      <Limit>0</Limit>
      <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
      <valid>true</valid>
      <Asset>
         <Asset_element>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
         </Asset_element>
         <Asset_element>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
         </Asset_element>
      </Asset>
   </Parcel>
   <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

1 Comment

This solution was almost perfect. Just missing array of simple elements: {"array": ["1", "2", "3"]}
4

Updating Samuel Murphy answer.

Updates include:

  • Support for null
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    xmlns:exsl="http://exslt.org/common"
    xmlns:so="http://stackoverflow.com/questions/13007280"
    exclude-result-prefixes="xsl xs json so exsl">
    <xsl:output indent="yes" encoding="UTF-8" />
    <xsl:strip-space elements="*" /> 

    <xsl:variable name="quot" select="'&quot;'" />
    <xsl:variable name="numbers" select="'0123456789'"/>
    <xsl:variable name="booleans" select="'tf'"/>
    <xsl:variable name="nulls" select="'n'"/>

    <xsl:template match="/*">
        <xsl:variable name="t1">
            <xsl:call-template name="object">
                <xsl:with-param name="json-in" select="." />
            </xsl:call-template>
        </xsl:variable>
        <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
    </xsl:template>

    <xsl:template match="*" mode="copy-sans-namespace">
        <xsl:element name="{name()}" namespace="{namespace-uri()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="copy-sans-namespace" />
        </xsl:element>
    </xsl:template>

    <xsl:template name="field">
        <!-- Input like: "Open": "25.15" bla -->
        <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
        <xsl:variable name="remainder" select="substring-after($json-in,':')" />
        <xsl:call-template name="value">
            <xsl:with-param name="json-in" select="$remainder" />
            <xsl:with-param name="parent-ele" select="$field-name" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="fields">
        <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
        <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:variable name="n" select="normalize-space($json-in)" />
        <xsl:choose>
            <xsl:when test="substring($n,1,1) = $quot">
                <xsl:variable name="t1">
                    <xsl:call-template name="field">
                        <xsl:with-param name="json-in" select="$n" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                <xsl:variable name="t3">
                    <xsl:choose>
                        <xsl:when test="substring($t2,1,1)=','">
                            <xsl:call-template name="fields">
                                <xsl:with-param name="json-in" select="substring-after($t2,',')" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$t2">
                            <so:extra><xsl:value-of select="$t2" /></so:extra>
                        </xsl:when>
                    </xsl:choose>
                </xsl:variable>
                <so:output>
                    <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                </so:output>
                <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
            </xsl:when>
            <xsl:when test="$n">
                <so:extra><xsl:value-of select="$n" /></so:extra>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="object">
        <!-- Input like: { X } bla -->
        <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" select="''" />
        <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
        <xsl:variable name="t2">
                <xsl:call-template name="fields">
                <xsl:with-param name="json-in" select="$t1" />
            </xsl:call-template>
        </xsl:variable>  
        <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
        <so:output>
            <xsl:choose>
                <xsl:when test="$parent-ele">
                    <xsl:element name="{$parent-ele}">
                        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>    
                    <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                </xsl:otherwise>    
            </xsl:choose>
        </so:output>
        <xsl:if test="$t3">
            <so:extra><xsl:value-of select="$t3" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="objects">
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="n" select="normalize-space($json-in)" />
        <xsl:choose>
            <xsl:when test="substring($n,1,1) = '{'">
                <xsl:variable name="t1">
                    <xsl:call-template name="object">
                        <xsl:with-param name="json-in" select="$n" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                <xsl:variable name="t3">
                    <xsl:choose>
                        <xsl:when test="substring($t2,1,1)='{'">
                            <xsl:call-template name="objects">
                                <xsl:with-param name="json-in" select="$t2" />
                                <xsl:with-param name="parent-ele" select="$parent-ele" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
                            <xsl:call-template name="objects">
                                <xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
                                <xsl:with-param name="parent-ele" select="$parent-ele" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$t2">
                            <so:extra><xsl:value-of select="$t2" /></so:extra>
                        </xsl:when>
                    </xsl:choose>
                </xsl:variable>
                <so:output>
                    <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                </so:output>
                <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
            </xsl:when>
            <xsl:when test="$n">
                <so:extra><xsl:value-of select="$n" /></so:extra>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="array">
        <!-- Input like: [ X1 X2 ] bla -->
        <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
        <xsl:variable name="t2">
            <xsl:call-template name="objects">
                <xsl:with-param name="json-in" select="$t1" />
                <xsl:with-param name="parent-ele" select="$parent-ele" />
            </xsl:call-template>
        </xsl:variable>  
        <xsl:variable name="t3">
            <xsl:choose>
                <xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
                    <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="t4">
            <xsl:element name="{$parent-ele}">
                <xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
                    <xsl:variable name="self" select="."/>
                    <xsl:variable name="tempResult">
                        <xsl:element name="{concat($parent-ele,'_element')}">
                            <xsl:copy-of select="exsl:node-set($self/*)" />
                        </xsl:element>
                    </xsl:variable>
                    <xsl:copy-of select="exsl:node-set($tempResult)"/>
                </xsl:for-each>
            </xsl:element>
        </xsl:variable>
        <xsl:variable name="t5" select="exsl:node-set($t4)"/>
        <so:output>
            <xsl:copy-of select="$t5"/>
        </so:output>
        <xsl:if test="$t3">
            <so:extra><xsl:value-of select="$t3" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="value">
        <!-- Input like either array, object or string -->
        <!-- output like either array, object or string -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
        <xsl:choose>
            <xsl:when test="$first-letter='{'">
                <xsl:call-template name="object">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$first-letter='['">
                <xsl:call-template name="array">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$first-letter=$quot">
                <xsl:call-template name="string">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($numbers,$first-letter)">
                <xsl:call-template name="number">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($booleans,$first-letter)">
                <xsl:call-template name="boolean">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($nulls,$first-letter)">
                <xsl:call-template name="boolean">
                    <xsl:with-param name="json-in" select="$json-in" />
                    <xsl:with-param name="parent-ele" select="$parent-ele"/>
                </xsl:call-template>
            </xsl:when>         
            <xsl:otherwise>
                <so:output>ERROR</so:output>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="string">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
        <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>

    <xsl:template name="number">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="remainder">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>  
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>
    <xsl:template name="boolean">
        <!-- Input like: "X" bla -->
        <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
        <xsl:param name="json-in" />
        <xsl:param name="parent-ele" />
        <xsl:variable name="value">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="remainder">
            <xsl:choose>
                <xsl:when test="contains(substring-before($json-in,','),'}')">
                    <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                </xsl:when>
                <xsl:when test="contains(substring-before($json-in,','),']')">
                    <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>  
        <so:output>
            <xsl:element name="{$parent-ele}">
                <xsl:value-of select="$value" />
            </xsl:element>
        </so:output>
        <xsl:if test="$remainder">
            <so:extra><xsl:value-of select="$remainder" /></so:extra>
        </xsl:if>  
    </xsl:template>

</xsl:stylesheet>

Comments

2

The question is tagged for XSLT 1.0, so I do not know if this answer will help the original question. But if you can use XSLT 3.0, the function json-to-xml does exactly what you need.

https://www.w3.org/TR/xslt-30/#func-json-to-xml

1 Comment

Using our utility as pre-processing step you can get the XSLT 3.0 JSON/XML format without requiring an XSLT 3.0 processor: github.com/AtomGraph/JSON2XML
0

As well as Dimitre's XSLT parsing framework, there is also Gunther Rademacher's Rex parser generator, which also includes JSON as one of its sample grammars:

http://www.bottlecaps.de/rex/

Comments

0

Try this lib:

https://github.com/bramstein/xsltjson

Looks very nice.

Its a 2.0 XSLT solution, although he also points to 1.0 version.

Comments

-1

XSLT has many strengths and a few big weaknesses. Text processing is its weakness at least version 1.0 Although it would be technically possible to process that text with XSLT 1.0, I can't think of any situation where it would be a very good idea, and where it wouldn't be a very fragile conversion. The code you would have to produce would be very unwieldy.

Is there no other language available to you to do the processing?

9 Comments

@Woody--I have to do this only using XSLT that too v1.0
ok, well, as I said it is possible, and the worst possible option, but if it is the only option I guess you have to proceed with it. So is there only one order per tag? Is the case always the same, are the keys in the script always the same? are there any other tags? To do it with xslt you have to use very dumb text processing and throw away all the useful features of xslt, so you need to know the restrictions beforehand (which are, if the format is not exactly the same, it WILL break)
@Woody--The tags will be changed(can't depend on the tagnames).Is it possible to get the general the xslt
I have to disagree @Woody in relation to fragility. It looks like Dimitre's json to xml library function is generic enough to be considered robust. I'm sure he will drop by and have a word or two about it.
@Woody, People, Please don't make absolute statements about XSLT. What is true for XSLT 1.0 maynot be true for XSLT 2.0 and XSLT 3.0. The fact is that XSLT 2.0 and above has very powerful string processing capabilities and there are complete and generic parser (LR-1) frameworks written completely in XSLT 2.0. I have written parsers for subset of JSON and for XPath 2.0 -- using my generic LR-1 framework -- all writtent in pure XSLT 2.0. We should seriously think what we should understand as default when someone says"XSLT". I believe this no longer shoud be XSLT 1.0 -- nowadays this is XSLT 2.0.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.