/[cvs]/joko/Scripts/xupdate/xupdate.pl
ViewVC logotype

Annotation of /joko/Scripts/xupdate/xupdate.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Sat Apr 26 01:42:39 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.2: +53 -16 lines
File MIME type: text/plain
first attempt to make up a crud template

1 joko 1.1 #!/usr/bin/perl
2    
3     ## -------------------------------------------------------------------------
4 joko 1.3 ## $Id: xupdate.pl,v 1.2 2003/04/25 12:35:23 joko Exp $
5 joko 1.1 ## -------------------------------------------------------------------------
6 joko 1.2 ## $Log: xupdate.pl,v $
7 joko 1.3 ## Revision 1.2 2003/04/25 12:35:23 joko
8     ## added some pod
9     ## revamped xsl
10     ##
11 joko 1.2 ## Revision 1.1 2003/04/24 21:07:36 joko
12     ## initial commit
13     ##
14 joko 1.1 ## -------------------------------------------------------------------------
15    
16    
17     =pod
18    
19    
20     =head3 Overview
21    
22     This is not the same xupdate currently available from CPAN at
23     http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/xupdate .
24    
25     Its intention - however - is identical:
26     xupdate - Process XUpdate commands over an XML document
27    
28     =head3 Their implementations differ:
29     xupdate (by Petr Pajas) uses ...
30     XML::XUpdate::LibXML - Simple implementation of XUpdate format
31    
32     ... which is based on XML::LibXML which in turn is:
33     This module is an interface to the gnome libxml2 DOM parser (no SAX parser support yet),
34     and the DOM tree. It also provides an XML::XPath-like findnodes() interface, providing
35     access to the XPath API in libxml2.
36    
37    
38 joko 1.2 =head3 Yet another xupdate - facts in short:
39 joko 1.1
40 joko 1.2 S: It would be nice to have a pure perl thingy which does (almost) the same stuff....
41 joko 1.1
42 joko 1.2 Q: Can we achieve compliance with its (XML::XUpdate::LibXML) API? (just a subset ....)
43 joko 1.1
44     Q: Can we achieve the processing declared as a XSL document processed using CPAN's XML::XSLT?
45    
46     Q: Proposal: XML::XUpdate::XSLT!?
47    
48     Q: Can we mimic/use the interface of the - already established - 'xupdate' program???
49    
50 joko 1.2 Q: Should we follow the CRUD path first?
51    
52 joko 1.1
53     =cut
54    
55    
56     package main;
57    
58     use strict;
59     use warnings;
60    
61     BEGIN {
62     use lib '../../../nfo/perl/libs';
63     }
64    
65     use Data::Dumper;
66     use XML::XSLT;
67     #use XML::DOM;
68     #use XML::DOM::Document;
69     use shortcuts::files qw( f2s );
70    
71     my $file = 'c:/home/amo/develop/top-scores.net/ts/backend/var/resources/control/import_select.xml';
72    
73     my $stylesheet = '<?xml version="1.0" encoding="ISO-8859-1"?>
74    
75     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
76    
77     <!--
78    
79     Purpose of this XML Stylesheet is to implement a set of templates
80     to do simple xml node manipulation. (kinda XML API)
81     Main target is "flexible insertion of new xml child nodes".
82    
83     If this attempt works out well, it will be continued by a follow-up
84     trying to implement XUpdate in XSLT.
85    
86    
87     References:
88     - XUpdate:
89     Requirements: http://www.xmldb.org/xupdate/xupdate-req.html
90     Working Draft: http://www.xmldb.org/xupdate/xupdate-wd.html
91     - XML API:
92     - XML::XUpdate::LibXML: http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/lib/XML/XUpdate/LibXML.pm
93     - XSL / XSLT:
94     http://www.w3.org/TR/xslt
95     http://www.xsl-rp.de/
96     http://xml.klute-thiemann.de/w3c-de/REC-xslt-20020318/
97     http://xmlxslt.sourceforge.net/
98     - misc pointers:
99     "Re: Modify XML documents": http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/1265431
100     XSL Extensions: http://xmlsoft.org/XSLT/extensions.html
101     EXSLT: http://www.exslt.org/set/functions/difference/index.html
102     "how to insert element at required position in document tree?": http://p2p.wrox.com/archive/xslt/2001-06/98.asp
103     XML APIs for Databases: http://www.javaworld.com/javaworld/jw-01-2000/jw-01-dbxml.html
104    
105     -->
106    
107     <xsl:output method="raw"/>
108    
109 joko 1.3 <!-- 1. This is the passthru logic (copy all untouched nodes). -->
110     <xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template>
111     <!-- activate this -->
112 joko 1.2 <xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template>
113 joko 1.3 <!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule -->
114     <!-- <xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> -->
115 joko 1.1
116 joko 1.2 <!-- 2. This is the crud API. -->
117    
118     <!-- 2.a. The context finder. -->
119 joko 1.1
120 joko 1.3 <!-- query part one: the node name -->
121     <xsl:template match="source">
122 joko 1.2 <xsl:choose>
123 joko 1.3 <!-- query part two: the attrib key and value -->
124 joko 1.2 <xsl:when test="@key=expekt">
125 joko 1.3 <xsl:call-template name="crud" >
126     <xsl:with-param name="action">5</xsl:with-param>
127     <xsl:with-param name="payload">------- payload -------</xsl:with-param>
128     </xsl:call-template>
129 joko 1.2 </xsl:when>
130     <xsl:otherwise>
131     <xsl:call-template name="passthru_kay" />
132     </xsl:otherwise>
133     </xsl:choose>
134     </xsl:template>
135    
136    
137     <!-- 2.b. The node manipulators: They translate the current context. -->
138    
139     <!-- by Mike Kay, Software AG [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] -->
140     <!-- enhancements & comments: Andreas Motl, rotamente.de -->
141     <!-- <xsl:template match="source"> -->
142     <xsl:template name="passthru_kay">
143     <xsl:copy>
144     <xsl:copy-of select="@*" />
145     <xsl:apply-templates />
146     </xsl:copy>
147     </xsl:template>
148    
149     <xsl:template name="inject_kay_motl">
150 joko 1.3 <xsl:param name="payload" />
151     <xsl:value-of select="$payload" />
152 joko 1.2 <xsl:copy>
153     <xsl:copy-of select="@*" />
154     <xsl:apply-templates />
155     <!-- <xsl:apply-templates select="*[3]" /> -->
156     <!-- <xsl:value-of select="text()" /> -->
157     <!-- Multiple handlers here now: a) create fressshhhh element, b) clone s.th. -->
158     <!-- TODO: Do switch/case or couple of if/then statements here to be able to handle that "at runtime". -->
159     <xsl:call-template name="create_fresh_element" />
160     <xsl:call-template name="clone_last_element" />
161     <xsl:call-template name="clone_element_nr" />
162     </xsl:copy>
163 joko 1.1 </xsl:template>
164    
165 joko 1.3 <xsl:template name="crud">
166     <xsl:param name="action" />
167     <xsl:param name="payload" />
168     action: <xsl:value-of select="$action" />
169     payload: <xsl:value-of select="$payload" />
170    
171     <xsl:if test="$action=5">
172     CRUD: CREATE
173     </xsl:if>
174    
175     <xsl:copy>
176     <xsl:copy-of select="@*" />
177     <xsl:apply-templates />
178    
179     <!-- crud action dispatcher -->
180     <xsl:choose>
181     <xsl:when test="{$action}=5">
182     <!-- <xsl:when test="blah=\'blub\'"> -->
183     CRUD: CREATE
184     <xsl:call-template name="clone_last_element" />
185     </xsl:when>
186     <xsl:otherwise>
187     <xsl:comment> UNKOWN CRUD ACTION! </xsl:comment>
188     </xsl:otherwise>
189     </xsl:choose>
190    
191     </xsl:copy>
192    
193     </xsl:template>
194    
195 joko 1.1
196    
197 joko 1.2 <!-- 2.c. The element manipulators: They mungle a specific element inside the current context. -->
198 joko 1.1
199     <!-- copy over node 1:1 (use last element as a template) -->
200     <xsl:template name="clone_last_element">
201 joko 1.3 <xsl:copy-of select="*[last()]" />
202 joko 1.1 </xsl:template>
203    
204     <xsl:template name="clone_element_nr">
205 joko 1.3 <!-- TODO: let idx be passed in via "with-param" or s.th.l.th. -->
206     <xsl:copy-of select="*[2]" />
207 joko 1.1 </xsl:template>
208    
209     <!-- creates instance of new element -->
210     <xsl:template name="create_fresh_element">
211 joko 1.3 <xsl:element name="new">content</xsl:element>
212 joko 1.1 </xsl:template>
213    
214    
215     <!-- inject other stuff -->
216     <xsl:template name="inject_template">
217     <xsl:comment> Hello World! </xsl:comment>
218    
219     </xsl:template>
220    
221    
222    
223 joko 1.2
224     <!-- 0. The root node dispatcher. -->
225     <!-- <xsl:template match="source"><xsl:call-template name="insertChildNode"/></xsl:template> -->
226    
227     <!-- 2. This is the manipulation logic: insertChildNode -->
228    
229     <!-- 2.a. The "API" method. - now deprecated -->
230     <xsl:template name="insertChildNode">
231    
232     <!-- manipulate node -->
233     <xsl:comment> node - begin </xsl:comment>
234    
235     <!-- <xsl:copy-of select="source" /> -->
236 joko 1.1 <xsl:copy>
237 joko 1.2
238     <!-- Pass through children 1:1. -->
239 joko 1.1 <xsl:apply-templates />
240 joko 1.2
241     <!-- Call one of the manipulators - this implements injection behavior. -->
242 joko 1.1 <xsl:call-template name="clone_last_element" />
243 joko 1.2 <xsl:call-template name="inject_template" />
244    
245     </xsl:copy>
246     <xsl:comment> node - end </xsl:comment>
247    
248 joko 1.1 </xsl:template>
249    
250    
251     </xsl:stylesheet>
252    
253     ';
254    
255     sub main {
256    
257     my $xslt = XML::XSLT->new(
258     Source => $stylesheet,
259     #debug => 1,
260     warnings => 1
261     );
262    
263     #print f2s($file), "\n";
264    
265     $xslt->open_xml( Source => f2s($file) );
266    
267     $xslt->process();
268     print $xslt->toString(), "\n";
269    
270     }
271    
272     main();
273     print "ready.", "\n";
274    
275    
276     1;
277     __END__

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