Mercurial > pub > ModelGenerator
comparison 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 |
comparison
equal
deleted
inserted
replaced
10:19a8a71eaa54 | 11:14162f9133a1 |
---|---|
1 <?xml version="1.0" encoding="UTF-8"?> | 1 <?xml version="1.0" encoding="UTF-8"?> |
2 <xsl:stylesheet version="1.0" | 2 <xsl:stylesheet version="1.0" |
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
4 <xsl:output method="text"/> | 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
5 | 5 <xsl:output method="text" /> |
6 <xsl:template match="/"> | 6 |
7 <xsl:apply-templates mode="json-array" /> | 7 <xsl:template match="*[count(*) = 0]"> |
8 <xsl:apply-templates mode="json-value" /> | |
9 </xsl:template> | |
10 | |
11 <xsl:template match="*[count(*) > 0]"> | |
12 <xsl:text>{ </xsl:text> | |
13 <xsl:apply-templates mode="json-object-member"/> | |
14 <xsl:text> }</xsl:text> | |
8 </xsl:template> | 15 </xsl:template> |
9 | 16 |
10 <xsl:template match="*" mode="json-array"> | 17 <xsl:template match="text()"/> |
11 [ | 18 |
12 <xsl:for-each select="*"> | 19 <!-- handle json-object --> |
13 <xsl:apply-templates select="." mode="json-value"/> | 20 |
14 <xsl:if test="position() != last()">,</xsl:if> | 21 <xsl:template name="write-object"> |
15 </xsl:for-each> | 22 |
16 ] | 23 </xsl:template> |
24 | |
25 <xsl:template match="*" mode="json-object-member"> | |
26 <xsl:call-template name="write-json-string"> | |
27 <xsl:with-param name="text" select="local-name(.)"/> | |
28 </xsl:call-template> | |
29 <xsl:text> : </xsl:text> | |
30 <xsl:apply-templates mode="json-value"/> | |
31 <xsl:if test="position() != last()"> | |
32 <xsl:text>, </xsl:text> | |
33 </xsl:if> | |
17 </xsl:template> | 34 </xsl:template> |
18 | 35 |
19 <xsl:template match="*[count(*) != 0]" mode="json-array-item"> | 36 <!-- handle json-value --> |
20 "<xsl:value-of select="name()"/> : <xsl:apply-templates mode="json-value"/>" | 37 |
21 <xsl:if test="position() != last()">,</xsl:if> | 38 <xsl:template match="*[@xsi:nil = 'true']"> |
39 <xsl:text>null</xsl:text> | |
40 </xsl:template> | |
41 | |
42 <xsl:template match="text()[. = 'true']" mode="json-value"> | |
43 <xsl:text>true</xsl:text> | |
44 </xsl:template> | |
45 | |
46 <xsl:template match="text()[. = 'false']" | |
47 mode="json-value"> | |
48 <xsl:text>false</xsl:text> | |
49 </xsl:template> | |
50 | |
51 <xsl:template match="text()[string(number(.)) != 'NaN']" | |
52 mode="json-value"> | |
53 <xsl:value-of select="number(.)" /> | |
54 </xsl:template> | |
55 | |
56 <xsl:template match="text()" mode="json-value"> | |
57 <xsl:call-template name="write-json-string"> | |
58 <xsl:with-param name="text" select="."/> | |
59 </xsl:call-template> | |
60 </xsl:template> | |
61 | |
62 <xsl:template match="*" mode="json-value"> | |
63 <xsl:apply-templates select="."/> | |
22 </xsl:template> | 64 </xsl:template> |
23 | 65 |
24 <xsl:template match="*" mode="json-value"> | 66 <!-- template traits --> |
25 <xsl:choose> | 67 <xsl:template name="write-json-string"> |
26 <xsl:when test="count(*) != 0">{ <xsl:apply-templates mode="json-named-value" /> }</xsl:when> | 68 <xsl:param name="text"/> |
27 <xsl:when test="text() = 'true'">true</xsl:when> | 69 <xsl:value-of select="concat('"', $text,'"')" /> |
28 <xsl:when test="text() = 'false'">false</xsl:when> | |
29 <xsl:when test="text() = 'null'">null</xsl:when> | |
30 <xsl:when test="string(number(.)) != 'NaN'"><xsl:value-of select="number(.)"/></xsl:when> | |
31 <xsl:otherwise>"<xsl:value-of select="text()"/>"</xsl:otherwise> | |
32 </xsl:choose> | |
33 </xsl:template> | 70 </xsl:template> |
34 | 71 |
35 <xsl:template match="*" mode="json-named-value"> | |
36 <xsl:value-of select="name()"/> : <xsl:apply-templates select="." mode="json-value"/> | |
37 </xsl:template> | |
38 </xsl:stylesheet> | 72 </xsl:stylesheet> |