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 |
<xsl:param name="town" /> |
150 |
|
151 |
<xsl:variable name="town" select="huhu">haha</xsl:variable> |
152 |
|
153 |
<!-- |
154 |
<xsl:comment> |
155 |
service: <xsl:value-of select="$service" /> |
156 |
position: <xsl:value-of select="$position" /> |
157 |
test: <xsl:value-of select="$test" /> |
158 |
</xsl:comment> |
159 |
--> |
160 |
|
161 |
<!-- this resembles some parts of the CRUD API - retranslated to self-hosting xsl --> |
162 |
|
163 |
<!-- V1 --> |
164 |
<!-- <xsl:element name="xsl:template"> --> |
165 |
<!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> --> |
166 |
|
167 |
<!-- fallback --> |
168 |
<xsl:element name="xsl:template"> |
169 |
<xsl:attribute name="match">*</xsl:attribute> |
170 |
<xsl:element name="xsl:call-template"> |
171 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
172 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
173 |
</xsl:element> |
174 |
</xsl:element> |
175 |
|
176 |
<!-- V2 --> |
177 |
<xsl:element name="xsl:template"> |
178 |
<!-- <xsl:attribute name="match">/addresses/address</xsl:attribute> --> |
179 |
<!-- <xsl:attribute name="match">*</xsl:attribute> --> |
180 |
<xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> |
181 |
|
182 |
<xsl:element name="xsl:choose"> |
183 |
<!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> --> |
184 |
<xsl:element name="xsl:when"> |
185 |
<!-- <xsl:attribute name="test"><xsl:value-of select="@select" /></xsl:attribute> --> |
186 |
<xsl:attribute name="test"><xsl:value-of select="@test" /><xsl:value-of select="$test" /></xsl:attribute> |
187 |
|
188 |
<xsl:choose> |
189 |
<xsl:when test="{$service}=Insert"> |
190 |
|
191 |
<xsl:comment> 2.a. Insert </xsl:comment> |
192 |
|
193 |
<!-- Implementation of "xupdate:insert-before" --> |
194 |
<xsl:choose> |
195 |
<xsl:when test="{$position}=before"> |
196 |
<xsl:apply-templates /> |
197 |
</xsl:when> |
198 |
</xsl:choose> |
199 |
|
200 |
<!-- call the "identity template rule" to passthru all childnodes --> |
201 |
<xsl:element name="xsl:call-template"> |
202 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
203 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
204 |
</xsl:element> |
205 |
|
206 |
<!-- Implementation of "xupdate:insert-after" --> |
207 |
<xsl:choose> |
208 |
<xsl:when test="{$position}=after"> |
209 |
<xsl:apply-templates /> |
210 |
</xsl:when> |
211 |
</xsl:choose> |
212 |
|
213 |
</xsl:when> |
214 |
|
215 |
<!-- Implementation of "xupdate:append" --> |
216 |
<xsl:when test="{$service}=Append"> |
217 |
<xsl:comment> 2.b. Append </xsl:comment> |
218 |
<!-- V1 --> |
219 |
<!-- <xsl:apply-templates /> --> |
220 |
<!-- V2 --> |
221 |
<xsl:element name="xsl:copy"> |
222 |
<!-- |
223 |
<xsl:element name="xsl:copy-of"> |
224 |
<xsl:attribute name="select">@*</xsl:attribute> |
225 |
</xsl:element> |
226 |
--> |
227 |
<!-- Keep content ... --> |
228 |
<xsl:element name="xsl:apply-templates" /> |
229 |
<!-- Append this: --> |
230 |
<xsl:apply-templates /> |
231 |
</xsl:element> |
232 |
</xsl:when> |
233 |
|
234 |
<!-- Implementation of "xupdate:update" --> |
235 |
<xsl:when test="{$service}=Update"> |
236 |
<xsl:comment> 2.c. Update </xsl:comment> |
237 |
<!-- V1 --> |
238 |
<!-- <xsl:apply-templates /> --> |
239 |
<!-- V2 --> |
240 |
<xsl:element name="xsl:copy"> |
241 |
<!-- |
242 |
<xsl:element name="xsl:copy-of"> |
243 |
<xsl:attribute name="select">@*</xsl:attribute> |
244 |
</xsl:element> |
245 |
--> |
246 |
<!-- Keep content ... --> |
247 |
<!-- <xsl:element name="xsl:apply-templates" /> --> |
248 |
<!-- Add (replace through) this: --> |
249 |
<xsl:apply-templates /> |
250 |
</xsl:element> |
251 |
</xsl:when> |
252 |
|
253 |
<!-- Implementation of "xupdate:remove" --> |
254 |
<xsl:when test="{$service}=Remove"> |
255 |
<xsl:comment> 2.d. Remove </xsl:comment> |
256 |
</xsl:when> |
257 |
|
258 |
<!-- Implementation of "xupdate:rename" --> |
259 |
<xsl:when test="{$service}=Rename"> |
260 |
<xsl:comment> 2.e. Rename (Remove context and Append modified identity template) </xsl:comment> |
261 |
|
262 |
<xsl:comment> [5.a.] modify node name </xsl:comment> |
263 |
<xsl:element name="xsl:element"> |
264 |
<!-- this renames the node (change the "name"-attribute) --> |
265 |
<xsl:attribute name="name"> |
266 |
<xsl:apply-templates /> |
267 |
</xsl:attribute> |
268 |
<!-- this propagates the original content --> |
269 |
<xsl:element name="xsl:apply-templates" /> |
270 |
</xsl:element> |
271 |
|
272 |
</xsl:when> |
273 |
|
274 |
</xsl:choose> |
275 |
|
276 |
</xsl:element> |
277 |
<xsl:element name="xsl:otherwise"> |
278 |
|
279 |
<!-- call the "identity template rule" to passthru all childnodes --> |
280 |
<xsl:element name="xsl:call-template"> |
281 |
<xsl:attribute name="name">identity_template_rule</xsl:attribute> |
282 |
<!-- <xsl:attribute name="name">passthru</xsl:attribute> --> |
283 |
</xsl:element> |
284 |
|
285 |
<!-- <xsl:apply-templates /> --> |
286 |
</xsl:element> |
287 |
|
288 |
</xsl:element> |
289 |
</xsl:element> |
290 |
|
291 |
<!-- <xsl:apply-templates /> --> |
292 |
|
293 |
</xsl:template> |
294 |
|
295 |
|
296 |
<!-- |
297 |
Rewrite XUpdate elements to XSLT elements. |
298 |
These templates pass through all attributes |
299 |
and childnodes rewriting the tagname only. |
300 |
FIXME: The redundant encoding for each XUpdate |
301 |
element type seems dummy. Could this be stripped |
302 |
shorter somehow sometimes? |
303 |
--> |
304 |
|
305 |
<xsl:template match="xupdate:element"> |
306 |
<xsl:comment> 3.a. vivify generic node </xsl:comment> |
307 |
<xsl:element name="xsl:element"> |
308 |
<xsl:copy-of select="@*"/> |
309 |
<xsl:apply-templates /> |
310 |
</xsl:element> |
311 |
</xsl:template> |
312 |
|
313 |
<xsl:template match="xupdate:attribute"> |
314 |
<xsl:comment> 3.b. vivify node attributes </xsl:comment> |
315 |
<xsl:element name="xsl:attribute"> |
316 |
<xsl:copy-of select="@*"/> |
317 |
<xsl:apply-templates /> |
318 |
</xsl:element> |
319 |
</xsl:template> |
320 |
|
321 |
<xsl:template match="xupdate:text"> |
322 |
<xsl:comment> 4.a. vivify text node </xsl:comment> |
323 |
<xsl:element name="xsl:text"> |
324 |
<xsl:copy-of select="@*"/> |
325 |
<xsl:apply-templates /> |
326 |
</xsl:element> |
327 |
</xsl:template> |
328 |
|
329 |
<xsl:template match="xupdate:processing-instruction"> |
330 |
<xsl:comment> 4.b. vivify PI node </xsl:comment> |
331 |
<xsl:element name="xsl:processing-instruction"> |
332 |
<xsl:copy-of select="@*"/> |
333 |
<xsl:apply-templates /> |
334 |
</xsl:element> |
335 |
</xsl:template> |
336 |
|
337 |
<xsl:template match="xupdate:comment"> |
338 |
<xsl:comment> 4.c. vivify comment node </xsl:comment> |
339 |
<xsl:element name="xsl:comment"> |
340 |
<xsl:copy-of select="@*"/> |
341 |
<xsl:apply-templates /> |
342 |
</xsl:element> |
343 |
</xsl:template> |
344 |
|
345 |
<!-- xsl for [2.9 Variables and Values of Variables] --> |
346 |
<xsl:template match="xupdate:variable"> |
347 |
<xsl:comment> 5.a. vivify xsl-element "variable" </xsl:comment> |
348 |
<xsl:element name="xsl:variable"> |
349 |
<xsl:copy-of select="@*"/> |
350 |
<!-- |
351 |
<xsl:apply-templates /> |
352 |
--> |
353 |
<!-- <xsl:element name="xsl:apply-templates" > --> |
354 |
<xsl:attribute name="select"> |
355 |
<!-- <xsl:apply-templates /> --> |
356 |
/addresses[2]/address[4]/town |
357 |
</xsl:attribute> |
358 |
<!-- </xsl:element> --> |
359 |
hui |
360 |
</xsl:element> |
361 |
|
362 |
</xsl:template> |
363 |
|
364 |
<xsl:template match="xupdate:value-of"> |
365 |
<xsl:comment> 5.b. vivify xsl-element "value-of" </xsl:comment> |
366 |
<xsl:element name="xsl:value-of"> |
367 |
<xsl:copy-of select="@*"/> |
368 |
<xsl:apply-templates /> |
369 |
</xsl:element> |
370 |
<xsl:text>abs</xsl:text> |
371 |
</xsl:template> |
372 |
|
373 |
|
374 |
</xsl:stylesheet> |
375 |
|