Bonjour à tous, je voudrais convertir un document XML en document JSON par l'intermédiaire d'une feuille XSLT. Et en fait après un premier jet de feuille XSLT je n'arrive pas à savoir quoi modifier pour aboutir au résultat suivant :
J'ai donc le document XML suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 { "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/services/opensearch/search.php?query%3Dtomate%26lang%3Den%26field%3DTERMFR%26step%3D10%26start%3D1%26input-encoding%3DUTF-8" }, { "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/services/urls/selection/?idt=TE.80342&lng=en" }] }, { "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/services/urls/selection/?idt=TE.84492&lng=en" }], "content": { "value": "Tomato black ring virus", "type": "text" } }] } }
et la feuille XSLT suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <?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/services/opensearch/search.php?query%3Dtomate%26lang%3Den%26field%3DTERMFR%26step%3D10%26start%3D1%26input-encoding%3DUTF-8"/> <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/services/urls/selection/?idt=TE.80342&lng=en"/> </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/services/urls/selection/?idt=TE.84492&lng=en"/> <content type="text">Tomato black ring virus</content> </entry> </feed>
Que dois-je modifier ??
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 <?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>
Merci d'avance
Partager