JSON to XML in XSLT

XSLT is powerful enough to process even non-XML input. For example, I have created a transformation that converts JSON text to well structured XML output:

JSON text:

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": [
         { "type": "home", "number": "212 555-1234" },
         { "type": "fax", "number": "646 555-4567" }
     ]
}

XML result:

 <?xml version="1.0" encoding="UTF-8"?>
<json>
   <object>
      <field name="firstName">
         <string>John</string>
      </field>
      <field name="lastName">
         <string>Smith</string>
      </field>
      <field name="age">
         <number>25</number>
      </field>
      <field name="address">
         <object>
            <field name="streetAddress">
               <string>21 2nd Street</string>
            </field>
            <field name="city">
               <string>New York</string>
            </field>
            <field name="state">
               <string>NY</string>
            </field>
            <field name="postalCode">
               <string>10021</string>
            </field>
         </object>
      </field>
      <field name="phoneNumber">
         <array>
            <object>
               <field name="type">
                  <string>home</string>
               </field>
               <field name="number">
                  <string>212 555-1234</string>
               </field>
            </object>
            <object>
               <field name="type">
                  <string>fax</string>
               </field>
               <field name="number">
                  <string>646 555-4567</string>
               </field>
            </object>
         </array>
      </field>
   </object>
</json>

It works by employing XML Pipeline technique:

  • first mode parses text using regular expressions and generates sequence of tokens in XML format: <comment>, <string>, <number>, <symbol>{</symbol>, etc.
  • second mode groups all tokens between "{" and "}" symbols into <object> element, between "[" and "]" symbols into <array> element
  • third mode makes a <field> element from <string><symbol>:<symbol>(<string>|<number>|<object>|<array>) sequence
  • fourth mode drops comma between consecutive <field> elements

and finally it performs XSD validation check.

This approach is quite exotic and non-standard, because when people hear word "parsing" they think of BNF, state-machine, AST, YACC, etc., but these technologies were created to parse complex programming languages.

I claim that XSLT coupled w/ regexp as tokenizer is powerful enough to convert simple markup languages such as JSON, CSS, MIF, RTF, wiki, etc., into XML.

p.s.
See attachment below for source code of the transformation.

AttachmentSize
json2xml.zip5.13 KB

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options