My favorites
▼
|
Sign in
lindenb
Pierre Lindenbaum's library
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
xsl
/
sql2json.xsl
‹r491
r508
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
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version='1.0'
>
<!--
Motivation:
This stylesheet transforms the output of 'desc table *' in mysql+XML for the UCSC database
to a JSON file
Author:
Pierre Lindenbaum
http://plindenbaum.blogspot.com
plindenbaum@yahoo.fr
Params:
* "var" var @name={content}; will be added OPTIONAL
* "ucsc"=true extension for ucsc tables OPTIONAL
* "mongo"=name for mondodb collection OPTIONAL
-->
<xsl:output method="text" version="1.0" encoding="UTF-8"/>
<xsl:param name="var"></xsl:param>
<xsl:param name="ucsc">false</xsl:param>
<xsl:param name="mongo"></xsl:param>
<!-- ================ DOCUMENT ROOT ========================================================== -->
<xsl:template match="/">
<xsl:if test="string-length($var)>0 and string-length($mongo)=0"><xsl:value-of select="concat('var ',$var,'=')"/></xsl:if>
<xsl:apply-templates/>
<xsl:if test="string-length($var)>0 and string-length($mongo)=0"><xsl:text>;</xsl:text></xsl:if>
<xsl:text>
</xsl:text>
</xsl:template>
<!-- ================ RESULT SET ========================================================== -->
<xsl:template match="resultset">
<xsl:if test="string-length($mongo)=0">
<xsl:text>[</xsl:text>
</xsl:if>
<xsl:for-each select="row">
<xsl:if test="string-length($mongo)>0">
<xsl:text>record=</xsl:text>
</xsl:if>
<xsl:if test="position()!=1 and string-length($mongo)=0"><xsl:text>,</xsl:text></xsl:if>
<xsl:text>{</xsl:text>
<xsl:for-each select="field">
<xsl:if test="position()!=1"><xsl:text>,</xsl:text></xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:text>}</xsl:text>
<xsl:if test="string-length($mongo)>0">
<xsl:text>; db.</xsl:text>
<xsl:value-of select="$mongo"/>
<xsl:text>.save(record);
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="string-length($mongo)=0">
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="field">
<xsl:variable name="s" select="."/>
<xsl:choose>
<xsl:when test="@name='id' and string-length($mongo)>0">
<xsl:text>_id</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="quote">
<xsl:with-param name="s" select="@name"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:text>:</xsl:text>
<xsl:choose>
<xsl:when test="$ucsc='true' and (@name='exonEnds' or @name='exonStarts' )">
<xsl:text>[</xsl:text>
<xsl:call-template name="exons">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:when test="$s='-'">
<xsl:text>"-"</xsl:text>
</xsl:when>
<xsl:when test="$s='+'">
<xsl:text>"+"</xsl:text>
</xsl:when>
<xsl:when test="number($s)=number($s)"><!-- test will fail if NaN=Nan -->
<xsl:value-of select="$s"/>
</xsl:when>
<xsl:when test="$s='FALSE' or $s='false'">
<xsl:text>false</xsl:text>
</xsl:when>
<xsl:when test="$s='TRUE' or $s='true'">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:when test="@xsi:nil='true'">
<xsl:text>null</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="quote">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="quote">
<xsl:param name="s"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template name="escape">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'"')">
<xsl:value-of select="substring-before($s,'"')"/>
<xsl:text>\"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="substring-after($s,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="exons">
<xsl:param name="s"/>
<xsl:variable name="s2" select="normalize-space($s)"/>
<xsl:choose>
<xsl:when test="$s2=',' or $s2=''">
</xsl:when>
<xsl:when test="contains($s2,',')">
<xsl:value-of select="substring-before($s2,',')"/>
<xsl:variable name="s3" select="substring-after($s2,',')"/>
<xsl:if test="$s3!=',' and $s3!=''">
<xsl:text>,</xsl:text>
<xsl:call-template name="exons">
<xsl:with-param name="s" select="$s3"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$s2'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Show details
Hide details
Change log
r505
by plindenbaum on Sep 29, 2010
Diff
support json
Go to:
/trunk/src/xsl/sql2json.xsl
Project members,
sign in
to write a code review
Older revisions
r491
by plindenbaum on Aug 2, 2010
Diff
cont
All revisions of this file
File info
Size: 4579 bytes, 173 lines
View raw file
Powered by
Google Project Hosting