Free XSLT 1.0 stylesheet to transform XML data to JSON
While looking for an XSLT 1.0 stylesheet, which is able to transform XML to JSON, I had to realize that none of the existing stylesheets generate valid JSON code.Most of them have problems if a node has sub-nodes and attributes at the same time, others don't handle arrays correctly. But both are essential requirements for me, so I've decided to write my own XML to JSON transformation stylesheet.
Features & Limitations
- Support of arrays, even if equal-named nodes are interrupted by other nodes.
- Support of nodes with sub-nodes and attributes at the same time.
- Support for sub-nodes and attributes using the same name.
- Highly optimized for speed and size (less than 4 KiB code including comments).
- Error-proven in the XSLT engines of PHP (Sablotron v1.0.3), Internet Explorer 7/8 (MSXML) and Firefox 3.5 (TransforMiiX) using numerous XML files.
- Escaping of quotation marks, tabulators, carriage returns, newlines, slashs and backslashes by their escape-codes (\", \t, \r, \n, \/, \\).
- Form-feed and backspace characters don't get escaped to \f and \b, because XML version 1.0 does not support these characters.
- Text content will be only returned till the first occurrence of a sub-node (<node>Some <b>bold</b> text</node> will return {"node":{"#":"Some ","b":{"#":"bold"}}}).
- All textual data will be stored as string. I've decided not to interprete "true" and "false" as boolean, or "1000" as number to prevent unexpected results. If you need a specific data type, simply convert it in your script.
License
This work is licensed under the "Creative Commons — Attribution-Share Alike 3.0 Germany" license.
That means you are free:
to Share
to copy, distribute and transmit the work
to copy, distribute and transmit the work
to Remix
to adapt the work
to adapt the work
Under the following conditions:
Attribution.
You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
Share Alike.
If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get permission from the copyright holder.
The author's moral rights.
Download
| Filename | Version | Release Date | Size |
|---|---|---|---|
| xml2json.xsl | v1.0.0 | 2009-08-09 | 3.52 KiB |
| xml2json.zip | v1.0.0 (ZIP archive) | 2009-08-09 | 1.07 KiB |
Sample
This output is formatted to give you an overview about the object layout. The code which is returned by the stylesheet doesn't contain spaces and line-feeds to get the object code as small as possible.
Move the mouse cursor above a key name, to get more details about it.
Test your own XML file (max. 256 KiB):