xsl-list
[Top] [All Lists]

[xsl] XSLT functions for JSON

2008-05-18 06:53:12
I think we need three XSLT functions for JSON.

As I have found out from the "XSL in the AJAX world" thread,
http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200805/msg00326.html,
there are several issues in xml2json transformation and in json2xml
transformation:

1. As seen from Google's use of JSON,
http://code.google.com/apis/gdata/json.html#Background,
and from Stefan Goessner's article, Converting Between XML and JSON,
http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html,
property names in JSON could begin with characters not allowed as
first character in XML like "$" (used by Goggle) and "@", and "#" used
by article. I find it strange but not surprising that even in JSON
only "$" works in both IE7 and Firefox. "@" only works in Firefox, and
"#" not even in Firefox!

The bottom line is: some characters are allowed as first character in
JSON but not in XML making even the most simple json2xml
transformation problematic:  the maker of the function must find a way
to deal with illegal characters in XML.

2. JSON has no way of handling even the most simple document oriented
XML structure like: "h2, p, h2, p" if the order matters as is likely
in the example. In JSON the order of the example will be changed to:
"h2, h2, p, p".

3. JSON has no standardized way of handling attributes. Attributes
will most likely be converted to what could just as well have been an
element or some prefix character will be added signaling that this was
originally an attribute. This prefix depends on who made the
conversion function, making it impossible to convert back unless a
similar function is used understanding the meaning of the prefix
character.

4. JSON has no standardized way of handling mixed content.
Theoretically the most simple mixed content could be handled with a
method similar to handling attributes but the prefix characters needed
to tell text and element apart depends on who made the function. If
mixed content consists of more than one text node and one element we
run into the reordering problem.

In XSLT I see the need for three JSON functions:

1. We need a "standalone" json2xml function. If  first characters are
used in JSON not legal in XML, or if other constructs that cannot be
converted to XML (?) is used, it must abort. For first character it
must be possible to use the function with a parameter solving the
first character issue changing it to something legal.

2.  We need a pair of xml2json and json2xml functions that can
transform back and forth. The xml2json function must abort if
reordering of elements becomes necessary or if mixed content is
detected. Many things in the XML document will of cause be lost: XML
Declaration, DOCTYPE, PIs and comments. If reordering of elements is
not a problem it must be possible to use some parameter making the
transformation successful.

Cheers,
Jesper Tverskov
http://www.xmlplease.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>