Transformasjon fra XML til JSON
Vi lage en transformasjon som lager et flatt format etter mønster av eksempelet reorganisering i modulen XML2XML
Transformasjonen er slik:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" doctype-system="olympreorg.dtd" encoding="UTF-8"/> <xsl:template match="/"> {"events":[ <xsl:apply-templates select="/IOC/OlympicGame/event"/> ]} </xsl:template> <xsl:template match="event"> { "distanse":"<xsl:value-of select="@dist"/>", "place":"<xsl:value-of select="ancestor::OlympicGame/@place"/>", "year":"<xsl:value-of select="ancestor::OlympicGame/@year"/>", "athlets":[ <xsl:apply-templates select="athlet"> <xsl:sort select="result" data-type="number" order="ascending"/> </xsl:apply-templates> ]}, </xsl:template> <xsl:template match="athlet"> {"name":"<xsl:apply-templates select="name"/>", "nation":"<xsl:apply-templates select="nation"/>", "result":"<xsl:apply-templates select="result"/>"}, </xsl:template> </xsl:stylesheet>
Resultatet er en textfil som ser slik ut:
_jsonfil
{"events":[ { "distanse":"100m", "place":"Barcelona", "year":"1992", "athlets":[ {"name":"Linford Christie", "nation":"GBR", "result":"09.96"}, {"name":"Frank Fredericks", "nation":"NAM", "result":"10.02"}, {"name":"Dennis Mitchell", "nation":"USA", "result":"10.04"}, {"name":"Bruny Surin", "nation":"CAN", "result":"10.09"}, {"name":"Leroy Burrell", "nation":"USA", "result":"10.10"}, {"name":"Olapade Adeniken", "nation":"NGR", "result":"10.12"}, {"name":"Raymond Stewart", "nation":"JAM", "result":"10.22"}, {"name":"Davidson Ezima", "nation":"NGR", "result":"10.26"}, ]}, { "distanse":"200m", "place":"Barcelona", "year":"1992", "athlets":[ {"name":"Mike Marsh", "nation":"USA", "result":"20.01"}, {"name":"Frank Fredericks", "nation":"NAM", "result":"20.13"}, {"name":"Michael Bates", "nation":"USA", "result":"20.38"}, {"name":"Robson Caetano da Silva", "nation":"BRA", "result":"20.45"}, {"name":"Olapade Adeniken", "nation":"NGR", "result":"20.50"}, {"name":"John Regis", "nation":"GBR", "result":"20.55"}, {"name":"Oluyemi Kayode", "nation":"NGR", "result":"20.67"}, {"name":"Marcus Adam", "nation":"GBR", "result":"20.80"}, ]}, { "distanse":"400m", "place":"Barcelona", "year":"1992", "athlets":[ {"name":"Quincy Watts", "nation":"USA", "result":"43.50"}, {"name":"Steve Lewis", "nation":"USA", "result":"44.21"}, {"name":"Samson Kitur", "nation":"KEN", "result":"44.24"}, {"name":"Ian Morris", "nation":"TTO", "result":"44.25"}, {"name":"Roberto Hernandez Prendes", "nation":"CUB", "result":"44.52"}, {"name":"David Grindley", "nation":"GBR", "result":"44.75"}, {"name":"Ibrahim Ismail", "nation":"QAT", "result":"45.10"}, {"name":"Susumu Takano", "nation":"JPN", "result":"45.18"}, ]}, { "distanse":"100m", "place":"Atlanta", "year":"1996", "athlets":[ {"name":"Donovan Bailey", "nation":"CAN", "result":"09.84"}, {"name":"Frank Fredericks", "nation":"NAM", "result":"09.89"}, {"name":"Ato Boldon", "nation":"TTO", "result":"09.90"}, {"name":"Dennis Mitchell", "nation":"USA", "result":"09.99"}, {"name":"Michael Marsh", "nation":"USA", "result":"10.00"}, {"name":"Davidson Ezima", "nation":"NGR", "result":"10.14"}, {"name":"Michael Green", "nation":"JAM", "result":"10.16"}, ]}, { "distanse":"200m", "place":"Atlanta", "year":"1996", "athlets":[ {"name":"Michael Johnson", "nation":"USA", "result":"19.32"}, {"name":"Frank Fredericks", "nation":"NAM", "result":"19.68"}, {"name":"Ato Boldon", "nation":"TTO", "result":"19.80"}, {"name":"Obadele Thompson", "nation":"BRB", "result":"20.14"}, {"name":"Jeff Williams", "nation":"USA", "result":"20.17"}, {"name":"Ivan Garcia", "nation":"CUB", "result":"20.21"}, {"name":"Patrick Stevens", "nation":"BEL", "result":"20.27"}, {"name":"Michael Marsh", "nation":"USA", "result":"20.48"}, ]}, { "distanse":"400m", "place":"Atlanta", "year":"1996", "athlets":[ {"name":"Michael Johnson", "nation":"USA", "result":"43.49"}, {"name":"Roger Black", "nation":"GBR", "result":"44.41"}, {"name":"Davis Kamoga", "nation":"UGA", "result":"44.53"}, {"name":"Alvin Harrison", "nation":"USA", "result":"44.62"}, {"name":"Iwan Thomas", "nation":"GBR", "result":"44.70"}, {"name":"Roxbert Martin", "nation":"JAM", "result":"44.83"}, {"name":"Davian Clarke", "nation":"JAM", "result":"44.99"}, ]}, { "distanse":"100m", "place":"Sidney", "year":"2000", "athlets":[ {"name":"Maurice Greene", "nation":"USA", "result":"09.87"}, {"name":"Ato Boldon", "nation":"TTO", "result":"09.99"}, {"name":"Obadele Thompson", "nation":"BRB", "result":"10.04"}, {"name":"Dwain Chambers", "nation":"GBR", "result":"10.08"}, {"name":"Jonathan Drummond", "nation":"USA", "result":"10.09"}, {"name":"Darren Campbell", "nation":"GB", "result":"10.13"}, {"name":"Kim Collins", "nation":"StK", "result":"10.17"}, ]}, { "distanse":"200m", "place":"Sidney", "year":"2000", "athlets":[ {"name":"Konstantinos Kenteris", "nation":"GRE", "result":"20.09 "}, {"name":"Darren Campbell", "nation":"GBR", "result":"20.14 "}, {"name":"Ato Boldon", "nation":"TTO", "result":"20.20"}, {"name":"Obadele Thompson", "nation":"BRB", "result":"20.20"}, {"name":"Christian Malcolm", "nation":"GBR", "result":"20.23"}, {"name":"Claudinei Silva", "nation":"BRA", "result":"20.28"}, {"name":"Coby Miller", "nation":"USA", "result":"20.35 "}, {"name":"John Capel Jr", "nation":"USA", "result":"20.49"}, ]}, { "distanse":"400m", "place":"Sidney", "year":"2000", "athlets":[ {"name":"Michael Johnson", "nation":"USA", "result":"43.84"}, {"name":"Alvin Harrison", "nation":"USA", "result":"44.40"}, {"name":"Gregory Haughton", "nation":"JAM", "result":"44.70"}, {"name":"Sanderlei Parrela", "nation":"BRA", "result":"45.01"}, {"name":"Robert Mackowiak", "nation":"POL", "result":"45.14"}, {"name":"Hendrick Mokganyetsi", "nation":"SAF", "result":"45.26"}, {"name":"Antonio Pettigrew", "nation":"USA", "result":"45.42"}, {"name":"Danny Mcfarlane", "nation":"JAM", "result":"45.55"}, ]}, { "distanse":"100m", "place":"Athens", "year":"2004", "athlets":[ {"name":"Justin Gatlin", "nation":"USA", "result":"09.85"}, {"name":"Francis Obikwelu", "nation":"POR", "result":"09.86"}, {"name":"Maurice Greene", "nation":"USA", "result":"09.87"}, {"name":"Shawn Crawford", "nation":"USA", "result":"09.89"}, {"name":"Asafa Powell", "nation":"JAM", "result":"09.94"}, {"name":"Kim Collins", "nation":"KNA", "result":"10.00"}, {"name":"Obadele Thompson", "nation":"BRB", "result":"10.10"}, ]}, { "distanse":"200m", "place":"Athens", "year":"2004", "athlets":[ {"name":"Shawn Crawford", "nation":"USA", "result":"19.79"}, {"name":"Bernard Williams", "nation":"USA", "result":"20.01"}, {"name":"Justin Gatlin", "nation":"USA", "result":"20.03"}, {"name":"Frank Fredericks", "nation":"NAM", "result":"20.03"}, {"name":"Francis Obikwelu", "nation":"POR", "result":"20.14"}, {"name":"Stephane Buckland", "nation":"MUS", "result":"20.24"}, {"name":"Tobias Unger", "nation":"GER", "result":"20.64"}, ]}, { "distanse":"400m", "place":"Athens", "year":"2004", "athlets":[ {"name":"Jeremy Wariner", "nation":"USA", "result":"44.00"}, {"name":"Otis Harris", "nation":"USA", "result":"44.16"}, {"name":"Derrick Brew", "nation":"USA", "result":"44.42"}, {"name":"Alleyne Francique", "nation":"GRN", "result":"44.66"}, {"name":"Brandon Simpson", "nation":"JAM", "result":"44.76"}, {"name":"Davian Clarke", "nation":"JAM", "result":"44.83"}, {"name":"Leslie Djhone", "nation":"FRA", "result":"44.94"}, {"name":"Michael Blackwood", "nation":"JAM", "result":"45.55"}, ]}, { "distanse":"100m", "place":"Beijing", "year":"2008", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"09.69"}, {"name":"Richard Thompson", "nation":"TTO", "result":"09.89"}, {"name":"Walter Dix", "nation":"USA", "result":"09.91"}, {"name":"Churandy Martina", "nation":"ANT", "result":"09.93"}, {"name":"Asafa Powell", "nation":"JAM", "result":"09.95"}, {"name":"Michael Frater", "nation":"JAM", "result":"09.97"}, {"name":"Marc Burns", "nation":"TTO", "result":"10.01"}, {"name":"Darvis Patton", "nation":"USA", "result":"10.03"}, ]}, { "distanse":"200m", "place":"Beijing", "year":"2008", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"19.30"}, {"name":"Churandy Martina", "nation":"ANT", "result":"19.82"}, {"name":"Shawn Crawford", "nation":"USA", "result":"19.96"}, {"name":"Walter Dix", "nation":"USA", "result":"19.98"}, {"name":"Brian Dzingai", "nation":"NAM", "result":"20.22"}, {"name":"Christian Malcolm", "nation":"GBR", "result":"20.40"}, {"name":"Kim Collins", "nation":"KNA", "result":"20.59"}, ]}, { "distanse":"400m", "place":"Beijing", "year":"2008", "athlets":[ {"name":"LaShawn Merritt", "nation":"USA", "result":"43.75"}, {"name":"Jeremy Wariner", "nation":"USA", "result":"44.74"}, {"name":"David Neville", "nation":"USA", "result":"44.80"}, {"name":"Christopher Brown", "nation":"BAM", "result":"44.84"}, {"name":"Leslie Djhone", "nation":"FRA", "result":"45.11"}, {"name":"Martyn Rooney", "nation":"GBR", "result":"45.12"}, {"name":"Renny Quow", "nation":"TTO", "result":"45.22"}, {"name":"Johan Wissmann", "nation":"SWE", "result":"45.39"}, ]}, { "distanse":"100m", "place":"London", "year":"2012", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"09.63"}, {"name":"Yohan Blake", "nation":"JAM", "result":"09.75"}, {"name":"Justin Gatlin", "nation":"USA", "result":"09.79"}, {"name":"Tyson Gay", "nation":"USA", "result":"09.80"}, {"name":"Ryan Bailey", "nation":"USA", "result":"09.88"}, {"name":"Churandy Martina", "nation":"NED", "result":"09.94"}, {"name":"Richard Thompson", "nation":"TTO", "result":"09.98"}, {"name":"Asafa Powell", "nation":"JAM", "result":"11.99"}, ]}, { "distanse":"200m", "place":"London", "year":"2012", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"19.32"}, {"name":"Yohan Blake", "nation":"JAM", "result":"19.44"}, {"name":"Warren Weir", "nation":"JAM", "result":"19.84"}, {"name":"Wallace Spearmon", "nation":"USA", "result":"19.90"}, {"name":"Churandy Martina", "nation":"NED", "result":"20.00"}, {"name":"Christophe Lemaitre", "nation":"FRA", "result":"20.19"}, {"name":"Anaso Jobodwana", "nation":"SAF", "result":"20.57"}, {"name":"Alex Quinonez", "nation":"ECU", "result":"20.69"}, ]}, { "distanse":"400m", "place":"London", "year":"2012", "athlets":[ {"name":"Kirani James", "nation":"GRE", "result":"43.94"}, {"name":"Luguelin Santos", "nation":"DOM", "result":"44.46"}, {"name":"Lalonde Gordon", "nation":"TTO", "result":"44.52"}, {"name":"Chris Brown", "nation":"BAM", "result":"44.79"}, {"name":"Kevin Borlee", "nation":"BEL", "result":"44.81"}, {"name":"Jonathan Borlee", "nation":"BEL", "result":"44.83"}, {"name":"Demetrius Pinder", "nation":"BAM", "result":"44.98"}, {"name":"Steven Solomon", "nation":"AUS", "result":"45.14"}, ]}, { "distanse":"100m", "place":"Rio", "year":"2016", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"09.81"}, {"name":"Justin Gatlin", "nation":"USA", "result":"09.89"}, {"name":"Andre De Grasse", "nation":"CAN", "result":"09.91"}, {"name":"Yohan Blake", "nation":"JAM", "result":"09.93"}, {"name":"Akani Simbine", "nation":"ZAF", "result":"09.94"}, {"name":"Ben Youssef Meïté", "nation":"CIV", "result":"09.96"}, {"name":"Jimmy Vicaut", "nation":"FRA", "result":"10.04"}, {"name":"Trayvon Bromell", "nation":"USA", "result":"10.06"}, ]}, { "distanse":"200m", "place":"Rio", "year":"2016", "athlets":[ {"name":"Usain Bolt", "nation":"JAM", "result":"19.78"}, {"name":"Andre De Grasse", "nation":"CAN", "result":"20.02"}, {"name":"Christophe Lemaitre", "nation":"FRA", "result":"20.12"}, {"name":"Adam Gemili", "nation":"GBR", "result":"20.12"}, {"name":"Churandy Martina", "nation":"NED", "result":"20.13"}, {"name":"LaShawn Merritt", "nation":"USA", "result":"20.19"}, {"name":"Alonso Edward", "nation":"PAN", "result":"20.23"}, {"name":"Ramil Guliyev", "nation":"TUR", "result":"20.43"}, ]}, { "distanse":"400m", "place":"Rio", "year":"2016", "athlets":[ {"name":"Wayde van Niekerk", "nation":"ZAF", "result":"43.03"}, {"name":"Kirani James", "nation":"GRD", "result":"43.76"}, {"name":"LaShawn Merritt", "nation":"USA", "result":"43.85"}, {"name":"Machel Cedenio", "nation":"TTO", "result":"44.01"}, {"name":"Karabo Sibanda", "nation":"BWA", "result":"44.25"}, {"name":"Ali Khamis", "nation":"BHR", "result":"44.36"}, {"name":"Bralon Taplin", "nation":"GRD", "result":"44.45"}, {"name":"Matthew Hudson-Smith", "nation":"GBR", "result":"44.61"}, ]}, ]}
Javascriptet som leser og parser denne fila ser slik ut:
_olymp.js
//------------------------ // bs 2011:08 //------------------------ // globals // loaded json object var olympicObject=null; // where to place result var resultHolder=null; //--------------------------------------- // ajax access to jsonfile function getIt(address) { $.ajax({ url:address, type:'GET', async:false, dataType:'html', success:function(data,status,xhr) { var S=data; olympicObject=eval('(' + S + ')'); }, error:function(data) { alert('error'); } }); } //----------------- // display all function paintIt() { var oList=olympicObject.events; var T=''; var OL='x'; for(var ix=0;ix<oList.length;ix++) { var E=oList[ix]; if(E==null) //IE continue; var place=E.place; if(place != OL) { OL=place; T+='<h2><img src="'+OL+'.gif" alt="'+OL+'"/>'+OL+' - '+E.year+'</h2>'; } T+='\n<h3>'+E.distanse+'</h3>'; T+='<table>'; for(var ax=0;ax < E.athlets.length;ax++) { var A=E.athlets[ax]; if(A==null) //IE continue; T+='<tr>'; T+='<td>'+A.name+'</td>'+ '<td>'+A.nation+'</td>'+ '<td>'+A.result+'</td>'; T+='</tr>'; } T+='</table>'; } resultHolder.innerHTML=T; } //--------------------------------- // When page is loaded, or reloaded function initPage() { resultHolder=document.getElementById('resultholder'); getIt("output.txt"); paintIt(); }
Resultatet blir slik:
Dette er en svært enkel løsning uten noe forsøk på å sette stiler eller pen layout. HTML-fila som drar det hele er slik:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"/> <title>Olympiade</title> <script src="https://borres.hiof.no/resources/jslibs/jQ/jquery-min.js" type="text/javascript"> </script> <script src="olymp.js" > </script> </head> <body onload="initPage()"> <h1>Olympiader</h1> <div id="resultholder"></div> </body> </html>