Mercurial > pub > ModelGenerator
annotate xslt/json.xsl @ 13:197a850b1f6f default tip
working version of xml2json transformation
| author | cin | 
|---|---|
| date | Mon, 09 Apr 2018 16:27:26 +0300 | 
| parents | 191b81b2052b | 
| children | 
| 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:template match="/"> | |
| 13 | 9 <xsl:apply-templates mode="json-value" /> | 
| 
0
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
10 </xsl:template> | 
| 
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
11 | 
| 12 | 12 | 
| 11 | 13 <!-- handle json-object --> | 
| 14 | |
| 13 | 15 <xsl:template match="*" mode="json-object"> | 
| 16 <xsl:call-template name="write-members"/> | |
| 12 | 17 </xsl:template> | 
| 18 | |
| 13 | 19 <xsl:template match="*|@*" mode="json-member"> | 
| 20 <xsl:param name="values" select="."/> | |
| 12 | 21 <xsl:call-template name="write-string"> | 
| 13 | 22 <xsl:with-param name="text"><xsl:apply-templates select="." mode="json-member-name"/></xsl:with-param> | 
| 11 | 23 </xsl:call-template> | 
| 24 <xsl:text> : </xsl:text> | |
| 13 | 25 <xsl:apply-templates select="." mode="json-member-value"> | 
| 26 <xsl:with-param name="values" select="$values"/> | |
| 27 </xsl:apply-templates> | |
| 12 | 28 </xsl:template> | 
| 29 | |
| 13 | 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="."/> | |
| 12 | 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 | |
| 13 | 52 <xsl:template match="*|@*" mode="json-array-item"> | 
| 12 | 53 <xsl:apply-templates select="." mode="json-value"/> | 
| 11 | 54 <xsl:if test="position() != last()"> | 
| 55 <xsl:text>, </xsl:text> | |
| 56 </xsl:if> | |
| 
0
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
57 </xsl:template> | 
| 
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
58 | 
| 11 | 59 <!-- handle json-value --> | 
| 60 | |
| 13 | 61 <xsl:template match="text()[. = 'true'] | @*[. = 'true']" mode="json-value"> | 
| 11 | 62 <xsl:text>true</xsl:text> | 
| 63 </xsl:template> | |
| 64 | |
| 13 | 65 <xsl:template match="text()[. = 'false'] | @*[. = 'false']" | 
| 11 | 66 mode="json-value"> | 
| 67 <xsl:text>false</xsl:text> | |
| 68 </xsl:template> | |
| 69 | |
| 13 | 70 <xsl:template match="text()[string(number(.)) != 'NaN'] | @*[string(number(.)) != 'NaN']" | 
| 11 | 71 mode="json-value"> | 
| 72 <xsl:value-of select="number(.)" /> | |
| 73 </xsl:template> | |
| 74 | |
| 13 | 75 <xsl:template match="text()|@*" mode="json-value"> | 
| 12 | 76 <xsl:call-template name="write-string"> | 
| 11 | 77 <xsl:with-param name="text" select="."/> | 
| 78 </xsl:call-template> | |
| 79 </xsl:template> | |
| 80 | |
| 12 | 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> | |
| 
0
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
87 </xsl:template> | 
| 
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
88 | 
| 11 | 89 <!-- template traits --> | 
| 12 | 90 | 
| 91 <xsl:template name="write-value"> | |
| 92 <xsl:param name="value" select="."/> | |
| 13 | 93 <xsl:apply-templates select="$value" mode="json-value"/> | 
| 12 | 94 </xsl:template> | 
| 95 | |
| 13 | 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"> | |
| 12 | 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> | |
| 13 | 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> | |
| 12 | 132 </xsl:template> | 
| 133 | |
| 134 <!-- specialized template traits --> | |
| 135 | |
| 136 <xsl:template name="write-string"> | |
| 11 | 137 <xsl:param name="text"/> | 
| 13 | 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> | |
| 
0
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
143 </xsl:template> | 
| 12 | 144 | 
| 145 <xsl:template name="write-object"> | |
| 146 <xsl:param name="value" select="."/> | |
| 147 <xsl:text>{ </xsl:text> | |
| 13 | 148 <xsl:apply-templates select="$value" mode="json-object"/> | 
| 12 | 149 <xsl:text> }</xsl:text> | 
| 150 </xsl:template> | |
| 151 | |
| 152 <xsl:template name="write-array"> | |
| 13 | 153 <xsl:param name="values"/> | 
| 12 | 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 | |
| 13 | 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> | |
| 12 | 174 </xsl:for-each> | 
| 175 </xsl:template> | |
| 13 | 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> | |
| 11 | 251 | 
| 
0
 
cbdada054b4a
Basic schemas for generating csharp internal dom from model definition
 
cin 
parents:  
diff
changeset
 | 
252 </xsl:stylesheet> | 
