Mercurial > pub > ModelGenerator
comparison xslt/json.xsl @ 13:197a850b1f6f default tip
working version of xml2json transformation
author | cin |
---|---|
date | Mon, 09 Apr 2018 16:27:26 +0300 |
parents | 191b81b2052b |
children |
comparison
equal
deleted
inserted
replaced
12:191b81b2052b | 13:197a850b1f6f |
---|---|
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
5 xmlns:exsl="http://exslt.org/common"> | 5 xmlns:exsl="http://exslt.org/common"> |
6 <xsl:output method="text" /> | 6 <xsl:output method="text" /> |
7 | 7 |
8 <xsl:key name="members" match="*" use="concat(generate-id(..),local-name(.))"/> | |
9 | |
10 <xsl:template match="/"> | 8 <xsl:template match="/"> |
11 <xsl:variable name="doc"> | 9 <xsl:apply-templates mode="json-value" /> |
12 <xsl:apply-templates/> | 10 </xsl:template> |
13 </xsl:variable> | 11 |
14 <xsl:apply-templates select="exsl:node-set($doc)" mode="json-value" /> | |
15 </xsl:template> | |
16 | |
17 <xsl:template match="*"> | |
18 <xsl:copy> | |
19 <xsl:apply-templates select="@*"/> | |
20 <xsl:apply-templates/> | |
21 </xsl:copy> | |
22 </xsl:template> | |
23 | |
24 <xsl:template match="@*"> | |
25 <xsl:copy/> | |
26 </xsl:template> | |
27 | 12 |
28 <!-- handle json-object --> | 13 <!-- handle json-object --> |
29 | 14 |
30 <xsl:template match="*" mode="json-object-members"> | 15 <xsl:template match="*" mode="json-object"> |
31 <xsl:apply-templates select="." mode="json-object-members-internal"/> | 16 <xsl:call-template name="write-members"/> |
32 </xsl:template> | 17 </xsl:template> |
33 | 18 |
34 <xsl:template match="*" mode="json-object-members-internal"> | 19 <xsl:template match="*|@*" mode="json-member"> |
35 <xsl:variable name="oid" select="generate-id(.)"/> | 20 <xsl:param name="values" select="."/> |
36 <xsl:variable name="grouped" select="*[generate-id(.) = generate-id(key('members',concat($oid,local-name(.))))]"/> | 21 <xsl:call-template name="write-string"> |
37 <xsl:apply-templates select="$grouped" mode="json-object-member"/> | 22 <xsl:with-param name="text"><xsl:apply-templates select="." mode="json-member-name"/></xsl:with-param> |
38 </xsl:template> | 23 </xsl:call-template> |
39 | 24 <xsl:text> : </xsl:text> |
40 <xsl:template match="*" mode="json-object-member"> | 25 <xsl:apply-templates select="." mode="json-member-value"> |
41 <xsl:call-template name="write-string"> | 26 <xsl:with-param name="values" select="$values"/> |
42 <xsl:with-param name="text" select="local-name(.)"/> | 27 </xsl:apply-templates> |
43 </xsl:call-template> | 28 </xsl:template> |
44 <xsl:text> : </xsl:text> | 29 |
45 <xsl:apply-templates select="." mode="json-member-value"/> | 30 <xsl:template match="*" mode="json-member-name"> |
46 <xsl:if test="position() != last()"> | 31 <xsl:value-of select="local-name(.)"/> |
47 <xsl:text>, </xsl:text> | 32 </xsl:template> |
48 </xsl:if> | 33 |
49 </xsl:template> | 34 <xsl:template match="@*" mode="json-member-name"> |
50 | 35 <xsl:value-of select="concat('_',local-name(.))"/> |
51 <xsl:template match="*" mode="json-member-value"> | 36 </xsl:template> |
52 <xsl:variable name="oid" select="generate-id(..)"/> | 37 |
53 <xsl:variable name="values" select="key('members',concat($oid,local-name(.)))"/> | 38 <xsl:template match="*|@*" mode="json-member-value"> |
39 <xsl:param name="values" select="."/> | |
54 <xsl:choose> | 40 <xsl:choose> |
55 <xsl:when test="count($values) > 1"> | 41 <xsl:when test="count($values) > 1"> |
56 <xsl:call-template name="write-array"> | 42 <xsl:call-template name="write-array"> |
57 <xsl:with-param name="values" select="$values"/> | 43 <xsl:with-param name="values" select="$values"/> |
58 </xsl:call-template> | 44 </xsl:call-template> |
61 <xsl:apply-templates select="$values" mode="json-value"/> | 47 <xsl:apply-templates select="$values" mode="json-value"/> |
62 </xsl:otherwise> | 48 </xsl:otherwise> |
63 </xsl:choose> | 49 </xsl:choose> |
64 </xsl:template> | 50 </xsl:template> |
65 | 51 |
66 <xsl:template match="*" mode="json-array-item"> | 52 <xsl:template match="*|@*" mode="json-array-item"> |
67 <xsl:apply-templates select="." mode="json-value"/> | 53 <xsl:apply-templates select="." mode="json-value"/> |
68 <xsl:if test="position() != last()"> | 54 <xsl:if test="position() != last()"> |
69 <xsl:text>, </xsl:text> | 55 <xsl:text>, </xsl:text> |
70 </xsl:if> | 56 </xsl:if> |
71 </xsl:template> | 57 </xsl:template> |
72 | 58 |
73 <!-- handle json-value --> | 59 <!-- handle json-value --> |
74 | 60 |
75 <xsl:template match="text()[. = 'true']" mode="json-value"> | 61 <xsl:template match="text()[. = 'true'] | @*[. = 'true']" mode="json-value"> |
76 <xsl:text>true</xsl:text> | 62 <xsl:text>true</xsl:text> |
77 </xsl:template> | 63 </xsl:template> |
78 | 64 |
79 <xsl:template match="text()[. = 'false']" | 65 <xsl:template match="text()[. = 'false'] | @*[. = 'false']" |
80 mode="json-value"> | 66 mode="json-value"> |
81 <xsl:text>false</xsl:text> | 67 <xsl:text>false</xsl:text> |
82 </xsl:template> | 68 </xsl:template> |
83 | 69 |
84 <xsl:template match="text()[string(number(.)) != 'NaN']" | 70 <xsl:template match="text()[string(number(.)) != 'NaN'] | @*[string(number(.)) != 'NaN']" |
85 mode="json-value"> | 71 mode="json-value"> |
86 <xsl:value-of select="number(.)" /> | 72 <xsl:value-of select="number(.)" /> |
87 </xsl:template> | 73 </xsl:template> |
88 | 74 |
89 <xsl:template match="text()" mode="json-value"> | 75 <xsl:template match="text()|@*" mode="json-value"> |
90 <xsl:call-template name="write-string"> | 76 <xsl:call-template name="write-string"> |
91 <xsl:with-param name="text" select="."/> | 77 <xsl:with-param name="text" select="."/> |
92 </xsl:call-template> | 78 </xsl:call-template> |
93 </xsl:template> | 79 </xsl:template> |
94 | 80 |
102 | 88 |
103 <!-- template traits --> | 89 <!-- template traits --> |
104 | 90 |
105 <xsl:template name="write-value"> | 91 <xsl:template name="write-value"> |
106 <xsl:param name="value" select="."/> | 92 <xsl:param name="value" select="."/> |
107 <xsl:apply-templates select="exsl:node-set($value)" mode="json-value"/> | 93 <xsl:apply-templates select="$value" mode="json-value"/> |
108 </xsl:template> | 94 </xsl:template> |
109 | 95 |
110 <xsl:template name="write-named-value"> | 96 <xsl:template name="write-member"> |
111 <xsl:param name="name"/> | 97 <xsl:param name="name"/> |
112 <xsl:param name="value"/> | 98 <xsl:param name="value"/> |
113 <xsl:call-template name="write-string"> | 99 <xsl:call-template name="write-string"> |
114 <xsl:with-param name="text" select="$name"/> | 100 <xsl:with-param name="text" select="$name"/> |
115 </xsl:call-template> | 101 </xsl:call-template> |
116 <xsl:text> : </xsl:text> | 102 <xsl:text> : </xsl:text> |
117 <xsl:apply-templates select="exsl:node-set($value)"/> | 103 <xsl:apply-templates select="$value" mode="json-value"/> |
104 </xsl:template> | |
105 | |
106 <xsl:template name="write-member-string"> | |
107 <xsl:param name="name"/> | |
108 <xsl:param name="value"/> | |
109 <xsl:call-template name="write-string"> | |
110 <xsl:with-param name="text" select="$name"/> | |
111 </xsl:call-template> | |
112 <xsl:text> : </xsl:text> | |
113 <xsl:call-template name="write-string"> | |
114 <xsl:with-param name="text" select="$value"/> | |
115 </xsl:call-template> | |
116 </xsl:template> | |
117 | |
118 <xsl:template name="write-member-array"> | |
119 <xsl:param name="name"/> | |
120 <xsl:param name="values"/> | |
121 <xsl:call-template name="write-string"> | |
122 <xsl:with-param name="text" select="$name"/> | |
123 </xsl:call-template> | |
124 <xsl:text> : </xsl:text> | |
125 <xsl:call-template name="write-array"> | |
126 <xsl:with-param name="values" select="$values"/> | |
127 </xsl:call-template> | |
128 </xsl:template> | |
129 | |
130 <xsl:template name="write-separator"> | |
131 <xsl:text>, </xsl:text> | |
118 </xsl:template> | 132 </xsl:template> |
119 | 133 |
120 <!-- specialized template traits --> | 134 <!-- specialized template traits --> |
121 | 135 |
122 <xsl:template name="write-string"> | 136 <xsl:template name="write-string"> |
123 <xsl:param name="text"/> | 137 <xsl:param name="text"/> |
124 <xsl:value-of select="concat('"', $text,'"')" /> | 138 <xsl:text>"</xsl:text> |
139 <xsl:call-template name="escape-bs-string"> | |
140 <xsl:with-param name="s" select="$text"/> | |
141 </xsl:call-template> | |
142 <xsl:text>"</xsl:text> | |
125 </xsl:template> | 143 </xsl:template> |
126 | 144 |
127 <xsl:template name="write-object"> | 145 <xsl:template name="write-object"> |
128 <xsl:param name="value" select="."/> | 146 <xsl:param name="value" select="."/> |
129 <xsl:text>{ </xsl:text> | 147 <xsl:text>{ </xsl:text> |
130 <xsl:apply-templates select="$value" mode="json-object-members"/> | 148 <xsl:apply-templates select="$value" mode="json-object"/> |
131 <xsl:text> }</xsl:text> | 149 <xsl:text> }</xsl:text> |
132 </xsl:template> | 150 </xsl:template> |
133 | 151 |
134 <xsl:template name="write-array"> | 152 <xsl:template name="write-array"> |
135 <xsl:param name="oid" select="generate-id(..)"/> | 153 <xsl:param name="values"/> |
136 <xsl:param name="values" select="key('members',concat($oid,local-name(.)))"/> | |
137 | 154 |
138 <xsl:text>[ </xsl:text> | 155 <xsl:text>[ </xsl:text> |
139 <xsl:apply-templates select="$values" mode="json-array-item"/> | 156 <xsl:apply-templates select="$values" mode="json-array-item"/> |
140 <xsl:text> ]</xsl:text> | 157 <xsl:text> ]</xsl:text> |
141 </xsl:template> | 158 </xsl:template> |
142 | 159 |
143 <xsl:template name="write-object-members"> | 160 <xsl:template name="write-members"> |
144 <xsl:param name="object" select="."/> | 161 <xsl:param name="members" select="*"/> |
145 <xsl:for-each select="exsl:node-set($object)"> | 162 |
146 <xsl:apply-templates select="." mode="json-object-members-internal"/> | 163 <xsl:for-each select="$members"> |
164 <xsl:variable name="current" select="."/> | |
165 <xsl:variable name="values" select="$members[local-name(.) = local-name($current)]"/> | |
166 <xsl:if test="generate-id($current) = generate-id($values)"> | |
167 <xsl:if test="position()>1"> | |
168 <xsl:call-template name="write-separator"/> | |
169 </xsl:if> | |
170 <xsl:apply-templates select="$current" mode="json-member"> | |
171 <xsl:with-param name="values" select="$values"/> | |
172 </xsl:apply-templates> | |
173 </xsl:if> | |
147 </xsl:for-each> | 174 </xsl:for-each> |
148 </xsl:template> | 175 </xsl:template> |
176 | |
177 <!-- escape string --> | |
178 <!-- | |
179 Copyright (c) 2006,2008 Doeke Zanstra | |
180 All rights reserved. | |
181 https://github.com/doekman/xml2json-xslt/blob/master/xml2json.xslt | |
182 --> | |
183 <!-- Escape the backslash (\) before everything else. --> | |
184 <xsl:template name="escape-bs-string"> | |
185 <xsl:param name="s"/> | |
186 <xsl:choose> | |
187 <xsl:when test="contains($s,'\')"> | |
188 <xsl:call-template name="escape-quot-string"> | |
189 <xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/> | |
190 </xsl:call-template> | |
191 <xsl:call-template name="escape-bs-string"> | |
192 <xsl:with-param name="s" select="substring-after($s,'\')"/> | |
193 </xsl:call-template> | |
194 </xsl:when> | |
195 <xsl:otherwise> | |
196 <xsl:call-template name="escape-quot-string"> | |
197 <xsl:with-param name="s" select="$s"/> | |
198 </xsl:call-template> | |
199 </xsl:otherwise> | |
200 </xsl:choose> | |
201 </xsl:template> | |
202 | |
203 <!-- Escape the double quote ("). --> | |
204 <xsl:template name="escape-quot-string"> | |
205 <xsl:param name="s"/> | |
206 <xsl:choose> | |
207 <xsl:when test="contains($s,'"')"> | |
208 <xsl:call-template name="encode-string"> | |
209 <xsl:with-param name="s" select="concat(substring-before($s,'"'),'\"')"/> | |
210 </xsl:call-template> | |
211 <xsl:call-template name="escape-quot-string"> | |
212 <xsl:with-param name="s" select="substring-after($s,'"')"/> | |
213 </xsl:call-template> | |
214 </xsl:when> | |
215 <xsl:otherwise> | |
216 <xsl:call-template name="encode-string"> | |
217 <xsl:with-param name="s" select="$s"/> | |
218 </xsl:call-template> | |
219 </xsl:otherwise> | |
220 </xsl:choose> | |
221 </xsl:template> | |
222 | |
223 <!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash | |
224 or double quote here, because they don't replace characters (� becomes \t), but they prefix | |
225 characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be | |
226 processed first. This function can't do that. --> | |
227 <xsl:template name="encode-string"> | |
228 <xsl:param name="s"/> | |
229 <xsl:choose> | |
230 <!-- tab --> | |
231 <xsl:when test="contains($s,'	')"> | |
232 <xsl:call-template name="encode-string"> | |
233 <xsl:with-param name="s" select="concat(substring-before($s,'	'),'\t',substring-after($s,'	'))"/> | |
234 </xsl:call-template> | |
235 </xsl:when> | |
236 <!-- line feed --> | |
237 <xsl:when test="contains($s,'
')"> | |
238 <xsl:call-template name="encode-string"> | |
239 <xsl:with-param name="s" select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/> | |
240 </xsl:call-template> | |
241 </xsl:when> | |
242 <!-- carriage return --> | |
243 <xsl:when test="contains($s,'
')"> | |
244 <xsl:call-template name="encode-string"> | |
245 <xsl:with-param name="s" select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/> | |
246 </xsl:call-template> | |
247 </xsl:when> | |
248 <xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise> | |
249 </xsl:choose> | |
250 </xsl:template> | |
149 | 251 |
150 </xsl:stylesheet> | 252 </xsl:stylesheet> |