JSONX to XML Conversion Using Xslt
What is JSON ?
JSON (JavaScript Object Notation) is a
lightweight data-interchange format. It is easy for humans to read and write.
It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262
3rd Edition - December 1999. JSON is a text format that is
completely language independent but uses conventions that are familiar to
programmers of the C-family of languages, including C, C++, C#, Java,
JavaScript, Perl, Python, and many others. These properties make JSON an ideal
data-interchange language.
JSON is built on two
structures:
- A collection of name/value pairs. In various languages,
this is realized as an object, record, struct, dictionary,
hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is
realized as an array, vector, list, or sequence.
These are universal data
structures. Virtually all modern programming languages support them in one form
or another. It makes sense that a data format that is interchangeable with
programming languages also be based on these structures.
What is JSONx ?
JSONx is an IBM®
standard format to represent JSON as XML. The appliance converts JSON messages
that are specified as JSON message type to JSONx. The appliance provides a
style sheet that you can use to convert JSONx to JSON.
JSONx conversion rules
specify how a DataPower® service converts a JSON structure to JSONx (XML).
The DataPower appliance
provides the default jsonx2json.xsl style sheet for converting JSONx to JSON.
This style sheet is in thestore: directory.
Transformation of Jsonx to Xml
JSONX
<?xml version="1.0" encoding="UTF-8"?> <json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"> <json:string name="name">John Smith</json:string> <json:object name="address"> <json:string name="streetAddress">21 2nd Street</json:string> <json:string name="city">New York</json:string> <json:string name="state">NY</json:string> <json:number name="postalCode">10021</json:number> </json:object> <json:array name="phoneNumbers"> <json:string>212 555-1111</json:string> <json:string>212 555-2222</json:string> </json:array> <json:null name="additionalInfo" /> <json:boolean name="remote">false</json:boolean> <json:number name="height">62.4</json:number> <json:string name="ficoScore">> 640</json:string> </json:object>
XSLT
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
exclude-result-prefixes="
json">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template
match="json:object[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template
match="json:array[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template
match="json:string[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template
match="json:number[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template
match="json:boolean[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template
match="json:null[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output
<?xml
version="1.0" encoding="UTF-8"?>
<name>John Smith</name>
<address>
<streetAddress>21 2nd
Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumbers>
212 555-1111
212 555-2222
</phoneNumbers>
<additionalInfo/>
<remote>false</remote>
<height>62.4</height>
<ficoScore>>
640</ficoScore>
No comments:
Post a Comment