XML to JSON. Complete transformation
This is an open discussion with 21 replies, filed under XSLT.
Search
It seems to me that your utility is doing a similar thing as the one I wrote a while ago (except for mine also taking care of element names).
This wasn't the first situation in wich this happened so we definitely need to find a method to prevent us from writing the same code over and over again.
Heh, nice.
This wasn't the first situation in wich this happened so we definitely need to find a method to prevent us from writing the same code over and over again.
Depends on everyone to FIRST check out what others have written (XSLT utilities) and after that decide what to do.
XML to JSON
updated to version 2.0
on 2nd of March 2012
I adapted the utility from Doeke Zanstra. This is complete stuff. See the descriprion.
Doesn't take into account numbers that start with 0
. Updated at this gist
Updated. Thanks, John.
Also, I was having a slight problem where the last xml element passed in, if it's value was null
ie an empty element, the JSON output wasn't correctly closed, missing the }
from the end.
Can you post an XML so I can take it to test?
yap, there’s a bug in the xml2json.xslt (I'm using this utility for a while, not vladG’s, but Doeke Zanstra’s one). Also single Item Arrays won’t be parsed as array which is unfortunate. The later i fixed by replacing a template match selector (around line 159):
<xsl:template match="*[count(../*[name(../*)=name(.)])=count(../*) and count(../*)>0 and name() = 'item']">
@designermonkey:
does this the trick?
@iwyg
It does the trick. I noticed your modification right now after I altered the utility in my own way :))
XML to JSON
updated to version 2.1
on 30th of April 2012
Removed comment.
I experienced some problems with this xsl utility, in some cases it still just fails (e.g. a single xml node like <foo><item>bar</item><item>baz</item></foo>
wil output something like that: "foo":["bar","baz"]}}
).
There're some promising alternatives (scroll down to XSLTJSON LITE
) out there. Unfortunately it requires xslt 2.0. but there's a xslt to json lite
thats compatible with xslt 1.0.
I will get my hands on it as soon as I can.
Funny you should mention xsltjson lite. I was looking at that the other day too.
To bump this thread: https://github.com/bojanbjelic/xml2json
I've modified this utility to support xml attributes. Also array structures can now be explicitly triggered using wildcard names.
Is there a way to output escaped html?
You will have to "serialize" your HTML. Here's a template that can do this:
or you can just use something like this and reduce the transformation overhead
Create an account or sign in to comment.
A new XSLT utility, "XML to JSON" is now available for download. Comments and feedback can be left here.