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 MIT license.
That means, you may:
- Use the work commercially.
- Make changes to the work.
- Distribute the compiled code and/or source.
- Uncorporate the work into something that has a more restrictive license
- Use the work for private use.
You may not:
- Hold the author liable. The work is provided "as is".
You must:
- Include the copyright notice in all copies or substantial uses of the work.
- >Include the license notice in all copies or substantial uses of the work.
Download
Filename | Release Date | Size |
---|---|---|
xml2json.xsl | 2019-10-16 | 3.52 KiB |
xml2json.zip | 2019-10-16 | 1.83 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):