/[volute]/trunk/projects/theory/snapdm/res/xmi2xsd.xsl
ViewVC logotype

Contents of /trunk/projects/theory/snapdm/res/xmi2xsd.xsl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 145 - (show annotations)
Tue Apr 1 18:11:01 2008 UTC (13 years, 6 months ago) by bourges.laurent
File MIME type: application/xml
File size: 9675 byte(s)
cleanup + XSLT 2 version
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5 xmlns:x2s="http://ns.eurovotech.org/registry-metadata#"
6 xmlns:uml="http://schema.omg.org/spec/UML/2.0"
7 xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
8
9
10 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
11 <xsl:strip-space elements="*" />
12
13 <!-- xml index on xml:id -->
14 <xsl:key name="classid" match="*" use="@xmi:id"/>
15
16
17 <!-- namespaces -->
18
19 <xsl:variable name="snap-ns">http://www.ivoa.net/xml/theory/SNAPSimulation_types/v0.1</xsl:variable>
20 <xsl:variable name="xsd-ns">http://www.w3.org/2001/XMLSchema</xsl:variable>
21 <xsl:variable name="xsd-prefix">xsd:</xsl:variable>
22
23
24 <!-- main -->
25 <xsl:template match="/">
26 <xsl:choose>
27 <xsl:when test="namespace-uri(/*) != 'http://schema.omg.org/spec/XMI/2.1'">
28 <x2s:ERROR>Wrong namespace: this script can convert only XMI v2.1</x2s:ERROR>
29 </xsl:when>
30 <xsl:otherwise>
31 <xsl:apply-templates select="xmi:XMI"/>
32 </xsl:otherwise>
33 </xsl:choose>
34 </xsl:template>
35
36
37
38 <xsl:template match="xmi:XMI">
39 <xsd:schema>
40 <xsl:attribute name="xml:snap">
41 <xsl:value-of select="$snap-ns"/>
42 </xsl:attribute>
43 <xsl:attribute name="xml:xsd">
44 <xsl:value-of select="$xsd-ns"/>
45 </xsl:attribute>
46 <xsd:annotation>
47 <xsd:documentation>
48 <xsl:text>Generated from XMI.</xsl:text>
49 </xsd:documentation>
50 </xsd:annotation>
51 <xsl:apply-templates select="uml:Model"/>
52 </xsd:schema>
53 </xsl:template>
54
55
56
57 <xsl:template match="uml:Model">
58 <xsl:apply-templates select=".//*[@xmi:type='uml:Class' and @name]"/>
59 <xsl:apply-templates select=".//*[@xmi:type='uml:DataType' and @name]"/>
60 <xsl:apply-templates select=".//*[@xmi:type='uml:Enumeration']"/>
61 </xsl:template>
62
63
64
65 <xsl:template match="*[@xmi:type='uml:Class']">
66 <xsd:complexType>
67 <xsl:attribute name="name">
68 <xsl:value-of select="@name"/>
69 </xsl:attribute>
70 <xsl:if test="@isAbstract">
71 <xsl:attribute name="abstract">
72 <xsl:text>true</xsl:text>
73 </xsl:attribute>
74 </xsl:if>
75 <xsl:apply-templates select="ownedComment"/>
76 <xsl:choose>
77 <xsl:when test="*[@xmi:type='uml:Generalization']">
78 <xsl:apply-templates select="*[@xmi:type='uml:Generalization']"/>
79 </xsl:when>
80 <xsl:otherwise>
81 <xsl:call-template name="class-content">
82 <xsl:with-param name="class" select="."/>
83 </xsl:call-template>
84 </xsl:otherwise>
85 </xsl:choose>
86 </xsd:complexType>
87 </xsl:template>
88
89
90
91 <xsl:template match="*[@xmi:type='uml:DataType']">
92 <xsl:choose>
93 <xsl:when test="*[@xmi:type='uml:Property']">
94 <xsl:call-template name="complex-datatype"/>
95 </xsl:when>
96 <xsl:otherwise>
97 <xsl:call-template name="simple-datatype"/>
98 </xsl:otherwise>
99 </xsl:choose>
100 </xsl:template>
101
102
103
104 <xsl:template name="complex-datatype">
105 <xsd:complexType>
106 <xsl:attribute name="name">
107 <xsl:value-of select="@name"/>
108 </xsl:attribute>
109 <xsl:if test="@isAbstract">
110 <xsl:attribute name="abstract">
111 <xsl:text>true</xsl:text>
112 </xsl:attribute>
113 </xsl:if>
114 <xsl:apply-templates select="ownedComment"/>
115 <xsl:choose>
116 <xsl:when test="*[@xmi:type='uml:Generalization']">
117 <xsl:apply-templates select="*[@xmi:type='uml:Generalization']"/>
118 </xsl:when>
119 <xsl:otherwise>
120 <xsl:call-template name="class-content">
121 <xsl:with-param name="class" select="."/>
122 </xsl:call-template>
123 </xsl:otherwise>
124 </xsl:choose>
125 </xsd:complexType>
126 </xsl:template>
127
128
129
130 <xsl:template name="simple-datatype">
131 <xsd:simpleType>
132 <xsl:attribute name="name">
133 <xsl:value-of select="@name"/>
134 </xsl:attribute>
135 <xsl:apply-templates select="ownedComment"/>
136 <xsd:restriction>
137 <xsl:attribute name="base">
138 <xsl:choose>
139 <xsl:when test="*[@xmi:type='uml:Generalization']">
140 <xsl:call-template name="class-from-id">
141 <xsl:with-param name="id">
142 <xsl:value-of select="*[@xmi:type='uml:Generalization']/@general"/>
143 </xsl:with-param>
144 </xsl:call-template>
145 </xsl:when>
146 <xsl:otherwise>
147 <xsl:text>xsd:string</xsl:text>
148 </xsl:otherwise>
149 </xsl:choose>
150 </xsl:attribute>
151 </xsd:restriction>
152 </xsd:simpleType>
153 </xsl:template>
154
155
156
157 <xsl:template match="*[@xmi:type='uml:Generalization']">
158 <xsd:complexContent>
159 <xsd:extension>
160 <xsl:attribute name="base">
161 <xsl:call-template name="class-from-id">
162 <xsl:with-param name="id">
163 <xsl:value-of select="@general"/>
164 </xsl:with-param>
165 </xsl:call-template>
166 </xsl:attribute>
167 <xsl:call-template name="class-content">
168 <xsl:with-param name="class" select=".."/>
169 </xsl:call-template>
170 </xsd:extension>
171 </xsd:complexContent>
172 </xsl:template>
173
174
175
176
177 <xsl:template name="class-content">
178 <xsl:param name="class"/>
179 <xsd:sequence>
180 <xsl:apply-templates select="$class//*[@xmi:type='uml:Property']"/>
181 </xsd:sequence>
182 </xsl:template>
183
184
185
186 <xsl:template match="*[@xmi:type='uml:Enumeration']">
187 <xsd:simpleType>
188 <xsl:attribute name="name">
189 <xsl:value-of select="@name"/>
190 </xsl:attribute>
191 <xsl:apply-templates select="ownedComment"/>
192 <xsd:restriction>
193 <xsl:attribute name="base">
194 <xsl:text>xsd:string</xsl:text>
195 </xsl:attribute>
196 <xsl:apply-templates select="*[@xmi:type='uml:EnumerationLiteral']"/>
197 </xsd:restriction>
198 </xsd:simpleType>
199 </xsl:template>
200
201
202
203 <xsl:template match="*[@xmi:type='uml:EnumerationLiteral']">
204 <xsd:enumeration>
205 <xsl:attribute name="value">
206 <xsl:value-of select="@name"/>
207 <!--
208 <xsl:value-of select="translate(substring(@name,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
209 <xsl:value-of select="translate(substring(@name,2),' !#$%&amp;()*+,-./:;&lt;=&gt;?@[\]^_`{|}~','_')"/>
210 -->
211 </xsl:attribute>
212 </xsd:enumeration>
213 </xsl:template>
214
215
216
217 <xsl:template match="*[@xmi:type='uml:Property']">
218 <xsl:if test="../@xmi:type='uml:Class' or ../@xmi:type='uml:DataType'">
219 <xsl:variable name="c" select="key('classid',@type)"/>
220 <xsd:element>
221 <xsl:attribute name="name">
222 <xsl:value-of select="@name"/>
223 </xsl:attribute>
224 <xsl:attribute name="type">
225 <xsl:choose>
226 <xsl:when test="not(./@aggregation='composite') and $c/@xmi:type = 'uml:Class'">
227 <xsl:text>xsd:IDREF</xsl:text>
228 </xsl:when>
229 <xsl:otherwise>
230 <xsl:call-template name="class-from-id">
231 <xsl:with-param name="id">
232 <xsl:value-of select="@type"/>
233 </xsl:with-param>
234 </xsl:call-template>
235 </xsl:otherwise>
236 </xsl:choose>
237 </xsl:attribute>
238 <xsl:attribute name="minOccurs">
239 <xsl:choose>
240 <xsl:when test="lowerValue/@value">
241 <xsl:value-of select="lowerValue/@value"/>
242 </xsl:when>
243 <xsl:otherwise>
244 <xsl:text>0</xsl:text>
245 </xsl:otherwise>
246 </xsl:choose>
247 </xsl:attribute>
248 <xsl:attribute name="maxOccurs">
249 <xsl:choose>
250 <xsl:when test="not(upperValue/@value)">
251 <xsl:text>1</xsl:text>
252 </xsl:when>
253 <xsl:when test="upperValue/@value = -1">
254 <xsl:text>unbounded</xsl:text>
255 </xsl:when>
256 <xsl:otherwise>
257 <xsl:value-of select="upperValue/@value"/>
258 </xsl:otherwise>
259 </xsl:choose>
260 </xsl:attribute>
261 <xsl:apply-templates select="ownedComment"/>
262 <xsl:if test="not(./@aggregation='composite') and $c/@xmi:type = 'uml:Class'">
263 <xsd:annotation>
264 <xsd:documentation>
265 <xsl:text>
266 This should be a reference to a </xsl:text><xsl:call-template name="class-from-id"><xsl:with-param name="id" select="@type"/></xsl:call-template><xsl:text>.
267 We use IDREF here, it may have to be changed to some other implementation of a reference.
268 </xsl:text>
269 </xsd:documentation>
270 </xsd:annotation>
271 </xsl:if>
272 </xsd:element>
273 </xsl:if>
274 </xsl:template>
275
276
277
278 <xsl:template match="ownedComment">
279 <xsd:annotation>
280 <xsd:documentation><xsl:text>
281 </xsl:text><xsl:value-of select="@body"/><xsl:text>
282 </xsl:text></xsd:documentation>
283 </xsd:annotation>
284 </xsl:template>
285
286
287
288 <xsl:template name="class-from-id">
289 <xsl:param name="id"/>
290 <xsl:param name="prefix"/>
291 <xsl:variable name="c" select="key('classid',$id)"/>
292 <xsl:choose>
293 <xsl:when test="$c/@xmi:type = 'uml:PrimitiveType'">
294 <xsl:value-of select="$xsd-prefix"/>
295 <xsl:value-of select="$c/@name"/>
296 </xsl:when>
297 <xsl:otherwise>
298 <xsl:value-of select="$prefix"/>
299 <xsl:value-of select="$c/@name"/>
300 </xsl:otherwise>
301 </xsl:choose>
302 </xsl:template>
303
304
305 </xsl:stylesheet>

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26