annotate xslt/json.xsl @ 11:14162f9133a1

working on xml to json transform
author cin
date Sun, 08 Apr 2018 23:30:11 +0300
parents cbdada054b4a
children 191b81b2052b
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"
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
5 <xsl:output method="text" />
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
6
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
7 <xsl:template match="*[count(*) = 0]">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
8 <xsl:apply-templates mode="json-value" />
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
9 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
10
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
11 <xsl:template match="*[count(*) > 0]">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
12 <xsl:text>{ </xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
13 <xsl:apply-templates mode="json-object-member"/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
14 <xsl:text> }</xsl:text>
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
11
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
17 <xsl:template match="text()"/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
18
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
19 <!-- handle json-object -->
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
20
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
21 <xsl:template name="write-object">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
22
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
23 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
24
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
25 <xsl:template match="*" mode="json-object-member">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
26 <xsl:call-template name="write-json-string">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
27 <xsl:with-param name="text" select="local-name(.)"/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
28 </xsl:call-template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
29 <xsl:text> : </xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
30 <xsl:apply-templates mode="json-value"/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
31 <xsl:if test="position() != last()">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
32 <xsl:text>, </xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
33 </xsl:if>
0
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
34 </xsl:template>
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
35
11
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
36 <!-- handle json-value -->
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
37
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
38 <xsl:template match="*[@xsi:nil = 'true']">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
39 <xsl:text>null</xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
40 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
41
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
42 <xsl:template match="text()[. = 'true']" mode="json-value">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
43 <xsl:text>true</xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
44 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
45
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
46 <xsl:template match="text()[. = 'false']"
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
47 mode="json-value">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
48 <xsl:text>false</xsl:text>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
49 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
50
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
51 <xsl:template match="text()[string(number(.)) != 'NaN']"
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
52 mode="json-value">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
53 <xsl:value-of select="number(.)" />
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
54 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
55
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
56 <xsl:template match="text()" mode="json-value">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
57 <xsl:call-template name="write-json-string">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
58 <xsl:with-param name="text" select="."/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
59 </xsl:call-template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
60 </xsl:template>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
61
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
62 <xsl:template match="*" mode="json-value">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
63 <xsl:apply-templates select="."/>
0
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
64 </xsl:template>
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
65
11
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
66 <!-- template traits -->
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
67 <xsl:template name="write-json-string">
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
68 <xsl:param name="text"/>
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
69 <xsl:value-of select="concat('&quot;', $text,'&quot;')" />
0
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
70 </xsl:template>
11
14162f9133a1 working on xml to json transform
cin
parents: 0
diff changeset
71
0
cbdada054b4a Basic schemas for generating csharp internal dom from model definition
cin
parents:
diff changeset
72 </xsl:stylesheet>