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