-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson-mf.xsl
157 lines (151 loc) · 8.08 KB
/
json-mf.xsl
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="no" method="text" omit-xml-declaration="yes" />
<xsl:variable name="xml" select="document('mf.xml')"/>
<xsl:variable name="root" select="/"/>
<xsl:template match="/">
<xsl:text>{"from": "</xsl:text>
<xsl:value-of select="/microformats/@from"/>
<xsl:text>", "title": "</xsl:text>
<xsl:value-of select="/microformats/@title"/>
<xsl:text>"</xsl:text>
<xsl:for-each select="$xml/microformats/*">
<xsl:variable name="node" select="."/>
<xsl:for-each select="$root/microformats/*[name() = name(current()) or name() = current()/@name]">
<xsl:if test="position() = 1 and string-length(.) > 0">
<xsl:value-of select="concat(', "', name(), '": ')"/>
<xsl:choose>
<xsl:when test="count($root/microformats/*[name() = name(current())]) > 1">
<!-- <xsl:when test="$node/@many = 'many'"> -->
<xsl:text>[</xsl:text>
<xsl:for-each select="$root/microformats/*[name() = name(current())]">
<xsl:apply-templates select=".">
<xsl:with-param name="name" select="name()" />
<xsl:with-param name="array" select="true()" />
</xsl:apply-templates>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select=".">
<xsl:with-param name="name" select="name()" />
<xsl:with-param name="array" select="true()" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="node()">
<xsl:param name="name"/>
<xsl:param name="array" select="false()"/>
<xsl:variable name="ele" select="."/>
<xsl:if test="not($array)">
<xsl:value-of select="concat('"', $name, '": ')"/>
</xsl:if>
<xsl:choose>
<xsl:when test="* and count($xml//*[name() = $name or @name = $name or name() = $ele/@type or @name = $ele/@type]/*) > 0">
<xsl:text>{</xsl:text>
<xsl:variable name="cur" select="."/>
<xsl:for-each select="$xml//*[name() = $name or @name = $name or name() = $ele/@type or @name = $ele/@type]/*">
<xsl:variable name="node" select="."/>
<xsl:for-each select="$cur/*[name() = name(current()) or name() = current()/@name]">
<xsl:if test="position() = 1 and (string-length(.) > 0 or @*)">
<xsl:if test="preceding-sibling::*">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="$node/@many = 'many'">
<xsl:value-of select="concat('"', name(), '": [')"/>
<xsl:for-each select="$cur/*[name() = name(current())]">
<xsl:apply-templates select=".">
<xsl:with-param name="name" select="name()" />
<xsl:with-param name="array" select="true()" />
</xsl:apply-templates>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select=".">
<xsl:with-param name="name" select="name()" />
<xsl:with-param name="array" select="false()" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="* and count($xml//*[name() = $name or @name = $name or name() = $ele/@type or @name = $ele/@type]/*) = 0">
<xsl:text>{</xsl:text>
<xsl:for-each select="*">
<xsl:apply-templates select=".">
<xsl:with-param name="name" select="name()" />
<xsl:with-param name="array" select="false()" />
</xsl:apply-templates>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="@date">
<xsl:value-of select="concat('"', @date, '"')"/>
</xsl:when>
<xsl:when test="@href and string-length(.) > 0">
<xsl:text>{"href": "</xsl:text>
<xsl:value-of select="@href"/>
<xsl:text>", "value": "</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="text" select="." />
</xsl:call-template>
<xsl:text>"}</xsl:text>
</xsl:when>
<xsl:when test="@href">
<xsl:value-of select="concat('"', @href, '"')"/>
</xsl:when>
<xsl:when test="string-length(.) > 0">
<xsl:text>"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="text" select="." />
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="escape">
<xsl:param name="text" select="''"/>
<xsl:choose>
<xsl:when test="contains($text, '"') or contains($text, '\')">
<xsl:choose>
<xsl:when test="string-length(substring-before($text, '"')) > string-length(substring-before($text, '\')) and string-length(substring-before($text, '\')) != 0">
<xsl:value-of select="substring-before($text, '\')"/>
<xsl:text>\\</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="text" select="substring-after($text, '\')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($text, '"')"/>
<xsl:text>\"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="text" select="substring-after($text, '"')" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>