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:
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.
| Attachment | Size |
|---|---|
| json2xml.zip | 5.13 KB |
Comments
Post new comment