Mercurial > pub > ImplabNet
comparison Implab/Xml/json.xsl @ 264:3a6e18c432be v3
Added XmlToJson xsl transformation.
Added JsonXmlReader.CreateJsonXmlReader(...) methods
Added SerializationHelpers.SerializeJson/DeserializeJson methods
author | cin |
---|---|
date | Mon, 16 Apr 2018 18:43:49 +0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
263:711572866e0c | 264:3a6e18c432be |
---|---|
1 <?xml version="1.0" encoding="UTF-8"?> | |
2 <xsl:stylesheet version="1.0" | |
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
5 xmlns:exsl="http://exslt.org/common"> | |
6 <xsl:output method="text" /> | |
7 | |
8 <xsl:template match="/"> | |
9 <xsl:apply-templates mode="json-value" /> | |
10 </xsl:template> | |
11 | |
12 | |
13 <!-- handle json-object --> | |
14 | |
15 <xsl:template match="*" mode="json-object"> | |
16 <xsl:call-template name="write-members"/> | |
17 </xsl:template> | |
18 | |
19 <xsl:template match="*|@*" mode="json-member"> | |
20 <xsl:param name="values" select="."/> | |
21 <xsl:call-template name="write-string"> | |
22 <xsl:with-param name="text"><xsl:apply-templates select="." mode="json-member-name"/></xsl:with-param> | |
23 </xsl:call-template> | |
24 <xsl:text> : </xsl:text> | |
25 <xsl:apply-templates select="." mode="json-member-value"> | |
26 <xsl:with-param name="values" select="$values"/> | |
27 </xsl:apply-templates> | |
28 </xsl:template> | |
29 | |
30 <xsl:template match="*" mode="json-member-name"> | |
31 <xsl:value-of select="local-name(.)"/> | |
32 </xsl:template> | |
33 | |
34 <xsl:template match="@*" mode="json-member-name"> | |
35 <xsl:value-of select="concat('_',local-name(.))"/> | |
36 </xsl:template> | |
37 | |
38 <xsl:template match="*|@*" mode="json-member-value"> | |
39 <xsl:param name="values" select="."/> | |
40 <xsl:choose> | |
41 <xsl:when test="count($values) > 1"> | |
42 <xsl:call-template name="write-array"> | |
43 <xsl:with-param name="values" select="$values"/> | |
44 </xsl:call-template> | |
45 </xsl:when> | |
46 <xsl:otherwise> | |
47 <xsl:apply-templates select="$values" mode="json-value"/> | |
48 </xsl:otherwise> | |
49 </xsl:choose> | |
50 </xsl:template> | |
51 | |
52 <xsl:template match="*|@*" mode="json-array-item"> | |
53 <xsl:apply-templates select="." mode="json-value"/> | |
54 <xsl:if test="position() != last()"> | |
55 <xsl:text>, </xsl:text> | |
56 </xsl:if> | |
57 </xsl:template> | |
58 | |
59 <!-- handle json-value --> | |
60 | |
61 <xsl:template match="text()[. = 'true'] | @*[. = 'true']" mode="json-value"> | |
62 <xsl:text>true</xsl:text> | |
63 </xsl:template> | |
64 | |
65 <xsl:template match="text()[. = 'false'] | @*[. = 'false']" | |
66 mode="json-value"> | |
67 <xsl:text>false</xsl:text> | |
68 </xsl:template> | |
69 | |
70 <xsl:template match="text()[string(number(.)) != 'NaN'] | @*[string(number(.)) != 'NaN']" | |
71 mode="json-value"> | |
72 <xsl:value-of select="number(.)" /> | |
73 </xsl:template> | |
74 | |
75 <xsl:template match="text()|@*" mode="json-value"> | |
76 <xsl:call-template name="write-string"> | |
77 <xsl:with-param name="text" select="."/> | |
78 </xsl:call-template> | |
79 </xsl:template> | |
80 | |
81 <xsl:template match="*[boolean(* | @*) or not(text())]" mode="json-value"> | |
82 <xsl:call-template name="write-object"/> | |
83 </xsl:template> | |
84 | |
85 <xsl:template match="*[@xsi:nil = 'true']" mode="json-value"> | |
86 <xsl:text>null</xsl:text> | |
87 </xsl:template> | |
88 | |
89 <!-- template traits --> | |
90 | |
91 <xsl:template name="write-value"> | |
92 <xsl:param name="value" select="."/> | |
93 <xsl:apply-templates select="$value" mode="json-value"/> | |
94 </xsl:template> | |
95 | |
96 <xsl:template name="write-member"> | |
97 <xsl:param name="name"/> | |
98 <xsl:param name="value"/> | |
99 <xsl:call-template name="write-string"> | |
100 <xsl:with-param name="text" select="$name"/> | |
101 </xsl:call-template> | |
102 <xsl:text> : </xsl:text> | |
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> | |
132 </xsl:template> | |
133 | |
134 <!-- specialized template traits --> | |
135 | |
136 <xsl:template name="write-string"> | |
137 <xsl:param name="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> | |
143 </xsl:template> | |
144 | |
145 <xsl:template name="write-object"> | |
146 <xsl:param name="value" select="."/> | |
147 <xsl:text>{ </xsl:text> | |
148 <xsl:apply-templates select="$value" mode="json-object"/> | |
149 <xsl:text> }</xsl:text> | |
150 </xsl:template> | |
151 | |
152 <xsl:template name="write-array"> | |
153 <xsl:param name="values"/> | |
154 | |
155 <xsl:text>[ </xsl:text> | |
156 <xsl:apply-templates select="$values" mode="json-array-item"/> | |
157 <xsl:text> ]</xsl:text> | |
158 </xsl:template> | |
159 | |
160 <xsl:template name="write-members"> | |
161 <xsl:param name="members" select="*"/> | |
162 | |
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> | |
174 </xsl:for-each> | |
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> | |
251 | |
252 </xsl:stylesheet> |