Mercurial > pub > ModelGenerator
annotate xslt/json.xsl @ 12:191b81b2052b
first version of the xslt transform from xml to json
author | cin |
---|---|
date | Mon, 09 Apr 2018 06:43:46 +0300 |
parents | 14162f9133a1 |
children | 197a850b1f6f |
rev | line source |
---|---|
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
1 <?xml version="1.0" encoding="UTF-8"?> |
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
2 <xsl:stylesheet version="1.0" |
11 | 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
12 | 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
5 xmlns:exsl="http://exslt.org/common"> | |
11 | 6 <xsl:output method="text" /> |
12 | 7 |
8 <xsl:key name="members" match="*" use="concat(generate-id(..),local-name(.))"/> | |
11 | 9 |
12 | 10 <xsl:template match="/"> |
11 <xsl:variable name="doc"> | |
12 <xsl:apply-templates/> | |
13 </xsl:variable> | |
14 <xsl:apply-templates select="exsl:node-set($doc)" mode="json-value" /> | |
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
15 </xsl:template> |
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
16 |
12 | 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 | |
11 | 28 <!-- handle json-object --> |
29 | |
12 | 30 <xsl:template match="*" mode="json-object-members"> |
31 <xsl:apply-templates select="." mode="json-object-members-internal"/> | |
11 | 32 </xsl:template> |
12 | 33 |
34 <xsl:template match="*" mode="json-object-members-internal"> | |
35 <xsl:variable name="oid" select="generate-id(.)"/> | |
36 <xsl:variable name="grouped" select="*[generate-id(.) = generate-id(key('members',concat($oid,local-name(.))))]"/> | |
37 <xsl:apply-templates select="$grouped" mode="json-object-member"/> | |
38 </xsl:template> | |
39 | |
11 | 40 <xsl:template match="*" mode="json-object-member"> |
12 | 41 <xsl:call-template name="write-string"> |
11 | 42 <xsl:with-param name="text" select="local-name(.)"/> |
43 </xsl:call-template> | |
44 <xsl:text> : </xsl:text> | |
12 | 45 <xsl:apply-templates select="." mode="json-member-value"/> |
46 <xsl:if test="position() != last()"> | |
47 <xsl:text>, </xsl:text> | |
48 </xsl:if> | |
49 </xsl:template> | |
50 | |
51 <xsl:template match="*" mode="json-member-value"> | |
52 <xsl:variable name="oid" select="generate-id(..)"/> | |
53 <xsl:variable name="values" select="key('members',concat($oid,local-name(.)))"/> | |
54 <xsl:choose> | |
55 <xsl:when test="count($values) > 1"> | |
56 <xsl:call-template name="write-array"> | |
57 <xsl:with-param name="values" select="$values"/> | |
58 </xsl:call-template> | |
59 </xsl:when> | |
60 <xsl:otherwise> | |
61 <xsl:apply-templates select="$values" mode="json-value"/> | |
62 </xsl:otherwise> | |
63 </xsl:choose> | |
64 </xsl:template> | |
65 | |
66 <xsl:template match="*" mode="json-array-item"> | |
67 <xsl:apply-templates select="." mode="json-value"/> | |
11 | 68 <xsl:if test="position() != last()"> |
69 <xsl:text>, </xsl:text> | |
70 </xsl:if> | |
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
71 </xsl:template> |
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
72 |
11 | 73 <!-- handle json-value --> |
74 | |
75 <xsl:template match="text()[. = 'true']" mode="json-value"> | |
76 <xsl:text>true</xsl:text> | |
77 </xsl:template> | |
78 | |
79 <xsl:template match="text()[. = 'false']" | |
80 mode="json-value"> | |
81 <xsl:text>false</xsl:text> | |
82 </xsl:template> | |
83 | |
84 <xsl:template match="text()[string(number(.)) != 'NaN']" | |
85 mode="json-value"> | |
86 <xsl:value-of select="number(.)" /> | |
87 </xsl:template> | |
88 | |
89 <xsl:template match="text()" mode="json-value"> | |
12 | 90 <xsl:call-template name="write-string"> |
11 | 91 <xsl:with-param name="text" select="."/> |
92 </xsl:call-template> | |
93 </xsl:template> | |
94 | |
12 | 95 <xsl:template match="*[boolean(* | @*) or not(text())]" mode="json-value"> |
96 <xsl:call-template name="write-object"/> | |
97 </xsl:template> | |
98 | |
99 <xsl:template match="*[@xsi:nil = 'true']" mode="json-value"> | |
100 <xsl:text>null</xsl:text> | |
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
101 </xsl:template> |
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
102 |
11 | 103 <!-- template traits --> |
12 | 104 |
105 <xsl:template name="write-value"> | |
106 <xsl:param name="value" select="."/> | |
107 <xsl:apply-templates select="exsl:node-set($value)" mode="json-value"/> | |
108 </xsl:template> | |
109 | |
110 <xsl:template name="write-named-value"> | |
111 <xsl:param name="name"/> | |
112 <xsl:param name="value"/> | |
113 <xsl:call-template name="write-string"> | |
114 <xsl:with-param name="text" select="$name"/> | |
115 </xsl:call-template> | |
116 <xsl:text> : </xsl:text> | |
117 <xsl:apply-templates select="exsl:node-set($value)"/> | |
118 </xsl:template> | |
119 | |
120 <!-- specialized template traits --> | |
121 | |
122 <xsl:template name="write-string"> | |
11 | 123 <xsl:param name="text"/> |
124 <xsl:value-of select="concat('"', $text,'"')" /> | |
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
125 </xsl:template> |
12 | 126 |
127 <xsl:template name="write-object"> | |
128 <xsl:param name="value" select="."/> | |
129 <xsl:text>{ </xsl:text> | |
130 <xsl:apply-templates select="$value" mode="json-object-members"/> | |
131 <xsl:text> }</xsl:text> | |
132 </xsl:template> | |
133 | |
134 <xsl:template name="write-array"> | |
135 <xsl:param name="oid" select="generate-id(..)"/> | |
136 <xsl:param name="values" select="key('members',concat($oid,local-name(.)))"/> | |
137 | |
138 <xsl:text>[ </xsl:text> | |
139 <xsl:apply-templates select="$values" mode="json-array-item"/> | |
140 <xsl:text> ]</xsl:text> | |
141 </xsl:template> | |
142 | |
143 <xsl:template name="write-object-members"> | |
144 <xsl:param name="object" select="."/> | |
145 <xsl:for-each select="exsl:node-set($object)"> | |
146 <xsl:apply-templates select="." mode="json-object-members-internal"/> | |
147 </xsl:for-each> | |
148 </xsl:template> | |
11 | 149 |
0
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff
changeset
|
150 </xsl:stylesheet> |