1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
2 |
|
3 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
4 |
|
5 |
<!-- |
6 |
|
7 |
xupdate2xsl: Translate XML document from namespace 'xupdate' to 'xsl'. |
8 |
|
9 |
Purpose of this XML Stylesheet is to implement a set of templates |
10 |
to translate XUpdate lingo into an intermediate xslt stylesheet |
11 |
which actually performs the update to the original xml document |
12 |
in a second step. |
13 |
|
14 |
The required glue code - written in Perl - is available in module |
15 |
XML::XUpdate::XSLT. Please have a look at this in order to write |
16 |
code to use this stylesheet from other language bindings. |
17 |
|
18 |
Please have a look at the XUpdate specs and associated resources: |
19 |
Requirements: http://www.xmldb.org/xupdate/xupdate-req.html |
20 |
Working Draft: http://www.xmldb.org/xupdate/xupdate-wd.html |
21 |
|
22 |
ChangeLog: |
23 |
2003-04-30: first alpha |
24 |
2003-05-01: works: xupdate:insert-after (elements/attributes) |
25 |
2003-05-06: works: xupdate:insert-before (elements/attributes) |
26 |
|
27 |
--> |
28 |
|
29 |
<xsl:output method="xml" /> |
30 |
|
31 |
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
32 |
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
33 |
<!-- activate this --> |
34 |
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
35 |
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
36 |
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
37 |
|
38 |
|
39 |
<!-- 2. This is the translation part: XUpdate becomes XSLT --> |
40 |
|
41 |
<!-- lib --> |
42 |
<!-- This node "encapsulates" common core infrastructure. --> |
43 |
<xsl:template match="xupdate:modifications"> |
44 |
|
45 |
<!-- <xsl:template name="identity_template_rule"> --> |
46 |
<xsl:element name="xsl:template"> |
47 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
48 |
|
49 |
<!-- An "identity template rule" - original version by Mike Kay, Software AG --> |
50 |
<!-- Please visit [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] --> |
51 |
<!-- Translated to "self hosting xsl": Andreas Motl, andreas.motl@ilo.de --> |
52 |
<!-- FIXME: Could this be used to replace passthru-logic at 1. ? --> |
53 |
<xsl:element name="xsl:copy"> |
54 |
<xsl:element name="xsl:copy-of"> |
55 |
<xsl:attribute name="select">@*</xsl:attribute> |
56 |
</xsl:element> |
57 |
<xsl:element name="xsl:apply-templates" /> |
58 |
</xsl:element> |
59 |
|
60 |
</xsl:element> |
61 |
<!-- </xsl:template> --> |
62 |
|
63 |
<!-- |
64 |
1. Note: Former custom passthru logic (copy all untouched nodes) |
65 |
now completely replaced by Mike Kay's "identity template rule". |
66 |
What is left here is just the required overriding of some builtin rules |
67 |
to (e.g.) not to loose comments. |
68 |
--> |
69 |
<xsl:comment> 1. override built-in rules </xsl:comment> |
70 |
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
71 |
<xsl:element name="xsl:template"><xsl:attribute name="match">comment()</xsl:attribute> |
72 |
<xsl:element name="xsl:call-template"> |
73 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
74 |
</xsl:element> |
75 |
</xsl:element> |
76 |
|
77 |
<!-- continue with all inline nodes --> |
78 |
<xsl:apply-templates /> |
79 |
|
80 |
</xsl:template> |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
<!-- code --> |
87 |
<!-- This node "encapsulates" infrastructure for handling the directives. --> |
88 |
|
89 |
<!-- Dispatch all "XUpdate Services" here. --> |
90 |
<!-- FIXME: Actually implement action = "CREATE|RETRIEVE|UPDATE|DELETE"!!! --> |
91 |
<xsl:template match="xupdate:insert-after"> |
92 |
<!-- <xsl:variable name="select" select="@select">Hello World!</xsl:variable> --> |
93 |
<xsl:call-template name="findcontext"> |
94 |
<xsl:with-param name="service">Insert</xsl:with-param> |
95 |
<xsl:with-param name="position">after</xsl:with-param> |
96 |
</xsl:call-template> |
97 |
</xsl:template> |
98 |
|
99 |
<xsl:template match="xupdate:insert-before"> |
100 |
<xsl:call-template name="findcontext"> |
101 |
<xsl:with-param name="service">Insert</xsl:with-param> |
102 |
<xsl:with-param name="position">before</xsl:with-param> |
103 |
</xsl:call-template> |
104 |
</xsl:template> |
105 |
|
106 |
<xsl:template match="xupdate:append"> |
107 |
<xsl:call-template name="findcontext"> |
108 |
<xsl:with-param name="service">Append</xsl:with-param> |
109 |
<!-- <xsl:with-param name="position">inline</xsl:with-param> --> |
110 |
<xsl:with-param name="test">.</xsl:with-param> |
111 |
</xsl:call-template> |
112 |
</xsl:template> |
113 |
|
114 |
<xsl:template match="xupdate:update"> |
115 |
<xsl:call-template name="findcontext"> |
116 |
<xsl:with-param name="service">Update</xsl:with-param> |
117 |
<!-- <xsl:with-param name="position">after</xsl:with-param> --> |
118 |
<xsl:with-param name="test">.</xsl:with-param> |
119 |
</xsl:call-template> |
120 |
</xsl:template> |
121 |
|
122 |
<xsl:template match="xupdate:remove"> |
123 |
<xsl:call-template name="findcontext"> |
124 |
<xsl:with-param name="service">Remove</xsl:with-param> |
125 |
<!-- <xsl:with-param name="position">after</xsl:with-param> --> |
126 |
<xsl:with-param name="test">.</xsl:with-param> |
127 |
</xsl:call-template> |
128 |
</xsl:template> |
129 |
|
130 |
<xsl:template match="xupdate:rename"> |
131 |
<xsl:call-template name="findcontext"> |
132 |
<xsl:with-param name="service">Rename</xsl:with-param> |
133 |
<!-- <xsl:with-param name="position">after</xsl:with-param> --> |
134 |
<xsl:with-param name="test">.</xsl:with-param> |
135 |
</xsl:call-template> |
136 |
</xsl:template> |
137 |
|
138 |
|
139 |
|
140 |
<xsl:template name="findcontext"> |
141 |
|
142 |
<xsl:comment> 2. context finder </xsl:comment> |
143 |
|
144 |
<!-- new of 2003-05-06: introducing parameters here to handle all cases --> |
145 |
<xsl:param name="service" /> |
146 |
<xsl:param name="position" /> |
147 |
<!-- new of 2003-05-07: replacement for test if undef --> |
148 |
<xsl:param name="test" /> |
149 |
|
150 |
<!-- |
151 |
<xsl:comment> |
152 |
service: <xsl:value-of select="$service" /> |
153 |
position: <xsl:value-of select="$position" /> |
154 |
test: <xsl:value-of select="$test" /> |
155 |
</xsl:comment> |
156 |
--> |
157 |
|
158 |
<!-- this resembles some parts of the CRUD API - retranslated to self-hosting xsl --> |
159 |
|
160 |
<!-- V1 --> |
161 |
<!-- <xsl:element name="xsl:template"> --> |
162 |
<!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> --> |
163 |
|
164 |
<!-- fallback --> |
165 |
<xsl:element name="xsl:template"> |
166 |
<xsl:attribute name="match">*</xsl:attribute> |
167 |
<xsl:element name="xsl:call-template"> |
168 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
169 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
170 |
</xsl:element> |
171 |
</xsl:element> |
172 |
|
173 |
<!-- V2 --> |
174 |
<xsl:element name="xsl:template"> |
175 |
<!-- <xsl:attribute name="match">/addresses/address</xsl:attribute> --> |
176 |
<!-- <xsl:attribute name="match">*</xsl:attribute> --> |
177 |
<xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> |
178 |
|
179 |
<xsl:element name="xsl:choose"> |
180 |
<!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> --> |
181 |
<xsl:element name="xsl:when"> |
182 |
<!-- <xsl:attribute name="test"><xsl:value-of select="@select" /></xsl:attribute> --> |
183 |
<xsl:attribute name="test"><xsl:value-of select="@test" /><xsl:value-of select="$test" /></xsl:attribute> |
184 |
|
185 |
<xsl:choose> |
186 |
<xsl:when test="{$service}=Insert"> |
187 |
|
188 |
<xsl:comment> 2.a. Insert </xsl:comment> |
189 |
|
190 |
<!-- Implementation of "xupdate:insert-before" --> |
191 |
<xsl:choose> |
192 |
<xsl:when test="{$position}=before"> |
193 |
<xsl:apply-templates /> |
194 |
</xsl:when> |
195 |
</xsl:choose> |
196 |
|
197 |
<!-- call the "identity template rule" to passthru all childnodes --> |
198 |
<xsl:element name="xsl:call-template"> |
199 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
200 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
201 |
</xsl:element> |
202 |
|
203 |
<!-- Implementation of "xupdate:insert-after" --> |
204 |
<xsl:choose> |
205 |
<xsl:when test="{$position}=after"> |
206 |
<xsl:apply-templates /> |
207 |
</xsl:when> |
208 |
</xsl:choose> |
209 |
|
210 |
</xsl:when> |
211 |
|
212 |
<!-- Implementation of "xupdate:append" --> |
213 |
<xsl:when test="{$service}=Append"> |
214 |
<xsl:comment> 2.b. Append </xsl:comment> |
215 |
<!-- V1 --> |
216 |
<!-- <xsl:apply-templates /> --> |
217 |
<!-- V2 --> |
218 |
<xsl:element name="xsl:copy"> |
219 |
<!-- |
220 |
<xsl:element name="xsl:copy-of"> |
221 |
<xsl:attribute name="select">@*</xsl:attribute> |
222 |
</xsl:element> |
223 |
--> |
224 |
<!-- Keep content ... --> |
225 |
<xsl:element name="xsl:apply-templates" /> |
226 |
<!-- Append this: --> |
227 |
<xsl:apply-templates /> |
228 |
</xsl:element> |
229 |
</xsl:when> |
230 |
|
231 |
<!-- Implementation of "xupdate:update" --> |
232 |
<xsl:when test="{$service}=Update"> |
233 |
<xsl:comment> 2.c. Update </xsl:comment> |
234 |
<!-- V1 --> |
235 |
<!-- <xsl:apply-templates /> --> |
236 |
<!-- V2 --> |
237 |
<xsl:element name="xsl:copy"> |
238 |
<!-- |
239 |
<xsl:element name="xsl:copy-of"> |
240 |
<xsl:attribute name="select">@*</xsl:attribute> |
241 |
</xsl:element> |
242 |
--> |
243 |
<!-- Keep content ... --> |
244 |
<!-- <xsl:element name="xsl:apply-templates" /> --> |
245 |
<!-- Add (replace through) this: --> |
246 |
<xsl:apply-templates /> |
247 |
</xsl:element> |
248 |
</xsl:when> |
249 |
|
250 |
<!-- Implementation of "xupdate:remove" --> |
251 |
<xsl:when test="{$service}=Remove"> |
252 |
<xsl:comment> 2.d. Remove </xsl:comment> |
253 |
</xsl:when> |
254 |
|
255 |
<!-- Implementation of "xupdate:rename" --> |
256 |
<xsl:when test="{$service}=Rename"> |
257 |
<xsl:comment> 2.e. Rename (Remove context and Append modified identity template) </xsl:comment> |
258 |
|
259 |
<xsl:comment> [5.a.] modify node name </xsl:comment> |
260 |
<xsl:element name="xsl:element"> |
261 |
<!-- this renames the node (change the "name"-attribute) --> |
262 |
<xsl:attribute name="name"> |
263 |
<xsl:apply-templates /> |
264 |
</xsl:attribute> |
265 |
<!-- this propagates the original content --> |
266 |
<xsl:element name="xsl:apply-templates" /> |
267 |
</xsl:element> |
268 |
|
269 |
</xsl:when> |
270 |
|
271 |
</xsl:choose> |
272 |
|
273 |
</xsl:element> |
274 |
<xsl:element name="xsl:otherwise"> |
275 |
|
276 |
<!-- call the "identity template rule" to passthru all childnodes --> |
277 |
<xsl:element name="xsl:call-template"> |
278 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
279 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
280 |
</xsl:element> |
281 |
|
282 |
<!-- <xsl:apply-templates /> --> |
283 |
</xsl:element> |
284 |
|
285 |
</xsl:element> |
286 |
</xsl:element> |
287 |
|
288 |
<!-- <xsl:apply-templates /> --> |
289 |
|
290 |
</xsl:template> |
291 |
|
292 |
|
293 |
<!-- |
294 |
Rewrite XUpdate elements to XSLT elements. |
295 |
These templates pass through all attributes |
296 |
and childnodes rewriting the tagname only. |
297 |
FIXME: The redundant encoding for each XUpdate |
298 |
element type seems dummy. Could this be stripped |
299 |
shorter somehow sometimes? |
300 |
--> |
301 |
|
302 |
<xsl:template match="xupdate:element"> |
303 |
<xsl:comment> 3.a. vivify generic node </xsl:comment> |
304 |
<xsl:element name="xsl:element"> |
305 |
<xsl:copy-of select="@*"/> |
306 |
<xsl:apply-templates /> |
307 |
</xsl:element> |
308 |
</xsl:template> |
309 |
|
310 |
<xsl:template match="xupdate:attribute"> |
311 |
<xsl:comment> 3.b. vivify node attributes </xsl:comment> |
312 |
<xsl:element name="xsl:attribute"> |
313 |
<xsl:copy-of select="@*"/> |
314 |
<xsl:apply-templates /> |
315 |
</xsl:element> |
316 |
</xsl:template> |
317 |
|
318 |
<xsl:template match="xupdate:text"> |
319 |
<xsl:comment> 4.a. vivify text node </xsl:comment> |
320 |
<xsl:element name="xsl:text"> |
321 |
<xsl:copy-of select="@*"/> |
322 |
<xsl:apply-templates /> |
323 |
</xsl:element> |
324 |
</xsl:template> |
325 |
|
326 |
<xsl:template match="xupdate:processing-instruction"> |
327 |
<xsl:comment> 4.b. vivify PI node </xsl:comment> |
328 |
<xsl:element name="xsl:processing-instruction"> |
329 |
<xsl:copy-of select="@*"/> |
330 |
<xsl:apply-templates /> |
331 |
</xsl:element> |
332 |
</xsl:template> |
333 |
|
334 |
<xsl:template match="xupdate:comment"> |
335 |
<xsl:comment> 4.c. vivify comment node </xsl:comment> |
336 |
<xsl:element name="xsl:comment"> |
337 |
<xsl:copy-of select="@*"/> |
338 |
<xsl:apply-templates /> |
339 |
</xsl:element> |
340 |
</xsl:template> |
341 |
|
342 |
<!-- |
343 |
xsl for [2.9 Variables and Values of Variables] |
344 |
Encapsulates block payload into a named template. |
345 |
--> |
346 |
<xsl:template match="xupdate:variable"> |
347 |
<xsl:comment> 5.a. vivify xsl-element "variable" </xsl:comment> |
348 |
|
349 |
<xsl:element name="xsl:template"> |
350 |
<xsl:attribute name="name">Variables</xsl:attribute> |
351 |
|
352 |
<!-- |
353 |
<xsl:element name="xsl:variable"> |
354 |
<xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute> |
355 |
--> |
356 |
|
357 |
<xsl:element name="xsl:apply-templates"> |
358 |
<xsl:attribute name="select"><xsl:value-of select="@select" /></xsl:attribute> |
359 |
</xsl:element> |
360 |
|
361 |
<!-- <xsl:value-of select="@select" /> --> |
362 |
<xsl:value-of select="." /> |
363 |
<!-- |
364 |
</xsl:element> |
365 |
--> |
366 |
|
367 |
</xsl:element> |
368 |
|
369 |
</xsl:template> |
370 |
|
371 |
<xsl:template match="xupdate:value-of"> |
372 |
<xsl:comment> 5.b. vivify xsl-element "value-of" </xsl:comment> |
373 |
<xsl:element name="xsl:call-template"> |
374 |
<xsl:attribute name="name">Variables</xsl:attribute> |
375 |
</xsl:element> |
376 |
<xsl:element name="xsl:value-of"> |
377 |
<xsl:copy-of select="@*"/> |
378 |
<!-- <xsl:apply-templates /> --> |
379 |
<!-- this propagates the original content --> |
380 |
<!-- <xsl:element name="xsl:apply-templates"> --> |
381 |
<!-- <xsl:attribute name="" > --> |
382 |
<!-- </xsl:element> --> |
383 |
</xsl:element> |
384 |
</xsl:template> |
385 |
|
386 |
|
387 |
</xsl:stylesheet> |
388 |
|