Bonjour,
je voudrais convertir un document xml en document Json par une feuille XSLT pour aboutir au rendu JSON suivant :
{
"version": "1.0",
"encoding": "UTF-8",
"feed":
{
"xmlns": "http://www.w3.org/2005/Atom",
"xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
"id":
{
"value": "http://www.termsciences.fr/services/opensearch/search.php"
},
"updated":
{
"value": "2010-06-07T09:40:58Z"
},
"title":
{
"type": "text",
"value": "Recherche TermSciences : tomate"
},
"link":[
{
"rel": "self",
"type": "application/jsonrequest",
"href": "http://www.termsciences.fr/..."
},
{
"rel": "related",
"type": "application/opensearchdescription+xml",
"href": "http://www.termsciences.fr/services/opensearch/search.xml"
}],
"openSearch$startIndex":
{
"value": "1"
},
"openSearch$itemsPerPage":
{
"value": "10"
},
"openSearch$totalResults":
{
"value": "12"
},
"openSearch$Query":
{
"searchTerms": "tomate",
"startPage": "1"
},
"content":
{
"type": "text"
},
"entry":[
{
"id":
{
"value": "urn:termsciences:TE.80342"
},
"updated":
{
"value": "2006-06-13T00:00:00Z"
},
"title":
{
"type": "text",
"value": "Tomato"
},
"content":
{
"type": "text",
"value": "Tomato, Tomatoes"
},
"link":[
{
"type": "text/html",
"href": "http://www.termsciences.fr/-/Index/Rechercher/Rapide/Naviguer/Arbre/?idt=TE.80342"
}]
},
{
"id":
{
"value": "urn:termsciences:TE.84492"
},
"title":
{
"value": "Tomato black ring virus",
"type": "text"
},
"updated":
{
"value": "2006-04-05T00:00:00Z"
},
"link":[
{
"type": "text/html",
"href": "http://www.termsciences.fr/-/Index/Rechercher/Rapide/Naviguer/Arbre/?idt=TE.84492"
}],
"content":
{
"value": "Tomato black ring virus",
"type": "text"
}
}]
}
}
J'ai le document XML suivant :
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="xmlToJson.xsl" type="text/xsl" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
<id>http://www.termsciences.fr/services/opensearch/search.php</id>
<updated>2010-04-13T10:19:35Z</updated>
<title type="text">Recherche TermSciences : tomate</title>
<link rel="self" type="application/atom+xml" href="http://www.termsciences.fr/..."/>
<link rel="related" type="application/opensearchdescription+xml" href="http://www.termsciences.fr/services/opensearch/search.xml"/>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>10</openSearch:itemsPerPage>
<openSearch:totalResults>12</openSearch:totalResults>
<openSearch:Query searchTerms="tomate" startPage="1"/>
<content type="text"/>
<entry>
<id>urn:termsciences:TE.80342</id>
<updated>2006-06-13T00:00:00Z</updated>
<title type="text">Tomato</title>
<content type="text">Tomato, Tomatoes</content>
<link type="text/html" href="http://www.termsciences.fr/-/Index/Rechercher/Rapide/Naviguer/Arbre/?idt=TE.80342"/>
</entry>
<entry>
<id>urn:termsciences:TE.84492</id>
<title type="text">Tomato black ring virus</title>
<updated>2006-04-05T00:00:00Z</updated>
<link type="text/html" href="http://www.termsciences.fr/-/Index/Rechercher/Rapide/Naviguer/Arbre/?idt=TE.84492"/>
<content type="text">Tomato black ring virus</content>
</entry>
</feed>
et la feuille XSLT suivante :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" exclude-result-prefixes="ebl">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:param name="recursionCnt">0</xsl:param>
<xsl:param name="isLast">1</xsl:param>
<xsl:param name="inArray">0</xsl:param>
<xsl:if test="$recursionCnt=0">
<xsl:text>{</xsl:text>
</xsl:if>
<!-- test what type of data to output -->
<xsl:variable name="elementDataType">
<xsl:value-of select="number(text())"/>
</xsl:variable>
<xsl:variable name="elementData">
<!-- TEXT ( use quotes ) -->
<xsl:if test="string($elementDataType) ='NaN'">
<xsl:if test="boolean(text())">
<xsl:text/>"<xsl:value-of select="text()"/>"<xsl:text/>
</xsl:if>
</xsl:if>
<!-- NUMBER (no quotes ) -->
<xsl:if test="string($elementDataType) !='NaN'">
<xsl:text/><xsl:value-of select="text()"/><xsl:text/>
</xsl:if>
<!-- NULL -->
<xsl:if test="not(*)">
<xsl:if test="not(text())">
<xsl:text/>null<xsl:text/>
</xsl:if>
</xsl:if>
</xsl:variable>
<xsl:variable name="hasRepeatElements">
<xsl:for-each select="*">
<xsl:if test="name() = name(preceding-sibling::*) or name() = name(following-sibling::*)">
<xsl:text/>true<xsl:text/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not(count(@*) > 0)">
<xsl:text/>"<xsl:value-of select="local-name()"/>":<xsl:value-of select="$elementData"/><xsl:text/>
</xsl:if>
<xsl:if test="count(@*) > 0">
<xsl:text/>"<xsl:value-of select="local-name()"/>":{"value":<xsl:value-of select="$elementData"/><xsl:text/>
<xsl:for-each select="@*">
<xsl:if test="position()=1">,</xsl:if>
<!-- test what type of data to output -->
<xsl:variable name="dataType">
<xsl:text/><xsl:value-of select="number(.)"/><xsl:text/>
</xsl:variable>
<xsl:variable name="data">
<!-- TEXT ( use quotes ) -->
<xsl:if test="string($dataType) ='NaN'">
<xsl:text/>"<xsl:value-of select="current()"/>"<xsl:text/> </xsl:if>
<!-- NUMBER (no quotes ) -->
<xsl:if test="string($dataType) !='NaN'">
<xsl:text/><xsl:value-of select="current()"/><xsl:text/>
</xsl:if>
</xsl:variable>
<xsl:text/><xsl:value-of select="local-name()"/>:<xsl:value-of select="$data"/><xsl:text/>
<xsl:if test="position() !=last()">,</xsl:if>
</xsl:for-each>
<xsl:text/>}<xsl:text/>
</xsl:if>
<xsl:if test="not($hasRepeatElements = '')">
<xsl:text/>[{<xsl:text/>
</xsl:if>
<xsl:for-each select="*">
<xsl:if test="position()=1">
<xsl:if test="$hasRepeatElements = ''">
<xsl:text>{</xsl:text>
</xsl:if>
</xsl:if>
<xsl:apply-templates select="current()">
<xsl:with-param name="recursionCnt" select="$recursionCnt+1"/>
<xsl:with-param name="isLast" select="position()=last()"/>
<xsl:with-param name="inArray" select="not($hasRepeatElements = '')"/>
</xsl:apply-templates>
<xsl:if test="position()=last()">
<xsl:if test="$hasRepeatElements = ''">
<xsl:text>}</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:if test="not($hasRepeatElements = '')">
<xsl:text/>}]<xsl:text/>
</xsl:if>
<xsl:if test="not( $isLast )">
<xsl:if test="$inArray = 'true'">
<xsl:text>}</xsl:text>
</xsl:if>
<xsl:text/>,<xsl:text/>
<xsl:if test="$inArray = 'true'">
<xsl:text>{</xsl:text>
</xsl:if>
</xsl:if>
<xsl:if test="$recursionCnt=0">}</xsl:if>
</xsl:template>
</xsl:stylesheet>
Que dois-je modifier pour avoir le bon rendu json ?