-
Notifications
You must be signed in to change notification settings - Fork 1
/
vo-dml2dsl.xsl
300 lines (250 loc) · 12.5 KB
/
vo-dml2dsl.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?xml version="1.0" encoding="UTF-8"?>
<!--
This XSLT script transforms a data model from the
official VODML XML representation to the VODSL representation.
Paul Harrison
-->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:vo-dml="http://www.ivoa.net/xml/VODML/v1">
<xsl:output method="text" encoding="UTF-8" indent="no" />
<xsl:param name="model.name" select="'silly'"/>
<xsl:strip-space elements="*" />
<xsl:key name="element" match="//*[vodml-id]" use="vodml-id"/>
<xsl:key name="package" match="//package/vodml-id" use="."/>
<xsl:variable name="packages" select="//package/vodml-id"/>
<xsl:variable name="sq"><xsl:text>'</xsl:text></xsl:variable>
<xsl:variable name="dq"><xsl:text>"</xsl:text></xsl:variable>
<xsl:variable name='nl'><xsl:text>
</xsl:text></xsl:variable>
<xsl:variable name='lt'><xsl:text disable-output-escaping="yes"><</xsl:text></xsl:variable>
<xsl:variable name='gt'><xsl:text disable-output-escaping="yes">></xsl:text></xsl:variable>
<xsl:variable name="modname">
<xsl:choose>
<xsl:when test="/vo-dml:model/vodml-id"><xsl:value-of select="/vo-dml:model/vodml-id" /></xsl:when>
<xsl:otherwise><xsl:value-of select="/vo-dml:model/name" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="/">
<xsl:message>Starting VODSL</xsl:message>
<xsl:apply-templates select="vo-dml:model"/>
</xsl:template>
<xsl:template match="vo-dml:model">
<xsl:message>Found model <xsl:value-of select="$modname"/></xsl:message>
model <xsl:value-of select="$modname"/> (<xsl:value-of select="version"/>) "<xsl:value-of select="description"/>"
<xsl:apply-templates select="author" />
<xsl:apply-templates select="title" />
<xsl:apply-templates select="import" />
<xsl:apply-templates select="* except (import|version|description|vodml-id|identifier|lastModified|name|title|author|previousVersion)"/>
</xsl:template>
<xsl:template match="import">
<xsl:analyze-string regex="([^/]+)\.vo-dml" select="url">
<xsl:matching-substring>
include "<xsl:value-of select="regex-group(1)"/>.vodsl"
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="author">
<xsl:value-of select="concat($nl,' author ',$dq,.,$dq)"/>
</xsl:template>
<xsl:template match="title">
<xsl:value-of select="concat($nl,'title ',$dq,.,$dq)"/>
</xsl:template>
<xsl:template match="package">
package <xsl:value-of select="concat(name,' ')"/> <xsl:call-template name= "do-description"/>
{
<xsl:apply-templates select="* except (vodml-id|description|name)" />
}
</xsl:template>
<xsl:template match="primitiveType">
primitive <xsl:value-of select="concat(name, ' ')"/> <xsl:call-template name= "do-description"/>
</xsl:template>
<!-- do some heuristics to try to normalize some different practices -->
<!-- TODO could to better with comparing child packages rather than just looking if same package - could avoid the full package referencing - however full package referencing does work in vodsl is a bit ugly -->
<xsl:template match='extends/vodml-ref'>
<xsl:variable name="ctx" select="current()/parent::extends/parent::*"/>
<!-- <xsl:message><xsl:value-of select="concat(' extends ctx=',$ctx/name,' ',$ctx/vodml-id, ' ref=',current(), ' refparent=',key('element',substring-after(current(),':'))/parent::*/vodml-id )"/></xsl:message>-->
<xsl:choose>
<xsl:when test="substring-before( .,':') = $modname">
<xsl:choose>
<xsl:when test="key('element',substring-after(current(),':'))/parent::package/vodml-id = $ctx/parent::package/vodml-id"> <!-- in same package -->
<xsl:value-of select="key('element',substring-after(current(),':'))/name"/><!-- IMPL perhaps the -->
</xsl:when>
<xsl:otherwise>
<xsl:variable name="fullpath" select="string-join(key('element',substring-after(current(),':'))/ancestor-or-self::*/name[not(../name() = 'vo-dml:model')],'.')"/>
<xsl:value-of select="$fullpath"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- <xsl:value-of select="translate(.,':','.')"/> -->
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match='datatype/vodml-ref'>
<xsl:variable name="ctx" select="current()/parent::datatype/parent::*/parent::*"/>
<!-- <xsl:message><xsl:value-of select="concat('datatype ctx=',$ctx/name(),' ',$ctx/vodml-id, ' ref=',current(), ' refparent=',key('element',substring-after(current(),':'))/parent::*/vodml-id )"/></xsl:message>-->
<xsl:choose>
<xsl:when test="substring-before( .,':') = $modname">
<xsl:choose>
<xsl:when test="key('element',substring-after(current(),':'))/parent::package/vodml-id = $ctx/parent::package/vodml-id">
<xsl:value-of select="substring-after(current(),':')"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="fullpath" select="string-join(key('element',substring-after(current(),':'))/ancestor-or-self::*/name[not(../name() = 'vo-dml:model')],'.')"/>
<xsl:value-of select="$fullpath"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- <xsl:value-of select="translate(.,':','.')"/> -->
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="objectType">
<xsl:value-of select="$nl"/><xsl:if test="@abstract and xsd:boolean(@abstract)">abstract </xsl:if>otype <xsl:value-of select="name"/><xsl:text> </xsl:text>
<xsl:apply-templates select= "extends"/>
<xsl:call-template name= "do-description"/>
{ <xsl:apply-templates select="* except (vodml-id|description|name|extends)"/>
}
</xsl:template>
<xsl:template match="dataType"><!-- is this really so different from object? -->
<xsl:value-of select="$nl"/><xsl:if test="@abstract and xsd:boolean(@abstract)">abstract </xsl:if>dtype <xsl:value-of select="name"/><xsl:text> </xsl:text>
<xsl:apply-templates select= "extends"/>
<xsl:call-template name= "do-description"/>
{ <xsl:apply-templates select="* except (vodml-id|description|name|extends)"/>
}
</xsl:template>
<xsl:template name ="do-description">
<xsl:choose>
<xsl:when test="description">
<xsl:text> "</xsl:text><xsl:if test="not(matches(description/text(),'^\s*TODO'))"><xsl:value-of select='translate(description,$dq,$sq)'/></xsl:if><xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>""</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="attribute">
<xsl:text>
</xsl:text>
<xsl:value-of select="concat(name, ': ')"/>
<xsl:apply-templates select="datatype/vodml-ref"/><xsl:text> </xsl:text>
<xsl:apply-templates select="multiplicity"/><xsl:text> </xsl:text>
<xsl:apply-templates select="constraint[@xsi:type='vo-dml:NaturalKey']" /><!-- IMPL perhaps iskey is in 'wrong place' in vodsl -->
<xsl:call-template name="do-description"/>
<xsl:apply-templates select="* except (description|datatype|name|vodml-id|multiplicity|constraint[@xsi:type='vo-dml:NaturalKey'])"/>
<xsl:text>;</xsl:text>
</xsl:template>
<xsl:template match="utype">
<xsl:choose>
<xsl:when test="starts-with(text(),'ivoa:')"><xsl:value-of select="substring-after(text(),'ivoa:')"/></xsl:when> <!-- assume the ivoa namespace as default -->
<xsl:when test="starts-with(text(),concat(/vo-dml:model/vodml-id,':')) or starts-with(text(),concat(/vo-dml:model/name,':'))">
<!-- TODO should deal with nested packages -->
<xsl:variable name="pname" select="ancestor::package/name/text()"/>
<xsl:for-each select="tokenize(substring-after(text(),':'), '\.')">
<xsl:if test=". ne $pname or position() eq last()"><xsl:sequence select="."/>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
<xsl:text> /* utype=</xsl:text><xsl:value-of select="."/><xsl:text>*/</xsl:text>
</xsl:template>
<xsl:template match="multiplicity">
<xsl:choose>
<xsl:when test="number(minOccurs) eq 1 and number(maxOccurs) eq 1"><!-- do nothing --></xsl:when>
<xsl:when test="number(minOccurs) eq 0 and number(maxOccurs) eq 1"> @? </xsl:when>
<xsl:when test="number(minOccurs) eq 0 and number(maxOccurs) eq -1"> @* </xsl:when>
<xsl:when test="number(minOccurs) eq 1 and number(maxOccurs) eq -1"> @+ </xsl:when>
<xsl:otherwise><xsl:value-of select="concat('@[',minOccurs,'..', maxOccurs,']')"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="enumeration">
enum <xsl:value-of select="name"/><xsl:text> </xsl:text>
<xsl:call-template name= "do-description"/>
{
<xsl:apply-templates select="literal"/>
}
</xsl:template>
<xsl:template match="literal">
<xsl:value-of select="name"/><xsl:text> </xsl:text><xsl:call-template name= "do-description"/>
<xsl:if test="position() != last()">,
</xsl:if>
</xsl:template>
<xsl:template match="composition|container"> <!-- are they both present? -->
<xsl:text>
</xsl:text>
<xsl:value-of select="concat(name, ' : ')"/>
<xsl:apply-templates select="datatype/vodml-ref,multiplicity"/>
<xsl:text> as</xsl:text>
<xsl:if test="@isOrdered"><xsl:text> ordered</xsl:text></xsl:if>
<xsl:text> composition</xsl:text>
<xsl:call-template name= "do-description"/>
<xsl:text>;</xsl:text>
</xsl:template>
<xsl:template match="reference">
<xsl:text>
</xsl:text>
<xsl:value-of select="concat(name, ' ')"/>
<xsl:apply-templates select="multiplicity"/>
<xsl:text> references </xsl:text>
<xsl:apply-templates select="datatype/vodml-ref"/>
<xsl:call-template name= "do-description"/>
<xsl:text>;</xsl:text>
</xsl:template>
<xsl:template match="extends">
<xsl:text> -> </xsl:text><xsl:apply-templates/>
</xsl:template>
<xsl:template match="semanticconcept">
<xsl:text> semantic "</xsl:text><xsl:value-of select="topConcept"/><xsl:text>" in "</xsl:text><xsl:value-of select="vocabularyURI"/><xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="constraint[@xsi:type='vo-dml:SubsettedRole']"><!-- FIXME these apply to attributes...I think.... -->
<xsl:text>
subset </xsl:text> <xsl:value-of select="role/vodml-ref"/><xsl:text> as </xsl:text><xsl:value-of select="datatype/vodml-ref"/><xsl:text>;</xsl:text>
</xsl:template>
<xsl:template match="constraint[@xsi:type='vo-dml:NaturalKey']">
<xsl:text> iskey </xsl:text>
</xsl:template>
<xsl:template match="constraint[parent::objectType and not (@xsi:type='vo-dml:SubsettedRole') ]"> <!-- FIXME - need to work out where this goes for plain constraint -->
<xsl:text>// constraint </xsl:text><xsl:value-of select="description"/>
</xsl:template>
<xsl:template match="constraint"> <!-- FIXME - need to work out where this goes for plain constraint -->
<xsl:text>< "</xsl:text><xsl:value-of select="description"/><text>" as Natural ></text>
</xsl:template>
<!-- I think that these specialized constraints have disappeared now -->
<xsl:template match="constraint/minValue">
<xsl:value-of select="concat(' min="',.,'"')"/>
</xsl:template>
<xsl:template match="constraint/minLength">
<xsl:value-of select="concat(' minlen=',.)"/>
</xsl:template>
<xsl:template match="constraint/maxLength">
<xsl:if test="number(.) ne -1">
<xsl:value-of select="concat(' maxlen=',.)"/>
</xsl:if>
</xsl:template>
<xsl:template match="constraint/length">
<xsl:value-of select="concat(' len=',.)"/>
</xsl:template>
<xsl:template match="constraint/uniqueGlobally">
<xsl:text> unique globally</xsl:text>
</xsl:template>
<xsl:template match="constraint/uniqueInCollection">
<xsl:text> unique</xsl:text>
</xsl:template>
<xsl:template match="text()|*">
<xsl:value-of select="."/>
</xsl:template>
<!-- catchall to indicate where there might be missed element to highlight when VODML might have changed
<xsl:template match="*">
<xsl:value-of select="concat('***',name(.),'*** ')"/>
<xsl:apply-templates/>
</xsl:template>
-->
</xsl:stylesheet>