-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathxml2json.xsl
220 lines (219 loc) · 12.9 KB
/
xml2json.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?xml version="1.0" encoding="UTF-8"?>
<!-- This XSLT transforms https://github.com/dret/webconcepts into JSON. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns="http://www.w3.org/2005/xpath-functions">
<xsl:output name="json" method="text" encoding="UTF-8"/>
<!-- -->
<xsl:template name="xml2json">
<xsl:result-document href="concepts.json" format="json">
<xsl:variable name="concepts-json">
<array>
<xsl:for-each select="$concepts/concepts/concept">
<xsl:sort select="@id"/>
<xsl:call-template name="concept-values-json">
<xsl:with-param name="concept" select="."/>
</xsl:call-template>
</xsl:for-each>
</array>
</xsl:variable>
<xsl:value-of select="xml-to-json($concepts-json, map{'indent':true()})"/>
</xsl:result-document>
<xsl:for-each select="$concepts/concepts/concept">
<xsl:variable name="concept" select="."/>
<xsl:for-each select="distinct-values($allspecs//*[local-name() = $concept/@id]/@def)">
<xsl:sort select="."/>
<xsl:variable name="concept-name" select="."/>
<xsl:variable name="concept-value-json">
<xsl:call-template name="concept-value-json">
<xsl:with-param name="concept-value" select="."/>
<xsl:with-param name="concept" select="$concept"/>
</xsl:call-template>
</xsl:variable>
<xsl:result-document href="{$concepts-dir}/{$concept/@id}/{$concept-name}.json" format="json">
<xsl:value-of select="xml-to-json($concept-value-json, map{'indent':true()})"/>
</xsl:result-document>
</xsl:for-each>
</xsl:for-each>
<xsl:for-each select="$concepts/concepts/concept">
<xsl:variable name="concept-values-json">
<xsl:call-template name="concept-values-json">
<xsl:with-param name="concept" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:result-document href="{$concepts-dir}/{@id}.json" format="json">
<xsl:value-of select="xml-to-json($concept-values-json, map{'indent':true()})"/>
</xsl:result-document>
</xsl:for-each>
<xsl:result-document href="specs.json" format="json">
<xsl:text>{
</xsl:text>
<xsl:for-each-group select="$allspecs/service" group-by="@primary">
<xsl:sort select="@primary"/>
<xsl:variable name="primary" select="$specs/specs/primary[@id eq current()/@primary]"/>
<xsl:value-of select="concat(' "', @primary, '": {
')"/>
<xsl:text> "id": </xsl:text>
<xsl:value-of select="concat('"http://webconcepts.info/', $specs-dir, '/', $primary/@id, '/",
')"/>
<xsl:text> "name": </xsl:text>
<xsl:value-of select="concat('"', $primary/name, '",
')"/>
<xsl:if test="exists($primary/name/@short)">
<xsl:text> "short": </xsl:text>
<xsl:value-of select="concat('"', $primary/name/@short, '",
')"/>
</xsl:if>
<xsl:text> "series": [</xsl:text>
<xsl:for-each-group select="current-group()" group-by="@secondary">
<xsl:sort select="@secondary"/>
<xsl:variable name="secondary" select="$primary/secondary[@id eq current()/@secondary]"/>
<xsl:value-of select="concat('{
 "', @secondary, '": {
')"/>
<xsl:text> "id": </xsl:text>
<xsl:value-of select="concat('"http://webconcepts.info/', $specs-dir, '/', $primary/@id, '/', $secondary/@id, '/",
')"/>
<xsl:text> "name": </xsl:text>
<xsl:value-of select="concat('"', $secondary/name, '",
')"/>
<xsl:if test="exists($secondary/name/@short)">
<xsl:text> "short": </xsl:text>
<xsl:value-of select="concat('"', $secondary/name/@short, '",
')"/>
</xsl:if>
<xsl:text> "specs": [</xsl:text>
<xsl:for-each select="current-group()">
<xsl:sort select="@id"/>
<xsl:if test="count($specs/specs/primary[@id eq current()/@primary]) ne 1">
<xsl:message terminate="yes" select="concat('Non-matching service/@primary: ', current()/@primary)"/>
</xsl:if>
<xsl:if test="count($specs/specs/primary[@id eq current()/@primary]/secondary[@id eq current()/@secondary]) ne 1">
<xsl:message terminate="yes" select="concat('Non-matching service/@secondary: ', current()/@primary, '/', current()/@secondary)"/>
</xsl:if>
<xsl:if test="not(matches(@id, $secondary/id-pattern))">
<xsl:message terminate="yes" select="concat('Non-matching service/@id: ', $primary, '/', $secondary, '/', @id)"/>
</xsl:if>
<xsl:variable name="id" select="replace(@id, $secondary/id-pattern, $secondary/md-pattern)"/>
<xsl:value-of select="concat('{
 "', @id, '": {
')"/>
<xsl:text> "id": </xsl:text>
<xsl:value-of select="concat('"http://webconcepts.info/', $specs-dir, '/', $primary/@id, '/', $secondary/@id, '/', $id, '",
')"/>
<xsl:text> "title": </xsl:text>
<xsl:value-of select="concat('"', replace(title, '"', '\\"'), '",
')"/>
<xsl:text> "name": "</xsl:text>
<xsl:call-template name="spec-name">
<xsl:with-param name="id" select="$id"/>
<xsl:with-param name="secondary" select="$secondary"/>
</xsl:call-template>
<xsl:text>",
</xsl:text>
<xsl:if test="exists($secondary/uri-pattern)">
<xsl:text> "URI": </xsl:text>
<xsl:value-of select="concat('"', replace(@id, $secondary/id-pattern, $secondary/uri-pattern), '",
')"/>
</xsl:if>
<xsl:text> "URL": </xsl:text>
<xsl:value-of select="concat('"', if (exists(@url)) then @url else replace(@id, $secondary/id-pattern, $secondary/url-pattern), '",
')"/>
<xsl:text> "abstract": </xsl:text>
<xsl:value-of select="concat('"', replace(documentation/text(), '"', '\\"'), '",
')"/>
<xsl:text> "concepts": [</xsl:text>
<xsl:for-each select="*[local-name() = $concepts/concepts/concept/@id]">
<xsl:sort select="$concepts//concept[@id eq current()/local-name()]/@id"/>
<xsl:sort select="@def"/>
<xsl:variable name="concept-id" select="$concepts//concept[@id eq current()/local-name()]/@id"/>
<xsl:text>{
 "</xsl:text>
<xsl:value-of select="concat('http://webconcepts.info/', $concepts-dir, '/', $concept-id, '/')"/>
<xsl:text>": "</xsl:text>
<xsl:value-of select="concat('http://webconcepts.info/', $concepts-dir, '/', $concept-id, '/', @def)"/>
<xsl:text>" }</xsl:text>
<xsl:if test="position() ne last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]}}</xsl:text>
<xsl:if test="position() ne last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]}}</xsl:text>
<xsl:if test="position() ne last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each-group>
<xsl:text>]}</xsl:text>
<xsl:if test="position() ne last()">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
<xsl:text>}</xsl:text>
</xsl:result-document>
</xsl:template>
<xsl:template name="spec-name">
<xsl:param name="id"/>
<xsl:param name="secondary"/>
<xsl:value-of select="replace(replace($id, '^(..*)$', $secondary/name-pattern), '"', '\\"')"/>
</xsl:template>
<xsl:template name="concept-value-json">
<xsl:param name="concept-value"/>
<xsl:param name="concept"/>
<map>
<string key="value">
<xsl:value-of select="$concept-value"/>
</string>
<string key="concept">
<xsl:value-of select="concat('http://webconcepts.info/', $concepts-dir, '/', $concept/@id, '/')"/>
</string>
<string key="id">
<xsl:value-of select="concat('http://webconcepts.info/', $concepts-dir, '/', $concept/@id, '/', $concept-value)"/>
</string>
<xsl:variable name="desc" select="$allspecs//*[local-name() eq $concept/@id][@def eq $concept-value][1]/@desc"/>
<!-- this is cheating by (randomly) picking the first description should there be more than one in all specifications. -->
<xsl:if test="exists($desc)">
<string key="description">
<xsl:value-of select="$desc"/>
</string>
</xsl:if>
<array key="details">
<xsl:for-each select="$allspecs/service/*[local-name() eq $concept/@id][@def eq $concept-value]">
<xsl:variable name="secondary" select="$specs/specs/primary[@id eq current()/../@primary]/secondary[@id eq current()/../@secondary]"/>
<xsl:variable name="id" select="replace(current()/../@id, $secondary/id-pattern, $secondary/md-pattern)"/>
<map>
<string key="description">
<xsl:value-of select="documentation/text()"/>
</string>
<string key="documentation">
<xsl:value-of select="documentation/@source"/>
</string>
<string key="specification">
<xsl:value-of select="concat('http://webconcepts.info/', $specs-dir, '/', $secondary/../@id, '/', $secondary/@id, '/', $id)"/>
</string>
<string key="spec-name" >
<xsl:call-template name="spec-name">
<xsl:with-param name="id" select="$id"/>
<xsl:with-param name="secondary" select="$secondary"/>
</xsl:call-template>
</string>
</map>
</xsl:for-each>
</array>
</map>
</xsl:template>
<xsl:template name="concept-values-json">
<xsl:param name="concept"/>
<map>
<string key="concept">
<xsl:value-of select="@id"/>
</string>
<string key="id">
<xsl:value-of select="concat('http://webconcepts.info/', $concepts-dir, '/', @id, '/')"/>
</string>
<string key="name-singular">
<xsl:value-of select="title-singular"/>
</string>
<string key="name-plural">
<xsl:value-of select="title-plural"/>
</string>
<xsl:if test="exists(iana-registry)">
<string key="registry">
<xsl:value-of select="iana-registry"/>
</string>
</xsl:if>
<array key="values">
<xsl:for-each select="distinct-values($allspecs//*[local-name() eq $concept/@id]/@def)">
<xsl:sort select="."/>
<xsl:call-template name="concept-value-json">
<xsl:with-param name="concept-value" select="."/>
<xsl:with-param name="concept" select="$concept"/>
</xsl:call-template>
</xsl:for-each>
</array>
</map>
</xsl:template>
</xsl:stylesheet>