0

I have using Windows and SAXON 9.9(HE).

My JSON Code is:

{"analystId": "Test","jobId": "","profileData":{"allAuthorCoverage": false,"abc":"xyz","assetClasses":[{"status": "Test1"}]}}

MY XSLT Code is:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" /> 
<xsl:strip-space elements="*"/>
<xsl:param name="input" select="'simple3.json'"/>
<xsl:template match="@*|node()"> 
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template> 
<xsl:template name="init">
<xsl:apply-templates select="json-to-xml(unparsed-text($input))" mode="copy"/>
</xsl:template>
<xsl:template match="node() | @*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

My required output XML is:

<root>
<analystId>Test</analystId>
<jobId></jobId>
<profileData>
<allAuthorCoverage>false</allAuthorCoverage>
<abc>xyz</abc>
</profileData>
</root>

How do achieve this?

1 Answer 1

3

There are several ways to approach this conversion in XSLT 3.0.

One way is to use json-to-xml(), and then transform the resulting XML. The resulting XML is

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="analystId">Test</string>
   <string key="jobId"/>
   <map key="profileData">
      <boolean key="allAuthorCoverage">false</boolean>
      <string key="abc">xyz</string>
      <array key="assetClasses">
         <map>
            <string key="status">Test1</string>
         </map>
      </array>
   </map>
</map> 

and you can transform it either with generic rules such as

<xsl:template match="*[@key]">
  <xsl:element name="{@key}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

or with specific rules like:

<xsl:template match="fn:string[@key='analystId']>
  <analystId>{.}</analystId>
</xsl:template>

or with some combination of the two.

The second way to do it is to write a template for the desired output XML and then dive into the parsed JSON to extract specific values where required:

<xsl:template name="xsl:initial-template">
  <xsl:variable name="json" select="parse-json(....)"/>
  <root>
    <analystId>{$json?analystId}</analystId>
    <jobId>{$json?jobId}</jobId>
    <profileData>
      <allAuthorCoverage>{$json?profileData?allAuthorCoverage}</allAuthorCoverage>
      <abc>{$json?profileData?abc}</abc>
    </profileData>
  </root>
</xsl:template>

For this simple example, the second approach is probably easiest, but I suspect the first approach scales better to more complex real-world use cases.

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

2 Comments

@Mihael: Its working fine except dummy tags. How to avoid dummy element <map xmlns="http://www.w3.org/2005/xpath-functions"> and <type xmlns="">?
I don't know what you're doing, so I don't know what you're doing wrong...

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.