|
TransformingRules
Transforming rules of the Parker convention, with some introduction. version 0.4.
xml2json.xsltxml2json.xslt is a XSLT 1.0 stylesheet to transform arbitrary XML to JSON. There is also a version for javascript. The workings are demonstrated with the accompanied xml files. The target of this library is to create javascript-like JSON, not XML-like JSON, like the BadgerFish convention. The downside is it won't translate all markup, and there's a chance the JSON is invalid (see Plans and Issues). However, if you control the XML, you will enjoy the merits of javascript. Enjoy ;-) I will call this the Parker convention, after the comic Parker & Badger by Cuadrado. I can really recommend the comic, also in relation to this work and the BadgerFish convention. UsingYou can load the xml files in your browser (Firefox/Camino work, Safari doesn't, Internet Explorer 6 works, except test-js.xml). On windows, you can also try the commandline Msxsl.exe utility. And of course you can fire up your favorite transformation engine. Translation JSON1. The root element will be absorbed, for there is only one: <root>test</root> becomes "test" 2. Element names become object properties: <root><name>Xml</name><encoding>ASCII</encoding></root> becomes {"name":"Xml","encoding":"ASCII"}3. Numbers are recognized (integers and decimals): <root><age>12</age><height>1.73</height></root> becomes {"age":12,"height":1.73}4. Booleans are recognized case insensitive: <root><checked>True</checked><answer>FALSE</answer></root> becomes {"checked":true,"answer":false}5. Strings are escaped: <root>Quote: " New-line: </root> becomes "Quote: \" New-line:\n" 6. Empty elements will become null: <root><nil/><empty></empty></root> becomes {"nil":null,"empty":null}7. If all sibling elements have the same name, they become an array <root><item>1</item><item>2</item><item>three</item></root> becomes [1,2,"three"] 8. Mixed mode text-nodes, comments and attributes get absorbed: <root version="1.0">testing<!--comment--><element test="true">1</element></root> becomes {element:true}9. Namespaces get absorbed, and prefixes will just be part of the property name: <root xmlns:ding="http://zanstra.com/ding"><ding:dong>binnen</ding:dong></root> becomes {"ding:dong":"binnen"}Translation JS extraAll the same as the JSON translation, but with these extra's: 1. Property names are only escaped when necessary <root><while>true</while><wend>false</wend><only-if/></root> becomes {"while":true,wend:false,"only-if":null}2. Within a string, closing elements "</" are escaped as "<\/" <root><![CDATA[<script>alert("YES");</script>]]></root>becomes {script:"<script>alert(\"YES\")<\/script>"}3. Dates are created as new Date() objects <root>2006-12-25</root> becomes new Date(2006,12-1,25) 4. Attributes and comments are shown as comments (for testing-purposes): <!--testing--><root><test version="1.0">123</test></root> becomes /*testing*/{test/*@version="1.0"*/:123}5. A bit of indentation is done, to keep things ledgible Plans and IssuesI don't really have much plans with this library. It was mere a result of learning XSLT and XPath better. However, I will perform bug-fixes. Issues I might address, if I can find a satisfactory solution:
<root><item>1</item><item>2</item><name>test</name></root> becomes faulty JSON at the moment {"item":1,"item":2,"name":"test"}There's also some Copyright. |
Sign in to add a comment
<root><abc/></root>
I am interested in this library as it fits a need in an application I'm working on. I think I may have a fix for the "handle siblings with duplicate names" problem. If you would, please contact me at centaur at google.com so I can discuss the potential fix with you.
// Spry.XML.documentToObject - Utility method for creating a // JavaScript? object with properties and nested objects that // mirror an XML document tree structure. // // Sample XML: // // <employees> // <employee id="1000"> // <name>John Doe</name> // </employee> // <employee id="2000"> // <name><!Smith?]></name> // </employee> // </employees> // // Object returned by documentToObject(): // // { // employees: // { // employee: // [ // { // @id: "1000", // name: { "#text": "John Doe" } // }, // { // @id: "2000", // name: { "#cdata-section": "Jane Smith" } // } // ] // } // }
Spry.XML.documentToObject = function(xmlDoc) {
};<?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="xml-2-json.xsl"?>
<ConfListOfConfForHtmlAction> <ControlArea> <Action>
<StatusInfo> <SystemConfiguration> <DataArea> <ConfNode Key="SETBOX.COSHIP100" KeyType="1" HasDynamicChild="false" IsSelectable="false" IsRequired="false" IsNeedRestart="false" AppName="CW" InstanceName=""> <KeyItem Key="SETBOX.COSHIP100.DeviceID" ParentKey="SETBOX.COSHIP100" Name="DeviceID" KeyType="2" EditLevel="2" DefaultValue="0" HasDynamicChild="false" VisibelLevel="2" IsSelectable="true" IsRequired="false" IsNeedRestart="false" Scope="3" DataType="4" AppName="CW" Sequence="0" InstanceID="0_CW" InstanceName="Global" KeyPattern="SETBOX.*.DeviceID" NameDisplay="CW_10001" IsCommonConfig="0" UIKey="0_CW_SETBOX_COSHIP100_DeviceID" NameDisplayFlag="0" Value="0">how is that converting attributes to properties a problem that you have yet to find a satisfactory solution? From my perspective it seems rudimentary: attributes should transform to properties as literals (string, number, boolean), component elements are transformed to properties as objects {}... unless the given element is a collection of like elements ( array) or contains only a simple type ( literal). I'll be building an xslt transformer this way and I would be interested to know if there is something that I'm just not seeing... thanks :)
Hi Rein - I am interested to do the same exact thing that you describe above - convert attributes to props. Have you made progress? If so can you please share your code.
Thanks, Radu.
Here is a template for adding attributes:
I have expanded on your orignal work to include data typing and flattening of resulting JSON. Although this moves outside of the JSON definition as well as the BadgerFish? definition it provides for a more lightweight result set object from trusted sources.
You can see at http://code.google.com/p/xmltojson
Regards Keith Chadwick http://keithchadwick.wordpress.com
This is nice. I have 2 suggestions:
Concerning rule 7, what do you do if there is only a subset of siblings with the same name? For example, Atom <link> elements inside a <feed>. I would encode as an array but use the element name as property name. Something like:
Concerning rule 9, why don't you convert the colon to a dollar sign, to get a valid Javascript identifier.
Bruno