1 |
joko |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
## ------------------------------------------------------------------------- |
4 |
joko |
1.4 |
## $Id: xupdate.pl,v 1.3 2003/04/26 01:42:39 joko Exp $ |
5 |
joko |
1.1 |
## ------------------------------------------------------------------------- |
6 |
joko |
1.2 |
## $Log: xupdate.pl,v $ |
7 |
joko |
1.4 |
## Revision 1.3 2003/04/26 01:42:39 joko |
8 |
|
|
## first attempt to make up a crud template |
9 |
|
|
## |
10 |
joko |
1.3 |
## Revision 1.2 2003/04/25 12:35:23 joko |
11 |
|
|
## added some pod |
12 |
|
|
## revamped xsl |
13 |
|
|
## |
14 |
joko |
1.2 |
## Revision 1.1 2003/04/24 21:07:36 joko |
15 |
|
|
## initial commit |
16 |
|
|
## |
17 |
joko |
1.1 |
## ------------------------------------------------------------------------- |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
=pod |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
=head3 Overview |
24 |
|
|
|
25 |
|
|
This is not the same xupdate currently available from CPAN at |
26 |
|
|
http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/xupdate . |
27 |
|
|
|
28 |
|
|
Its intention - however - is identical: |
29 |
|
|
xupdate - Process XUpdate commands over an XML document |
30 |
|
|
|
31 |
|
|
=head3 Their implementations differ: |
32 |
|
|
xupdate (by Petr Pajas) uses ... |
33 |
|
|
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
34 |
|
|
|
35 |
|
|
... which is based on XML::LibXML which in turn is: |
36 |
|
|
This module is an interface to the gnome libxml2 DOM parser (no SAX parser support yet), |
37 |
|
|
and the DOM tree. It also provides an XML::XPath-like findnodes() interface, providing |
38 |
|
|
access to the XPath API in libxml2. |
39 |
|
|
|
40 |
|
|
|
41 |
joko |
1.2 |
=head3 Yet another xupdate - facts in short: |
42 |
joko |
1.1 |
|
43 |
joko |
1.2 |
S: It would be nice to have a pure perl thingy which does (almost) the same stuff.... |
44 |
joko |
1.1 |
|
45 |
joko |
1.2 |
Q: Can we achieve compliance with its (XML::XUpdate::LibXML) API? (just a subset ....) |
46 |
joko |
1.1 |
|
47 |
|
|
Q: Can we achieve the processing declared as a XSL document processed using CPAN's XML::XSLT? |
48 |
|
|
|
49 |
|
|
Q: Proposal: XML::XUpdate::XSLT!? |
50 |
|
|
|
51 |
|
|
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
52 |
|
|
|
53 |
joko |
1.2 |
Q: Should we follow the CRUD path first? |
54 |
|
|
|
55 |
joko |
1.1 |
|
56 |
|
|
=cut |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
package main; |
60 |
|
|
|
61 |
|
|
use strict; |
62 |
|
|
use warnings; |
63 |
|
|
|
64 |
|
|
BEGIN { |
65 |
|
|
use lib '../../../nfo/perl/libs'; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
use Data::Dumper; |
69 |
|
|
use XML::XSLT; |
70 |
|
|
#use XML::DOM; |
71 |
|
|
#use XML::DOM::Document; |
72 |
|
|
use shortcuts::files qw( f2s ); |
73 |
|
|
|
74 |
|
|
my $file = 'c:/home/amo/develop/top-scores.net/ts/backend/var/resources/control/import_select.xml'; |
75 |
|
|
|
76 |
joko |
1.4 |
|
77 |
|
|
#my $stylesheet_xupdate = ' |
78 |
|
|
|
79 |
|
|
my $stylesheet_xupdate = '<?xml version="1.0" encoding="ISO-8859-1"?> |
80 |
joko |
1.1 |
|
81 |
|
|
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
82 |
|
|
|
83 |
|
|
<!-- |
84 |
|
|
|
85 |
|
|
Purpose of this XML Stylesheet is to implement a set of templates |
86 |
|
|
to do simple xml node manipulation. (kinda XML API) |
87 |
|
|
Main target is "flexible insertion of new xml child nodes". |
88 |
|
|
|
89 |
|
|
If this attempt works out well, it will be continued by a follow-up |
90 |
|
|
trying to implement XUpdate in XSLT. |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
References: |
94 |
|
|
- XUpdate: |
95 |
|
|
Requirements: http://www.xmldb.org/xupdate/xupdate-req.html |
96 |
|
|
Working Draft: http://www.xmldb.org/xupdate/xupdate-wd.html |
97 |
|
|
- XML API: |
98 |
|
|
- XML::XUpdate::LibXML: http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/lib/XML/XUpdate/LibXML.pm |
99 |
|
|
- XSL / XSLT: |
100 |
|
|
http://www.w3.org/TR/xslt |
101 |
|
|
http://www.xsl-rp.de/ |
102 |
|
|
http://xml.klute-thiemann.de/w3c-de/REC-xslt-20020318/ |
103 |
|
|
http://xmlxslt.sourceforge.net/ |
104 |
|
|
- misc pointers: |
105 |
|
|
"Re: Modify XML documents": http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/1265431 |
106 |
|
|
XSL Extensions: http://xmlsoft.org/XSLT/extensions.html |
107 |
|
|
EXSLT: http://www.exslt.org/set/functions/difference/index.html |
108 |
|
|
"how to insert element at required position in document tree?": http://p2p.wrox.com/archive/xslt/2001-06/98.asp |
109 |
|
|
XML APIs for Databases: http://www.javaworld.com/javaworld/jw-01-2000/jw-01-dbxml.html |
110 |
|
|
|
111 |
|
|
--> |
112 |
|
|
|
113 |
|
|
<xsl:output method="raw"/> |
114 |
|
|
|
115 |
joko |
1.3 |
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
116 |
|
|
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
117 |
|
|
<!-- activate this --> |
118 |
joko |
1.2 |
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
119 |
joko |
1.3 |
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
120 |
joko |
1.4 |
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
121 |
joko |
1.1 |
|
122 |
joko |
1.2 |
<!-- 2. This is the crud API. --> |
123 |
|
|
|
124 |
|
|
<!-- 2.a. The context finder. --> |
125 |
joko |
1.1 |
|
126 |
joko |
1.3 |
<!-- query part one: the node name --> |
127 |
|
|
<xsl:template match="source"> |
128 |
joko |
1.2 |
<xsl:choose> |
129 |
joko |
1.3 |
<!-- query part two: the attrib key and value --> |
130 |
joko |
1.2 |
<xsl:when test="@key=expekt"> |
131 |
joko |
1.3 |
<xsl:call-template name="crud" > |
132 |
joko |
1.4 |
<xsl:with-param name="action">CREATE</xsl:with-param> |
133 |
|
|
<xsl:with-param name="mode">callback</xsl:with-param> |
134 |
|
|
<xsl:with-param name="cbname">abc</xsl:with-param> |
135 |
joko |
1.3 |
</xsl:call-template> |
136 |
joko |
1.2 |
</xsl:when> |
137 |
|
|
<xsl:otherwise> |
138 |
|
|
<xsl:call-template name="passthru_kay" /> |
139 |
|
|
</xsl:otherwise> |
140 |
|
|
</xsl:choose> |
141 |
|
|
</xsl:template> |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
<!-- 2.b. The node manipulators: They translate the current context. --> |
145 |
|
|
|
146 |
|
|
<!-- by Mike Kay, Software AG [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] --> |
147 |
|
|
<!-- enhancements & comments: Andreas Motl, rotamente.de --> |
148 |
|
|
<!-- <xsl:template match="source"> --> |
149 |
|
|
<xsl:template name="passthru_kay"> |
150 |
|
|
<xsl:copy> |
151 |
|
|
<xsl:copy-of select="@*" /> |
152 |
|
|
<xsl:apply-templates /> |
153 |
|
|
</xsl:copy> |
154 |
|
|
</xsl:template> |
155 |
|
|
|
156 |
|
|
<xsl:template name="inject_kay_motl"> |
157 |
joko |
1.3 |
<xsl:param name="payload" /> |
158 |
|
|
<xsl:value-of select="$payload" /> |
159 |
joko |
1.2 |
<xsl:copy> |
160 |
|
|
<xsl:copy-of select="@*" /> |
161 |
|
|
<xsl:apply-templates /> |
162 |
|
|
<!-- <xsl:apply-templates select="*[3]" /> --> |
163 |
|
|
<!-- <xsl:value-of select="text()" /> --> |
164 |
|
|
<!-- Multiple handlers here now: a) create fressshhhh element, b) clone s.th. --> |
165 |
|
|
<!-- TODO: Do switch/case or couple of if/then statements here to be able to handle that "at runtime". --> |
166 |
|
|
<xsl:call-template name="create_fresh_element" /> |
167 |
|
|
<xsl:call-template name="clone_last_element" /> |
168 |
|
|
<xsl:call-template name="clone_element_nr" /> |
169 |
|
|
</xsl:copy> |
170 |
joko |
1.1 |
</xsl:template> |
171 |
|
|
|
172 |
joko |
1.3 |
<xsl:template name="crud"> |
173 |
|
|
|
174 |
joko |
1.4 |
<xsl:param name="action" /> |
175 |
|
|
<xsl:param name="payload" /> |
176 |
|
|
<xsl:param name="mode" /> |
177 |
|
|
<xsl:param name="cbname" /> |
178 |
|
|
|
179 |
|
|
<!-- |
180 |
|
|
action: <xsl:value-of select="$action" /> |
181 |
|
|
payload: <xsl:value-of select="$payload" /> |
182 |
|
|
--> |
183 |
|
|
|
184 |
|
|
<!-- |
185 |
|
|
<xsl:if test="$action=5"> |
186 |
|
|
CRUD: CREATE - if |
187 |
|
|
</xsl:if> |
188 |
|
|
--> |
189 |
joko |
1.3 |
|
190 |
joko |
1.4 |
<xsl:copy> |
191 |
joko |
1.3 |
|
192 |
joko |
1.4 |
<xsl:copy-of select="@*" /> |
193 |
|
|
<xsl:apply-templates /> |
194 |
|
|
|
195 |
|
|
<!-- crud action dispatcher --> |
196 |
|
|
<xsl:choose> |
197 |
|
|
|
198 |
|
|
<!-- CREATE --> |
199 |
|
|
<xsl:when test="{$action}=CREATE"> |
200 |
|
|
|
201 |
|
|
<!-- thats a (compact) switch case in xsl lingo! --> |
202 |
|
|
<xsl:choose> |
203 |
|
|
|
204 |
|
|
<xsl:when test="$payload!="> |
205 |
|
|
<xsl:comment> Payload injected on {timestamp} </xsl:comment> |
206 |
|
|
<xsl:value-of select="$payload" /> |
207 |
|
|
</xsl:when> |
208 |
|
|
|
209 |
|
|
<xsl:when test="$mode=callback"> |
210 |
|
|
<xsl:comment> Payload injected via callback <xsl:value-of select="{$cbname}" /> on {$timestamp} </xsl:comment> |
211 |
|
|
<xsl:call-template name="xupdate_element_create_embedded" /> |
212 |
|
|
</xsl:when> |
213 |
|
|
|
214 |
|
|
<xsl:otherwise> |
215 |
|
|
<xsl:comment> Cloned last element as template on {timestamp} </xsl:comment> |
216 |
|
|
<xsl:call-template name="clone_last_element" /> |
217 |
|
|
</xsl:otherwise> |
218 |
|
|
|
219 |
|
|
</xsl:choose> |
220 |
|
|
|
221 |
|
|
</xsl:when> |
222 |
|
|
|
223 |
|
|
<xsl:otherwise> |
224 |
|
|
<xsl:comment> |
225 |
|
|
UNKOWN CRUD ACTION: "<xsl:value-of select="$action" />" on <xsl:value-of select="$now" />. |
226 |
|
|
</xsl:comment> |
227 |
|
|
</xsl:otherwise> |
228 |
|
|
|
229 |
|
|
</xsl:choose> |
230 |
|
|
|
231 |
|
|
</xsl:copy> |
232 |
joko |
1.3 |
|
233 |
|
|
</xsl:template> |
234 |
|
|
|
235 |
joko |
1.1 |
|
236 |
|
|
|
237 |
joko |
1.2 |
<!-- 2.c. The element manipulators: They mungle a specific element inside the current context. --> |
238 |
joko |
1.1 |
|
239 |
|
|
<!-- copy over node 1:1 (use last element as a template) --> |
240 |
|
|
<xsl:template name="clone_last_element"> |
241 |
joko |
1.3 |
<xsl:copy-of select="*[last()]" /> |
242 |
joko |
1.1 |
</xsl:template> |
243 |
|
|
|
244 |
|
|
<xsl:template name="clone_element_nr"> |
245 |
joko |
1.3 |
<!-- TODO: let idx be passed in via "with-param" or s.th.l.th. --> |
246 |
|
|
<xsl:copy-of select="*[2]" /> |
247 |
joko |
1.1 |
</xsl:template> |
248 |
|
|
|
249 |
|
|
<!-- creates instance of new element --> |
250 |
|
|
<xsl:template name="create_fresh_element"> |
251 |
joko |
1.3 |
<xsl:element name="new">content</xsl:element> |
252 |
joko |
1.1 |
</xsl:template> |
253 |
|
|
|
254 |
joko |
1.4 |
<xsl:template name="xupdate_element_create_embedded"> |
255 |
|
|
<xsl:variable name="huhu" select="*[0]/@key">haha</xsl:variable> |
256 |
|
|
<!-- <xsl:transform version="1.1" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
257 |
|
|
<!-- <xupdate:modifications version="1.0" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
258 |
|
|
<!-- <xsl:append select="/addresses" child="last()"> --> |
259 |
|
|
<xsl:element name="address"> |
260 |
|
|
<town>San Francisco</town> |
261 |
|
|
</xsl:element> |
262 |
|
|
<xsl:value-of select="*[0]/@key" /> |
263 |
|
|
<xsl:value-of select="$huhu" /> |
264 |
|
|
<!-- </xupdate:modifications> --> |
265 |
|
|
<!-- </xsl:append> --> |
266 |
|
|
<!-- </xsl:transform> --> |
267 |
|
|
</xsl:template> |
268 |
|
|
|
269 |
joko |
1.1 |
|
270 |
|
|
<!-- inject other stuff --> |
271 |
|
|
<xsl:template name="inject_template"> |
272 |
|
|
<xsl:comment> Hello World! </xsl:comment> |
273 |
|
|
|
274 |
|
|
</xsl:template> |
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
joko |
1.2 |
|
279 |
|
|
<!-- 0. The root node dispatcher. --> |
280 |
|
|
<!-- <xsl:template match="source"><xsl:call-template name="insertChildNode"/></xsl:template> --> |
281 |
|
|
|
282 |
|
|
<!-- 2. This is the manipulation logic: insertChildNode --> |
283 |
|
|
|
284 |
|
|
<!-- 2.a. The "API" method. - now deprecated --> |
285 |
|
|
<xsl:template name="insertChildNode"> |
286 |
|
|
|
287 |
|
|
<!-- manipulate node --> |
288 |
|
|
<xsl:comment> node - begin </xsl:comment> |
289 |
|
|
|
290 |
|
|
<!-- <xsl:copy-of select="source" /> --> |
291 |
joko |
1.1 |
<xsl:copy> |
292 |
joko |
1.2 |
|
293 |
|
|
<!-- Pass through children 1:1. --> |
294 |
joko |
1.1 |
<xsl:apply-templates /> |
295 |
joko |
1.2 |
|
296 |
|
|
<!-- Call one of the manipulators - this implements injection behavior. --> |
297 |
joko |
1.1 |
<xsl:call-template name="clone_last_element" /> |
298 |
joko |
1.2 |
<xsl:call-template name="inject_template" /> |
299 |
|
|
|
300 |
|
|
</xsl:copy> |
301 |
|
|
<xsl:comment> node - end </xsl:comment> |
302 |
|
|
|
303 |
joko |
1.1 |
</xsl:template> |
304 |
|
|
|
305 |
|
|
|
306 |
|
|
</xsl:stylesheet> |
307 |
|
|
|
308 |
|
|
'; |
309 |
|
|
|
310 |
joko |
1.4 |
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
# examples from http://www.xmldb.org/xupdate/xupdate-wd.html#N238f4 |
314 |
|
|
|
315 |
|
|
my $xml_document = qq(<?xml version="1.0"?> |
316 |
|
|
<addresses version="1.0"> |
317 |
|
|
<address id="1"> |
318 |
|
|
<fullname>Andreas Laux</fullname> |
319 |
|
|
<born day='1' month='12' year='1978'/> |
320 |
|
|
<town>Leipzig</town> |
321 |
|
|
<country>Germany</country> |
322 |
|
|
</address> |
323 |
|
|
</addresses> |
324 |
|
|
); |
325 |
|
|
|
326 |
|
|
# a stripped example (the "xupdate payload") |
327 |
|
|
my $xml_xupdate_stripped = qq( |
328 |
|
|
<xupdate:element name="address"> |
329 |
|
|
<xupdate:attribute name="id">2</xupdate:attribute> |
330 |
|
|
<fullname>Lars Martin</fullname> |
331 |
|
|
<born day='2' month='12' year='1974'/> |
332 |
|
|
<town>Leizig</town> |
333 |
|
|
<country>Germany</country> |
334 |
|
|
</xupdate:element> |
335 |
|
|
); |
336 |
|
|
|
337 |
|
|
# a full xupdate example (including the directive) |
338 |
|
|
my $xml_xupdate_full = qq(<?xml version="1.0"?> |
339 |
|
|
<xupdate:modifications version="1.0" xmlns:xupdate="http://www.xmldb.org/xupdate"> |
340 |
|
|
|
341 |
|
|
<xupdate:insert-after select="/addresses/address[1]" > |
342 |
|
|
|
343 |
|
|
<xupdate:element name="address"> |
344 |
|
|
<xupdate:attribute name="id">2</xupdate:attribute> |
345 |
|
|
<fullname>Lars Martin</fullname> |
346 |
|
|
<born day='2' month='12' year='1974'/> |
347 |
|
|
<town>Leizig</town> |
348 |
|
|
<country>Germany</country> |
349 |
|
|
</xupdate:element> |
350 |
|
|
|
351 |
|
|
</xupdate:insert-after> |
352 |
|
|
|
353 |
|
|
</xupdate:modifications> |
354 |
|
|
); |
355 |
|
|
|
356 |
|
|
sub main_old { |
357 |
|
|
#$xslt->open_xml( Source => f2s($file) ); |
358 |
|
|
#$xslt->open_xml( Source => $xml_xupdate_full ); |
359 |
|
|
#$xslt->xsl_ns('xupdate', 'xsl'); |
360 |
|
|
|
361 |
|
|
#$xslt->process(); |
362 |
|
|
#print $xslt->toString(), "\n"; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
|
366 |
joko |
1.1 |
sub main { |
367 |
|
|
|
368 |
joko |
1.4 |
use XML::XUpdate::XSLT; |
369 |
|
|
my $xupdate = XML::XUpdate::XSLT->new( |
370 |
joko |
1.1 |
warnings => 1 |
371 |
|
|
); |
372 |
joko |
1.4 |
|
373 |
|
|
#print Dumper($xupdate); |
374 |
|
|
|
375 |
|
|
$xupdate->open_document( $xml_document ); |
376 |
|
|
#exit; |
377 |
joko |
1.1 |
|
378 |
joko |
1.4 |
$xupdate->open_xupdate( $xml_xupdate_full ); |
379 |
|
|
|
380 |
|
|
$xupdate->process(); |
381 |
|
|
my $result = $xupdate->toString(); |
382 |
|
|
if ($result) { |
383 |
|
|
print $result, "\n"; |
384 |
|
|
} else { |
385 |
|
|
print "No result.", "\n"; |
386 |
|
|
} |
387 |
|
|
|
388 |
joko |
1.1 |
} |
389 |
|
|
|
390 |
|
|
main(); |
391 |
|
|
print "ready.", "\n"; |
392 |
|
|
|
393 |
|
|
1; |
394 |
|
|
__END__ |