1

When transforming an XML document with XSLT, is it possible to convert embedded JSON (i.e. JSON formatted content) in the process?

For example the following: -

<form>
    <data>[{"id":1,"name":"Hello"},{"id":2,"name":"World"}]</data>
</form>

Would be converted to: -

<form>
    <data>
        <id name="Hello">1</id>
        <id name="World">2</id>
    </data>
</form>

2 Answers 2

1

Parsing JSON is supported in XSLT 3.0 so using the commercial versions of Saxon 9.7 you could use

<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:strip-space elements="*"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="data">
        <xsl:copy>
            <xsl:apply-templates select="parse-json(.)?*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match=".[. instance of map(xs:string, item())]">
        <id name="{.?name}">
            <xsl:value-of select=".?id"/>
        </id>
    </xsl:template>

</xsl:stylesheet>

Using the open source version of Saxon 9.7 (i.e. Saxon 9.7 HE) the following takes up the suggestion made by wero to use json-to-xml and shows how to implement the requirement:

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

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

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

    <xsl:template match="fn:map">
        <id name="{fn:string[@key = 'name']}">
            <xsl:value-of select="fn:number[@key = 'id']"/>
        </id>
    </xsl:template>

</xsl:stylesheet>

Saxon 9.7 HE is available on Maven and from http://saxon.sourceforge.net/

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

2 Comments

Thanks but I fear a commercial offerring is beyond the scope of my project.. I was hoping for an XSLT 2.0 solution (the supported release within the Oracle 11gR2 database) OR an alternative method to support XSLT 3.0 within the Oracle database by importing a jar or whatever.
@AlbPuado, I have added an example that works with the open source HE edition of Saxon 9.7, it uses the function json-to-xml already suggested in the other answer. As for Oracle and XSLT 2.0, I am afraid I am not familiar with it and its features like extension functions, consider to tag your question with a specific tag if you are looking for a solution in that environment, perhaps than others familiar with it can tell you whether there are extension functions to parse and process JSON.
1

It should be possible in XSLT 3.0, given that it has a json-to-xml function:

Parses a string supplied in the form of a JSON text, returning the results in the form of an XML document node.

You could try to get this to run with the current implementation in Saxon.

Comments

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.