forked from mablesly/xsd2html2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsd2html2xml.xsl
317 lines (275 loc) · 12.6 KB
/
xsd2html2xml.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?xml version="1.0"?>
<xsl:stylesheet
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:include href="css/default-style.xsl" />
<xsl:include href="js/event-handlers.xsl" />
<xsl:include href="js/html-populators.xsl" />
<xsl:include href="js/initial-calls.xsl" />
<xsl:include href="js/polyfills.xsl" />
<xsl:include href="js/value-fixers.xsl" />
<xsl:include href="js/xml-generators.xsl" />
<xsl:include href="init.xsl" />
<xsl:include href="config.xsl" />
<xsl:include href="utils/appinfo.xsl" />
<xsl:include href="utils/attr-value.xsl" />
<xsl:include href="utils/documentation.xsl" />
<xsl:include href="utils/gui-attributes.xsl" />
<xsl:include href="utils/gui.xsl" />
<xsl:include href="utils/log.xsl" />
<xsl:include href="utils/namespaces.xsl" />
<xsl:include href="utils/nodeset-xslt2plus.xsl" />
<xsl:include href="utils/serialize.xsl" />
<xsl:include href="utils/strings.xsl" />
<xsl:include href="utils/types.xsl" />
<xsl:include href="matchers/all.xsl" />
<xsl:include href="matchers/attributeGroup@ref.xsl" />
<xsl:include href="matchers/attributes.xsl" />
<xsl:include href="matchers/choice.xsl" />
<xsl:include href="matchers/element-attribute@ref.xsl" />
<xsl:include href="matchers/element-complexType.xsl" />
<xsl:include href="matchers/element-simpleContent.xsl" />
<xsl:include href="matchers/element-simpleType.xsl" />
<xsl:include href="matchers/element@type.xsl" />
<xsl:include href="matchers/element.xsl" />
<xsl:include href="matchers/group@ref.xsl" />
<xsl:include href="matchers/sequence.xsl" />
<xsl:include href="matchers/unsupported.xsl" />
<xsl:include href="handlers/complex-elements.xsl" />
<xsl:include href="handlers/default-types.xsl" />
<xsl:include href="handlers/enumerations.xsl" />
<xsl:include href="handlers/extensions.xsl" />
<xsl:include href="handlers/multiline-strings.xsl" />
<xsl:include href="handlers/root-element.xsl" />
<xsl:include href="handlers/simple-elements.xsl" />
<xsl:variable name="templates" select="document('')/*/xsl:template"/>
<xsl:variable name="root-element-stylesheet" select="document('handlers/root-element.xsl')" />
<xsl:variable name="complex-elements-stylesheet" select="document('handlers/complex-elements.xsl')" />
<xsl:variable name="enumerations-stylesheet" select="document('handlers/enumerations.xsl')" />
<xsl:variable name="extensions-stylesheet" select="document('handlers/extensions.xsl')" />
<xsl:variable name="element-stylesheet" select="document('matchers/element@type.xsl')" />
<xsl:variable name="element-attribute-stylesheet" select="document('matchers/element-attribute@ref.xsl')" />
<xsl:variable name="element-complexType-stylesheet" select="document('matchers/element-complexType.xsl')" />
<xsl:variable name="element-simpleType-stylesheet" select="document('matchers/element-simpleType.xsl')" />
<xsl:variable name="element-simpleContent-stylesheet" select="document('matchers/element-simpleContent.xsl')" />
<xsl:variable name="group-stylesheet" select="document('matchers/group@ref.xsl')" />
<xsl:variable name="attributeGroup-stylesheet" select="document('matchers/attributeGroup@ref.xsl')" />
<xsl:variable name="attr-value-stylesheet" select="document('utils/attr-value.xsl')" />
<xsl:variable name="gui-attributes-stylesheet" select="document('utils/gui-attributes.xsl')" />
<xsl:variable name="namespaces-stylesheet" select="document('utils/namespaces.xsl')" />
<xsl:variable name="types-stylesheet" select="document('utils/types.xsl')" />
<xsl:template match="*" />
<xsl:template match="/*">
<xsl:element name="html">
<xsl:choose>
<!-- check if noNamespaceSchemaLocation contains a value -->
<xsl:when test="@xsi:noNamespaceSchemaLocation">
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>XML file detected. Loading schema: </xsl:text><xsl:value-of select="@xsi:noNamespaceSchemaLocation" />
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="add-metadata">
<xsl:with-param name="xml-document" select="." />
</xsl:call-template>
<xsl:for-each select="document(@xsi:noNamespaceSchemaLocation)/*">
<xsl:call-template name="handle-schema" />
</xsl:for-each>
</xsl:when>
<!-- check if schemaLocation contains a value -->
<xsl:when test="@xsi:schemaLocation">
<xsl:choose>
<!-- check if schemaLocation contains spaces (and thus namespace-location combinations) -->
<xsl:when test="contains(@xsi:schemaLocation, ' ')">
<!-- extract the namespace of the root element -->
<xsl:variable name="default-namespace">
<xsl:value-of select="namespace::*[name() = substring-before(name(), concat(':', local-name()))]" />
</xsl:variable>
<!-- extract schema location relative to default namespace -->
<xsl:variable name="schema-location">
<xsl:value-of select="normalize-space(substring-after(@xsi:schemaLocation, $default-namespace))" />
</xsl:variable>
<xsl:choose>
<!-- if schema-location still contains spaces, break off before the first one to find the schema location -->
<xsl:when test="contains($schema-location, ' ')">
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>XML file detected. Loading schema: </xsl:text><xsl:value-of select="normalize-space(substring-before($schema-location, ' '))" />
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="add-metadata">
<xsl:with-param name="xml-document" select="." />
</xsl:call-template>
<xsl:for-each select="document(normalize-space(substring-before($schema-location, ' ')))/*">
<xsl:call-template name="handle-schema" />
</xsl:for-each>
</xsl:when>
<!-- otherwise, the remaining value should point to a schema -->
<xsl:otherwise>
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>XML file detected. Loading schema: </xsl:text><xsl:value-of select="$schema-location" />
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="add-metadata">
<xsl:with-param name="xml-document" select="." />
</xsl:call-template>
<xsl:for-each select="document($schema-location)/*">
<xsl:call-template name="handle-schema" />
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- if not, assume the value points to a schema -->
<xsl:otherwise>
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>XML file detected. Loading schema: </xsl:text><xsl:value-of select="@xsi:schemaLocation" />
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="add-metadata">
<xsl:with-param name="xml-document" select="." />
</xsl:call-template>
<xsl:for-each select="document(@xsi:schemaLocation)/*">
<xsl:call-template name="handle-schema" />
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- else, assume an XSD document -->
<xsl:otherwise>
<xsl:call-template name="add-metadata" />
<xsl:call-template name="handle-schema" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<!-- add metadata, including optionally xml document, to head section -->
<xsl:template name="add-metadata">
<xsl:param name="xml-document"></xsl:param>
<xsl:element name="head">
<xsl:element name="title">
<xsl:value-of select="$config-title" />
</xsl:element>
<!-- add stylesheet and script elements -->
<xsl:if test="not($config-style = '')">
<xsl:element name="link">
<xsl:attribute name="rel">stylesheet</xsl:attribute>
<xsl:attribute name="type">text/css</xsl:attribute>
<xsl:attribute name="href">
<xsl:value-of select="$config-style" />
</xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:call-template name="add-style" />
<xsl:call-template name="add-polyfills" />
<xsl:call-template name="add-xml-generators" />
<xsl:call-template name="add-html-populators" />
<xsl:call-template name="add-value-fixers" />
<xsl:call-template name="add-event-handlers" />
<xsl:call-template name="add-initial-calls" />
<xsl:if test="not($config-script = '')">
<xsl:element name="script">
<xsl:attribute name="type">text/javascript</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="$config-script" />
</xsl:attribute>
</xsl:element>
</xsl:if>
<!-- add a generator meta element -->
<xsl:element name="meta">
<xsl:attribute name="name">generator</xsl:attribute>
<xsl:attribute name="content">XSD2HTML2XML v3: https://github.com/MichielCM/xsd2html2xml</xsl:attribute>
<!-- if an xml document has been provided, save it as an attribute to the meta element -->
<xsl:if test="not($xml-document = '')">
<xsl:attribute name="data-xsd2html2xml-source">
<xsl:apply-templates select="$xml-document" mode="serialize" />
</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- root match from which all other templates are invoked -->
<xsl:template name="handle-schema">
<xsl:call-template name="inform">
<xsl:with-param name="message">
<text>XSD file detected.</text>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="init" />
<xsl:call-template name="log">
<xsl:with-param name="reference">xs:schema</xsl:with-param>
</xsl:call-template>
<!-- save root-document for future use -->
<xsl:variable name="root-document" select="/*" />
<!-- load root-namespaces for future use -->
<xsl:variable name="root-namespaces">
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>Namespaces in root document:</xsl:text>
</xsl:with-param>
</xsl:call-template>
<xsl:for-each select="namespace::*">
<xsl:element name="root-namespace">
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:if test="not(name() = '')">
<xsl:value-of select="name()" />
<xsl:text>:</xsl:text>
</xsl:if>
<xsl:value-of select="." />
</xsl:with-param>
</xsl:call-template>
<xsl:if test="not(name() = '')">
<xsl:attribute name="prefix">
<xsl:value-of select="name()" />
<xsl:text>:</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="namespace">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:element name="body">
<xsl:element name="form">
<!-- disable default form action -->
<xsl:attribute name="action">javascript:void(0);</xsl:attribute>
<!-- add class for scoping -->
<xsl:attribute name="class">xsd2html2xml</xsl:attribute>
<!-- specify action on submit -->
<xsl:attribute name="onsubmit">
<xsl:value-of select="$config-callback" />
<xsl:text>(htmlToXML(this));</xsl:text>
</xsl:attribute>
<!-- add custom appinfo data -->
<xsl:for-each select="xs:annotation/xs:appinfo/*">
<xsl:call-template name="add-appinfo" />
</xsl:for-each>
<!-- start parsing the XSD from the top -->
<xsl:for-each select="xs:element">
<!-- use the element with the position indicated in config-root as root, or default to the first (usually the only) root element -->
<xsl:if test="($config-root = '' and position() = 1) or position() = $config-root">
<xsl:call-template name="forward-root">
<xsl:with-param name="stylesheet" select="$root-element-stylesheet" />
<xsl:with-param name="template">handle-root-element</xsl:with-param>
<xsl:with-param name="root-document" select="//xs:schema" />
<xsl:with-param name="root-namespaces" select="$root-namespaces" />
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<xsl:element name="button">
<xsl:attribute name="type">submit</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:element>
<xsl:call-template name="inform">
<xsl:with-param name="message">
<xsl:text>XSLT processing completed.</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>