/[cvs]/nfo/perl/libs/XML/XUpdate/xupdate2xsl.xml
ViewVC logotype

Diff of /nfo/perl/libs/XML/XUpdate/xupdate2xsl.xml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Thu May 1 20:39:54 2003 UTC revision 1.7 by joko, Fri Jun 6 04:02:34 2003 UTC
# Line 2  Line 2 
2        
3    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4        
5        <!--        <!--
6        Purpose of this XML Stylesheet is to implement a set of templates        
7        to translate XUpdate lingo into an intermediate xslt stylesheet          xupdate2xsl: Translate XML document from namespace 'xupdate' to 'xsl'.
8        which actually performs the update to the original xml document          
9        in a second step.          Purpose of this XML Stylesheet is to implement a set of templates
10        The required glue code - written in Perl - is available in module          to translate XUpdate lingo into an intermediate xslt stylesheet
11        XML::XUpdate::XSLT. Please have a look at this to port it to other          which actually performs the update to the original xml document
12        languages.          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" />        <xsl:output method="xml" />
# Line 21  Line 35 
35        <!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule -->        <!-- 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>        <xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template>
37        
38          
39        <!-- 2. This is the translation part: XUpdate becomes XSLT -->        <!-- 2. This is the translation part: XUpdate becomes XSLT -->
40                
41        <!-- This node "encapsulates" common infrastructure. -->        <!-- lib -->
42          <!-- This node "encapsulates" common core infrastructure. -->
43        <xsl:template match="xupdate:modifications">        <xsl:template match="xupdate:modifications">
44        
45            <!-- 1. This is the passthru logic (copy all untouched nodes). -->          <!-- <xsl:template name="identity_template_rule"> -->
46            <!-- in fact this is the xsl from above translated to be able to be generated by xsl itself! -->          <xsl:element name="xsl:template">
47                <xsl:comment> 1. passthru logic </xsl:comment>            <xsl:attribute name="name">identity_template_rule</xsl:attribute>
48                <xsl:element name="xsl:template">  
49                    <xsl:attribute name="name">passthru</xsl:attribute>                <!-- An "identity template rule" - original version by Mike Kay, Software AG -->
50                    <xsl:element name="xsl:copy"><xsl:element name="xsl:apply-templates" /></xsl:element>                <!-- Please visit [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] -->
51                </xsl:element>                <!-- Translated to "self hosting xsl": Andreas Motl, andreas.motl@ilo.de -->
52                <xsl:element name="xsl:template">                <!-- FIXME: Could this be used to replace passthru-logic at 1. ? -->
53                    <xsl:attribute name="match">*</xsl:attribute>                <xsl:element name="xsl:copy">
54                    <xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element>                  <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>                </xsl:element>
59                <xsl:element name="xsl:template">  
60                    <xsl:attribute name="match">comment()</xsl:attribute>          </xsl:element>
61                    <xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element>          <!-- </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>                </xsl:element>
76        
77            <!-- continue with all inline nodes -->            <!-- continue with all inline nodes -->
# Line 47  Line 79 
79        
80        </xsl:template>        </xsl:template>
81        
82    
83    
84    
85    
86          <!-- code -->
87        <!-- This node "encapsulates" infrastructure for handling the directives. -->        <!-- 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    
92          <!--
93            xsl for XUpdate Services [2 XUpdate Syntax] [2.3 Modifications]
94          -->
95    
96          <!-- xsl for [2.4 Inserts] -->
97        <xsl:template match="xupdate:insert-after">        <xsl:template match="xupdate:insert-after">
98            <xsl:comment> 2. context finder </xsl:comment>            <!-- <xsl:variable name="select" select="@select">Hello World!</xsl:variable> -->
99            <xsl:apply-templates />            <xsl:call-template name="findcontext">
100                <xsl:with-param name="service">Insert</xsl:with-param>
101                <xsl:with-param name="position">after</xsl:with-param>
102              </xsl:call-template>
103          </xsl:template>
104    
105          <!-- xsl for [2.4 Inserts] -->
106          <xsl:template match="xupdate:insert-before">
107              <xsl:call-template name="findcontext">
108                <xsl:with-param name="service">Insert</xsl:with-param>
109                <xsl:with-param name="position">before</xsl:with-param>
110              </xsl:call-template>
111          </xsl:template>
112    
113          <!-- xsl for [2.5 Append] -->
114          <xsl:template match="xupdate:append">
115              <xsl:call-template name="findcontext">
116                <xsl:with-param name="service">Append</xsl:with-param>
117                <!-- <xsl:with-param name="position">inline</xsl:with-param> -->
118                <xsl:with-param name="test">.</xsl:with-param>
119              </xsl:call-template>
120          </xsl:template>
121    
122          <!-- xsl for [2.6 Update] -->
123          <xsl:template match="xupdate:update">
124              <xsl:call-template name="findcontext">
125                <xsl:with-param name="service">Update</xsl:with-param>
126                <!-- <xsl:with-param name="position">after</xsl:with-param> -->
127                <xsl:with-param name="test">.</xsl:with-param>
128              </xsl:call-template>
129          </xsl:template>
130    
131          <!-- xsl for [2.7 Remove] -->
132          <xsl:template match="xupdate:remove">
133              <xsl:call-template name="findcontext">
134                <xsl:with-param name="service">Remove</xsl:with-param>
135                <!-- <xsl:with-param name="position">after</xsl:with-param> -->
136                <!-- 2003-06-04: removed -->
137                <!-- <xsl:with-param name="test">.</xsl:with-param> -->
138              </xsl:call-template>
139          </xsl:template>
140    
141          <!-- xsl for [2.8 Rename] -->
142          <xsl:template match="xupdate:rename">
143              <xsl:call-template name="findcontext">
144                <xsl:with-param name="service">Rename</xsl:with-param>
145                <!-- <xsl:with-param name="position">after</xsl:with-param> -->
146                <xsl:with-param name="test">.</xsl:with-param>
147              </xsl:call-template>
148          </xsl:template>
149    
150    
151    
152          <xsl:template name="findcontext">
153    
154            <xsl:comment> 2. context finder </xsl:comment>
155    
156            <!-- new of 2003-05-06: introducing parameters here to handle all cases -->
157            <xsl:param name="service" />
158            <xsl:param name="position" />
159            <!-- new of 2003-05-07: replacement for test if undef -->
160            <xsl:param name="test" />
161            
162            <!--
163            <xsl:comment>
164              service: <xsl:value-of select="$service" />
165              position: <xsl:value-of select="$position" />
166              test: <xsl:value-of select="$test" />
167            </xsl:comment>
168            -->
169    
170              <!-- this resembles some parts of the CRUD API - retranslated to self-hosting xsl -->
171    
172              <!-- V1 -->
173              <!-- <xsl:element name="xsl:template"> -->
174                  <!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> -->
175              
176              <!-- fallback -->
177              <xsl:element name="xsl:template">
178                  <xsl:attribute name="match">*</xsl:attribute>
179                  <xsl:element name="xsl:call-template">
180                    <xsl:attribute name="name">identity_template_rule</xsl:attribute>
181                    <!-- <xsl:attribute name="name">passthru</xsl:attribute> -->
182                  </xsl:element>
183              </xsl:element>
184    
185              <!-- V2 -->
186              <xsl:element name="xsl:template">
187                  <!-- <xsl:attribute name="match">/addresses/address</xsl:attribute> -->
188                  <!-- <xsl:attribute name="match">*</xsl:attribute> -->
189                  <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute>
190    
191              <xsl:element name="xsl:choose">
192                  <!-- <xsl:attribute name="match"><xsl:value-of select="@select" /></xsl:attribute> -->
193                <xsl:element name="xsl:when">
194                  <!-- <xsl:attribute name="test"><xsl:value-of select="@select" /></xsl:attribute> -->
195                  <xsl:attribute name="test"><xsl:value-of select="@test" /><xsl:value-of select="$test" /></xsl:attribute>
196    
197                  <xsl:choose>
198                    <xsl:when test="{$service}=Insert">
199    
200                    <xsl:comment> 2.a. Insert </xsl:comment>
201    
202                    <!-- Implementation of "xupdate:insert-before" -->
203                    <xsl:choose>
204                      <xsl:when test="{$position}=before">
205                        <xsl:apply-templates />
206                      </xsl:when>
207                    </xsl:choose>
208      
209                    <!-- call the "identity template rule" to passthru all childnodes -->
210                    <xsl:element name="xsl:call-template">
211                      <xsl:attribute name="name">identity_template_rule</xsl:attribute>
212                      <!-- <xsl:attribute name="name">passthru</xsl:attribute> -->
213                    </xsl:element>
214      
215                    <!-- Implementation of "xupdate:insert-after" -->
216                    <xsl:choose>
217                      <xsl:when test="{$position}=after">
218                        <xsl:apply-templates />
219                      </xsl:when>
220                    </xsl:choose>
221    
222                    </xsl:when>
223                    
224                    <!-- Implementation of "xupdate:append" -->
225                    <xsl:when test="{$service}=Append">
226                    <xsl:comment> 2.b. Append </xsl:comment>
227                      <!-- V1 -->
228                      <!-- <xsl:apply-templates /> -->
229                      <!-- V2 -->
230                      <xsl:element name="xsl:copy">
231                        <!--
232                        <xsl:element name="xsl:copy-of">
233                          <xsl:attribute name="select">@*</xsl:attribute>
234                        </xsl:element>
235                        -->
236                        <!-- Keep content ... -->
237                        <xsl:element name="xsl:apply-templates" />
238                        <!-- Append this: -->
239                        <xsl:apply-templates />
240                      </xsl:element>
241                    </xsl:when>
242                    
243                    <!-- Implementation of "xupdate:update" -->
244                    <xsl:when test="{$service}=Update">
245                    <xsl:comment> 2.c. Update </xsl:comment>
246                      <!-- V1 -->
247                      <!-- <xsl:apply-templates /> -->
248                      <!-- V2 -->
249                      <xsl:element name="xsl:copy">
250                        <!--
251                        <xsl:element name="xsl:copy-of">
252                          <xsl:attribute name="select">@*</xsl:attribute>
253                        </xsl:element>
254                        -->
255                        <!-- Keep content ... -->
256                        <!-- <xsl:element name="xsl:apply-templates" /> -->
257                        <!-- Add (replace through) this: -->
258                        <xsl:apply-templates />
259                      </xsl:element>
260                    </xsl:when>
261    
262                    <!-- Implementation of "xupdate:remove" -->
263                    <xsl:when test="{$service}=Remove">
264                      <xsl:comment> 2.d. Remove </xsl:comment>
265                    </xsl:when>
266    
267                    <!-- Implementation of "xupdate:rename" -->
268                    <xsl:when test="{$service}=Rename">
269                      <xsl:comment> 2.e. Rename (Remove context and Append modified identity template) </xsl:comment>
270                      
271                      <xsl:comment> [5.a.] modify node name </xsl:comment>
272                      <xsl:element name="xsl:element">
273                        <!-- this renames the node (change the "name"-attribute) -->
274                        <xsl:attribute name="name">
275                          <xsl:apply-templates />
276                        </xsl:attribute>
277                        <!-- this propagates the original content -->
278                        <xsl:element name="xsl:apply-templates" />
279                      </xsl:element>
280    
281                    </xsl:when>
282    
283                  </xsl:choose>
284    
285                </xsl:element>
286                <xsl:element name="xsl:otherwise">
287    
288                  <!-- call the "identity template rule" to passthru all childnodes -->
289                  <xsl:element name="xsl:call-template">
290                    <xsl:attribute name="name">identity_template_rule</xsl:attribute>
291                    <!-- <xsl:attribute name="name">passthru</xsl:attribute> -->
292                  </xsl:element>
293    
294                  <!-- <xsl:apply-templates /> -->
295                </xsl:element>
296    
297              </xsl:element>
298              </xsl:element>
299    
300              <!-- <xsl:apply-templates /> -->
301              
302        </xsl:template>        </xsl:template>
303        
304        <!-- This node passes through all attributes and childnodes rewriting the tagname only. -->    
305          <!--
306            Rewrite XUpdate elements to XSLT elements.
307            These templates pass through all attributes
308            and childnodes rewriting the tagname only.
309            FIXME: The redundant encoding for each XUpdate
310            element type seems dummy. Could this be stripped
311            shorter somehow sometimes?
312          -->
313    
314          <!--
315            xsl for [2.4 Inserts]
316          -->
317    
318          <!-- xsl for [2.4.1 Creating Elements] -->
319        <xsl:template match="xupdate:element">        <xsl:template match="xupdate:element">
320            <xsl:comment> 3. rewrite / vivify elements/attributes </xsl:comment>            <xsl:comment> 3.a. vivify generic node </xsl:comment>
321            <xsl:element name="xsl:element">            <xsl:element name="xsl:element">
322                <xsl:copy-of select="@*"/>                <xsl:copy-of select="@*"/>
323                <xsl:apply-templates />                <xsl:apply-templates />
324            </xsl:element>            </xsl:element>
325        </xsl:template>        </xsl:template>
326        
327        <!-- This node passes through all attributes and childnodes rewriting the tagname only. -->        <!-- xsl for [2.4.2 Creating Attributes] -->
328        <xsl:template match="xupdate:attribute">        <xsl:template match="xupdate:attribute">
329              <xsl:comment> 3.b. vivify node attributes </xsl:comment>
330            <xsl:element name="xsl:attribute">            <xsl:element name="xsl:attribute">
331                <xsl:copy-of select="@*"/>                <xsl:copy-of select="@*"/>
332                <xsl:apply-templates />                <xsl:apply-templates />
333            </xsl:element>            </xsl:element>
334        </xsl:template>        </xsl:template>
335        
336    </xsl:stylesheet>        <!-- xsl for [2.4.3 Creating Text] -->
337          <xsl:template match="xupdate:text">
338              <xsl:comment> 4.a. vivify text node </xsl:comment>
339              <xsl:element name="xsl:text">
340                  <xsl:copy-of select="@*"/>
341                  <xsl:apply-templates />
342              </xsl:element>
343          </xsl:template>
344    
345          <!-- xsl for [2.4.4 Creating Processing Instructions] -->
346          <xsl:template match="xupdate:processing-instruction">
347              <xsl:comment> 4.b. vivify PI node </xsl:comment>
348              <xsl:element name="xsl:processing-instruction">
349                  <xsl:copy-of select="@*"/>
350                  <xsl:apply-templates />
351              </xsl:element>
352          </xsl:template>
353    
354          <!-- xsl for [2.4.5 Creating Comments] -->
355          <xsl:template match="xupdate:comment">
356              <xsl:comment> 4.c. vivify comment node </xsl:comment>
357              <xsl:element name="xsl:comment">
358                  <xsl:copy-of select="@*"/>
359                  <xsl:apply-templates />
360              </xsl:element>
361          </xsl:template>
362    
363    
364          <!--
365            xsl for [2.9 Variables and Values of Variables]
366            Encapsulates block payload into a named template.
367          -->
368          <xsl:template match="xupdate:variable">
369              <xsl:comment> 5.a. vivify xsl-element "variable" </xsl:comment>
370              
371              <xsl:element name="xsl:template">
372                  <xsl:attribute name="name">Variables</xsl:attribute>
373              
374                  <!--
375                  <xsl:element name="xsl:variable">
376                      <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
377                  -->
378                  
379                  <xsl:choose>
380                  
381                    <!-- Mode 1 [spec]: just evaluate as (sub-)selection if "select"-attribute is not empty -->
382                    <xsl:when test="@select!=">
383                      <xsl:element name="xsl:apply-templates">
384                          <xsl:attribute name="select"><xsl:value-of select="@select" /></xsl:attribute>
385                      </xsl:element>
386                    </xsl:when>
387                    
388                    <!-- Mode 2 [feature]: in other cases: pass thru the current node w/o any operation(s) on it -->
389                    <xsl:otherwise>
390                        <xsl:value-of select="." />
391                    </xsl:otherwise>
392                        
393                  </xsl:choose>
394    
395                  <!--
396                  </xsl:element>
397                  -->
398    
399              </xsl:element>
400    
401          </xsl:template>
402    
403          <!--
404            xsl for [2.9 Variables and Values of Variables]
405            Calls encapsulated named template "Variables"
406            for each occourance of an "xupdate:value-of" element.
407          -->
408          <xsl:template match="xupdate:value-of">
409              <xsl:comment> 5.b. vivify xsl-element "value-of" </xsl:comment>
410              <xsl:element name="xsl:call-template">
411                <xsl:attribute name="name">Variables</xsl:attribute>
412              </xsl:element>
413              
414            <!-- is this required any more??? -->
415            <xsl:element name="xsl:value-of">
416                  <xsl:copy-of select="@*"/>
417                  <!-- <xsl:apply-templates /> -->
418                  <!-- this propagates the original content -->
419                  <!-- <xsl:element name="xsl:apply-templates"> -->
420                    <!-- <xsl:attribute name="" > -->
421                  <!-- </xsl:element> -->
422              </xsl:element>
423          </xsl:template>
424    
425    
426    </xsl:stylesheet>
427        

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed