-
Notifications
You must be signed in to change notification settings - Fork 4
/
json.xsl
executable file
·181 lines (160 loc) · 5.91 KB
/
json.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Template to produce results in JSON format
** Philip McAllister, 01/10/2012
** icerunner@gmail.com
** Additional fixes by bswatson (See github project for details)
** https://github.com/icerunner/google-mini
** Released under Creative Commons CC BY-SA 3.0
** The templates json.xsl and jsonp.xsl are released under Creative Commons license "Attribution-ShareAlike 3.0 Unported" http://creativecommons.org/licenses/by-sa/3.0/
** Please share any improvements or alterations you make to this template and please leave these comments in.
-->
<xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes" indent="no" media-type="application/json"/>
<xsl:template match="/">
<xsl:apply-templates select="GSP"/>
</xsl:template>
<xsl:template match="GSP">
<xsl:text disable-output-escaping="yes">{
"query": "</xsl:text>
<xsl:value-of select="Q" />
<xsl:text>",
</xsl:text>
<xsl:if test="GM">
<xsl:text disable-output-escaping="yes"> "keymatch": [
</xsl:text> <xsl:apply-templates select="GM" />
<xsl:text> ],
</xsl:text>
</xsl:if>
<xsl:apply-templates select="RES" />
<xsl:call-template name="results_navigation_wrapper">
<xsl:with-param name="prev" select="RES/NB/PU"/>
<xsl:with-param name="next" select="RES/NB/NU"/>
<xsl:with-param name="view_begin" select="RES/@SN"/>
<xsl:with-param name="view_end" select="RES/@EN"/>
<xsl:with-param name="guess" select="RES/M"/>
</xsl:call-template>
<xsl:text disable-output-escaping="yes">
}</xsl:text>
</xsl:template>
<!-- Results settings -->
<xsl:template name="results_navigation_wrapper">
<xsl:param name="prev"/>
<xsl:param name="next"/>
<xsl:param name="view_begin"/>
<xsl:param name="view_end"/>
<xsl:param name="guess"/>
<xsl:text disable-output-escaping="yes">
"results_nav": {
"total_results": "</xsl:text><xsl:value-of select="$guess" /><xsl:text>",
"results_start": "</xsl:text><xsl:value-of select="$view_begin" /><xsl:text>",
"results_end": "</xsl:text><xsl:value-of select="$view_end" /><xsl:text>",
"current_view": "</xsl:text><xsl:value-of select="$view_begin - 1" /><xsl:text>"</xsl:text>
<xsl:if test="/GSP/RES/NB/PU">
<xsl:text>,
"have_prev": "1"</xsl:text>
</xsl:if>
<xsl:if test="/GSP/RES/NB/NU">
<xsl:text>,
"have_next": "1"</xsl:text>
</xsl:if>
<xsl:text>
}
</xsl:text>
</xsl:template>
<xsl:template match="GM">
<xsl:text disable-output-escaping="yes"> {
</xsl:text>
<xsl:text disable-output-escaping="yes"> "title": "</xsl:text>
<xsl:value-of select="GD" /><xsl:text disable-output-escaping="yes">",
"url": "</xsl:text>
<xsl:value-of select="GL" /><xsl:text disable-output-escaping="yes">"
}</xsl:text>
<xsl:if test="position() != last()"><xsl:text>,
</xsl:text></xsl:if>
</xsl:template>
<xsl:template match="RES">
<xsl:text disable-output-escaping="yes"> "results": [
</xsl:text>
<xsl:apply-templates select="R"/>
<xsl:text disable-output-escaping="yes">
],</xsl:text>
</xsl:template>
<xsl:template match="R">
<xsl:text disable-output-escaping="yes">
{</xsl:text>
<xsl:apply-templates select="U"/>
<xsl:apply-templates select="T"/>
<xsl:apply-templates select="S"/>
<xsl:apply-templates select="HAS"/>
<xsl:if test="string(@MIME)">
<xsl:text disable-output-escaping="yes">,
"mime": "</xsl:text>
<xsl:value-of select ="@MIME" />
<xsl:text disable-output-escaping="yes">"
</xsl:text>
</xsl:if>
<xsl:text disable-output-escaping="yes">
}</xsl:text>
<xsl:if test="position() != last()"><xsl:text>,
</xsl:text></xsl:if>
</xsl:template>
<xsl:template match="U">
<xsl:text disable-output-escaping="yes"> "url": "</xsl:text>
<xsl:value-of select="." />
<xsl:text disable-output-escaping="yes">",
</xsl:text>
</xsl:template>
<xsl:template match="T">
<xsl:text disable-output-escaping="yes"> "title": "</xsl:text>
<xsl:call-template name="replace_apos">
<xsl:with-param name="string" select="." />
</xsl:call-template>
<xsl:text disable-output-escaping="yes">",
</xsl:text>
</xsl:template>
<xsl:template match="S">
<xsl:text disable-output-escaping="yes"> "summary": "</xsl:text>
<xsl:call-template name="replace_apos">
<xsl:with-param name="string" select="." />
</xsl:call-template>
<xsl:text disable-output-escaping="yes">"</xsl:text>
</xsl:template>
<xsl:template match="HAS">
<xsl:if test="string(C/@SZ)">
<xsl:text disable-output-escaping="yes">,
"size": "</xsl:text>
<xsl:value-of select="C/@SZ" />
<xsl:text disable-output-escaping="yes">"
</xsl:text>
</xsl:if>
</xsl:template>
<!-- *** Find and replace *** -->
<xsl:template name="replace_string">
<xsl:param name="find"/>
<xsl:param name="replace"/>
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string, $find)">
<xsl:value-of select="substring-before($string, $find)" />
<xsl:value-of select="$replace" />
<xsl:call-template name="replace_string">
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="string"
select="substring-after($string, $find)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="replace_apos">
<xsl:param name="string"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="find" select="$apos"/>
<xsl:with-param name="replace" select="'&#39;'"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>