-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_to_raw_xml.xslt
105 lines (105 loc) · 4.07 KB
/
json_to_raw_xml.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" version="1.0" id="raw_json_compatibility">
<xsl:variable name="node_name">olsc</xsl:variable>
<xsl:variable name="translate-o">{[ ,</xsl:variable>
<xsl:variable name="translate-c">}] </xsl:variable>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*" mode="value">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="o|l|c" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:value-of select="translate(name(),$node_name,$translate-o)"/>
<xsl:apply-templates select="(text()|*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
<xsl:value-of select="translate(name(),$node_name,$translate-c)"/>
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="s" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:value-of select="' '"/>
<xsl:if test="$is_string">
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="r|f" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:text>\n</xsl:text>
<xsl:apply-templates select="(text()|*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="e" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:text>\\</xsl:text>
<xsl:value-of select="text()"/>
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="text()" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:copy/>
<xsl:if test="$is_string and not(substring(.,string-length(.),1)='"')">
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="$is_string"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="text()[substring(.,1,1)='"']" mode="value">
<xsl:param name="is_string" select="false()"/>
<xsl:copy/>
<xsl:if test="$is_string = false() and (string-length(.)=1 or not(substring(.,string-length(.),1)='"'))">
<xsl:apply-templates select="(following-sibling::text()|following-sibling::*)[1]" mode="value">
<xsl:with-param name="is_string" select="true()"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="l/text()">
<xsl:element name="v">
<xsl:apply-templates mode="value" select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="l">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="o|text()[normalize-space(.)!='']|c"/>
</xsl:copy>
</xsl:template>
<xsl:template match="o">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="a"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<xsl:variable name="following" select="(following-sibling::text()[normalize-space(.)!='']|following-sibling::*[not(self::f or self::r or self::c or self::s)])[1]"/>
<xsl:copy>
<xsl:element name="n">
<xsl:value-of select="text()"/>
</xsl:element>
<xsl:choose>
<xsl:when test="$following/self::o or $following/self::l">
<xsl:apply-templates select="$following"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="v">
<xsl:apply-templates select="$following" mode="value"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>