Project

Profile

Help

Maintenance. Planio will be undergoing a scheduled maintenance this weekend. Between Saturday, July 24 at 9:00 UTC and Sunday, July 25, 22:00 UTC your account might observe occasional downtimes which may last up to several minutes in some cases.

How to connect?
Download (6.31 KB) Statistics
| Branch: | Tag: | Revision:

he / latest10 / hej / net / sf / saxon / data / xml-to-json.xsl @ 02f8308b

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
4
                xmlns:j="http://www.w3.org/2005/xpath-functions/json"
5
                exclude-result-prefixes="xs j"
6
                version="3.0">
7
   <xsl:param name="j:key-delimiter" as="xs:string">"</xsl:param>
8
   <xsl:param name="j:string-delimiter" as="xs:string">"</xsl:param>
9
   <xsl:param name="j:key-value-separator" as="xs:string">:</xsl:param>
10
   <xsl:param name="j:map-separator" as="xs:string">,</xsl:param>
11
   <xsl:param name="j:start-array" as="xs:string">[</xsl:param>
12
   <xsl:param name="j:end-array" as="xs:string">]</xsl:param>
13
   <xsl:param name="j:start-map" as="xs:string">{</xsl:param>
14
   <xsl:param name="j:end-map" as="xs:string">}</xsl:param>
15
   <xsl:param name="j:array-separator" as="xs:string">,</xsl:param>
16
   <xsl:template name="j:start-array">
17
            <xsl:value-of select="$j:start-array"/>
18
        </xsl:template>
19
   <xsl:template name="j:end-array">
20
            <xsl:value-of select="$j:end-array"/>
21
        </xsl:template>
22
   <xsl:template name="j:start-map">
23
            <xsl:value-of select="$j:start-map"/>
24
        </xsl:template>
25
   <xsl:template name="j:end-map">
26
            <xsl:value-of select="$j:end-map"/>
27
        </xsl:template>
28
   <xsl:template name="j:key-value-separator">
29
            <xsl:value-of select="$j:key-value-separator"/>
30
        </xsl:template>
31
   <xsl:template name="j:map-separator">
32
            <xsl:value-of select="$j:map-separator"/>
33
        </xsl:template>
34
   <xsl:template name="j:array-separator">
35
            <xsl:value-of select="$j:array-separator"/>
36
        </xsl:template>
37
   <xsl:function name="j:xml-to-json" as="xs:string">
38
            <xsl:param name="xml" as="node()"/>
39
            <xsl:variable name="parts" as="xs:string*">
40
                <xsl:apply-templates select="$xml" mode="j:xml-to-json"/>
41
            </xsl:variable>
42
            <xsl:value-of select="string-join($parts,'')"/>
43
        </xsl:function>
44
   <xsl:template name="j:xml-to-json" as="xs:string">
45
            <xsl:param name="xml" as="node()" select="."/>
46
            <xsl:value-of select="j:xml-to-json($xml)"/>
47
        </xsl:template>
48
   <xsl:mode name="j:xml-to-json" on-no-match="shallow-copy"/>
49
   <xsl:template match="j:map" mode="j:xml-to-json">
50
            <xsl:call-template name="j:start-map"/>
51
            <xsl:for-each select="*">
52
                <xsl:if test="position() gt 1">
53
                    <xsl:call-template name="j:map-separator"/>
54
                </xsl:if>
55
                <xsl:apply-templates select="@key" mode="j:xml-to-json"/>
56
                <xsl:call-template name="j:key-value-separator"/>
57
                <xsl:apply-templates select="." mode="j:xml-to-json"/>
58
            </xsl:for-each>
59
            <xsl:call-template name="j:end-map"/>
60
        </xsl:template>
61
   <xsl:template match="j:array" mode="j:xml-to-json">
62
            <xsl:call-template name="j:start-array"/>
63
            <xsl:for-each select="*">
64
                <xsl:if test="position() gt 1">
65
                    <xsl:call-template name="j:array-separator"/>
66
                </xsl:if>
67
                <xsl:apply-templates select="." mode="j:xml-to-json"/>
68
            </xsl:for-each>
69
            <xsl:call-template name="j:end-array"/>
70
        </xsl:template>
71
   <xsl:template match="j:string[@escaped='true']" mode="j:xml-to-json">
72
            <xsl:value-of select="concat($j:string-delimiter, ., $j:string-delimiter)"/>
73
        </xsl:template>
74
   <xsl:template match="j:string[not(@escaped='true')]" mode="j:xml-to-json">
75
            <xsl:value-of select="concat($j:string-delimiter, j:escape(.), $j:string-delimiter)"/>
76
        </xsl:template>
77
   <xsl:template match="j:number | j:boolean" mode="j:xml-to-json">
78
            <xsl:value-of select="."/>
79
        </xsl:template>
80
   <xsl:template match="j:null" mode="j:xml-to-json">
81
            <xsl:text>null</xsl:text>
82
        </xsl:template>
83
   <xsl:template match="j:*/@key[../@key-escaped='true']" mode="j:xml-to-json">
84
            <xsl:value-of select="concat($j:key-delimiter, ., $j:key-delimiter)"/>
85
        </xsl:template>
86
   <xsl:template match="j:*/@key[not(../@key-escaped='true')]" mode="j:xml-to-json">
87
            <xsl:value-of select="concat($j:key-delimiter, j:escape(.), $j:key-delimiter)"/>
88
        </xsl:template>
89
   <xsl:template match="text()" mode="j:xml-to-json"/>
90
   <xsl:function name="j:escape" as="xs:string">
91
            <xsl:param name="in" as="xs:string"/>
92
            <xsl:value-of>
93
                <xsl:for-each select="string-to-codepoints($in)">
94
                    <xsl:choose>
95
                        <xsl:when test=". gt 65535">
96
                            <xsl:value-of select="concat('\u', j:hex4((. - 65536) idiv 1024 + 55296))"/>
97
                            <xsl:value-of select="concat('\u', j:hex4((. - 65536) mod 1024 + 56320))"/>
98
                        </xsl:when>
99
                        <xsl:when test=". = 34">\"</xsl:when>
100
                        <xsl:when test=". = 92">\\</xsl:when>
101
                        <xsl:when test=". = 08">\b</xsl:when>
102
                        <xsl:when test=". = 09">\t</xsl:when>
103
                        <xsl:when test=". = 10">\n</xsl:when>
104
                        <xsl:when test=". = 12">\f</xsl:when>
105
                        <xsl:when test=". = 13">\r</xsl:when>
106
                        <xsl:when test=". lt 32 or (. ge 127 and . le 160)">
107
                            <xsl:value-of select="concat('\u', j:hex4(.))"/>
108
                        </xsl:when>
109
                        <xsl:otherwise>
110
                            <xsl:value-of select="codepoints-to-string(.)"/>
111
                        </xsl:otherwise>
112
                    </xsl:choose>
113
                </xsl:for-each>
114
            </xsl:value-of>
115
        </xsl:function>
116
   <xsl:function name="j:hex4" as="xs:string">
117
            <xsl:param name="ch" as="xs:integer"/>
118
            <xsl:variable name="hex" select="'0123456789abcdef'"/>
119
            <xsl:value-of>
120
                <xsl:value-of select="substring($hex, $ch idiv 4096 + 1, 1)"/>
121
                <xsl:value-of select="substring($hex, $ch idiv 256 mod 16 + 1, 1)"/>
122
                <xsl:value-of select="substring($hex, $ch idiv 16 mod 16 + 1, 1)"/>
123
                <xsl:value-of select="substring($hex, $ch mod 16 + 1, 1)"/>
124
            </xsl:value-of>
125
        </xsl:function>
126
</xsl:stylesheet>
(12-12/14)