4 |
## $Id$ |
## $Id$ |
5 |
## ------------------------------------------------------------------------- |
## ------------------------------------------------------------------------- |
6 |
## $Log$ |
## $Log$ |
7 |
|
## Revision 1.4 2003/05/01 19:47:09 joko |
8 |
|
## attempts to get things going |
9 |
|
## |
10 |
## Revision 1.3 2003/04/26 01:42:39 joko |
## Revision 1.3 2003/04/26 01:42:39 joko |
11 |
## first attempt to make up a crud template |
## first attempt to make up a crud template |
12 |
## |
## |
76 |
|
|
77 |
my $file = 'c:/home/amo/develop/top-scores.net/ts/backend/var/resources/control/import_select.xml'; |
my $file = 'c:/home/amo/develop/top-scores.net/ts/backend/var/resources/control/import_select.xml'; |
78 |
|
|
79 |
my $stylesheet = '<?xml version="1.0" encoding="ISO-8859-1"?> |
|
80 |
|
#my $stylesheet_xupdate = ' |
81 |
|
|
82 |
|
my $stylesheet_xupdate = '<?xml version="1.0" encoding="ISO-8859-1"?> |
83 |
|
|
84 |
<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"> |
85 |
|
|
120 |
<!-- activate this --> |
<!-- activate this --> |
121 |
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
122 |
<!-- 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 --> |
123 |
<!-- <xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> --> |
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
124 |
|
|
125 |
<!-- 2. This is the crud API. --> |
<!-- 2. This is the crud API. --> |
126 |
|
|
132 |
<!-- query part two: the attrib key and value --> |
<!-- query part two: the attrib key and value --> |
133 |
<xsl:when test="@key=expekt"> |
<xsl:when test="@key=expekt"> |
134 |
<xsl:call-template name="crud" > |
<xsl:call-template name="crud" > |
135 |
<xsl:with-param name="action">5</xsl:with-param> |
<xsl:with-param name="action">CREATE</xsl:with-param> |
136 |
<xsl:with-param name="payload">------- payload -------</xsl:with-param> |
<xsl:with-param name="mode">callback</xsl:with-param> |
137 |
|
<xsl:with-param name="cbname">abc</xsl:with-param> |
138 |
</xsl:call-template> |
</xsl:call-template> |
139 |
</xsl:when> |
</xsl:when> |
140 |
<xsl:otherwise> |
<xsl:otherwise> |
173 |
</xsl:template> |
</xsl:template> |
174 |
|
|
175 |
<xsl:template name="crud"> |
<xsl:template name="crud"> |
|
<xsl:param name="action" /> |
|
|
<xsl:param name="payload" /> |
|
|
action: <xsl:value-of select="$action" /> |
|
|
payload: <xsl:value-of select="$payload" /> |
|
|
|
|
|
<xsl:if test="$action=5"> |
|
|
CRUD: CREATE |
|
|
</xsl:if> |
|
176 |
|
|
177 |
<xsl:copy> |
<xsl:param name="action" /> |
178 |
<xsl:copy-of select="@*" /> |
<xsl:param name="payload" /> |
179 |
<xsl:apply-templates /> |
<xsl:param name="mode" /> |
180 |
|
<xsl:param name="cbname" /> |
181 |
|
|
182 |
|
<!-- |
183 |
|
action: <xsl:value-of select="$action" /> |
184 |
|
payload: <xsl:value-of select="$payload" /> |
185 |
|
--> |
186 |
|
|
187 |
|
<!-- |
188 |
|
<xsl:if test="$action=5"> |
189 |
|
CRUD: CREATE - if |
190 |
|
</xsl:if> |
191 |
|
--> |
192 |
|
|
193 |
<!-- crud action dispatcher --> |
<xsl:copy> |
|
<xsl:choose> |
|
|
<xsl:when test="{$action}=5"> |
|
|
<!-- <xsl:when test="blah=\'blub\'"> --> |
|
|
CRUD: CREATE |
|
|
<xsl:call-template name="clone_last_element" /> |
|
|
</xsl:when> |
|
|
<xsl:otherwise> |
|
|
<xsl:comment> UNKOWN CRUD ACTION! </xsl:comment> |
|
|
</xsl:otherwise> |
|
|
</xsl:choose> |
|
194 |
|
|
195 |
</xsl:copy> |
<xsl:copy-of select="@*" /> |
196 |
|
<xsl:apply-templates /> |
197 |
|
|
198 |
|
<!-- crud action dispatcher --> |
199 |
|
<xsl:choose> |
200 |
|
|
201 |
|
<!-- CREATE --> |
202 |
|
<xsl:when test="{$action}=CREATE"> |
203 |
|
|
204 |
|
<!-- thats a (compact) switch case in xsl lingo! --> |
205 |
|
<xsl:choose> |
206 |
|
|
207 |
|
<xsl:when test="$payload!="> |
208 |
|
<xsl:comment> Payload injected on {timestamp} </xsl:comment> |
209 |
|
<xsl:value-of select="$payload" /> |
210 |
|
</xsl:when> |
211 |
|
|
212 |
|
<xsl:when test="$mode=callback"> |
213 |
|
<xsl:comment> Payload injected via callback <xsl:value-of select="{$cbname}" /> on {$timestamp} </xsl:comment> |
214 |
|
<xsl:call-template name="xupdate_element_create_embedded" /> |
215 |
|
</xsl:when> |
216 |
|
|
217 |
|
<xsl:otherwise> |
218 |
|
<xsl:comment> Cloned last element as template on {timestamp} </xsl:comment> |
219 |
|
<xsl:call-template name="clone_last_element" /> |
220 |
|
</xsl:otherwise> |
221 |
|
|
222 |
|
</xsl:choose> |
223 |
|
|
224 |
|
</xsl:when> |
225 |
|
|
226 |
|
<xsl:otherwise> |
227 |
|
<xsl:comment> |
228 |
|
UNKOWN CRUD ACTION: "<xsl:value-of select="$action" />" on <xsl:value-of select="$now" />. |
229 |
|
</xsl:comment> |
230 |
|
</xsl:otherwise> |
231 |
|
|
232 |
|
</xsl:choose> |
233 |
|
|
234 |
|
</xsl:copy> |
235 |
|
|
236 |
</xsl:template> |
</xsl:template> |
237 |
|
|
254 |
<xsl:element name="new">content</xsl:element> |
<xsl:element name="new">content</xsl:element> |
255 |
</xsl:template> |
</xsl:template> |
256 |
|
|
257 |
|
<xsl:template name="xupdate_element_create_embedded"> |
258 |
|
<xsl:variable name="huhu" select="*[0]/@key">haha</xsl:variable> |
259 |
|
<!-- <xsl:transform version="1.1" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
260 |
|
<!-- <xupdate:modifications version="1.0" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
261 |
|
<!-- <xsl:append select="/addresses" child="last()"> --> |
262 |
|
<xsl:element name="address"> |
263 |
|
<town>San Francisco</town> |
264 |
|
</xsl:element> |
265 |
|
<xsl:value-of select="*[0]/@key" /> |
266 |
|
<xsl:value-of select="$huhu" /> |
267 |
|
<!-- </xupdate:modifications> --> |
268 |
|
<!-- </xsl:append> --> |
269 |
|
<!-- </xsl:transform> --> |
270 |
|
</xsl:template> |
271 |
|
|
272 |
|
|
273 |
<!-- inject other stuff --> |
<!-- inject other stuff --> |
274 |
<xsl:template name="inject_template"> |
<xsl:template name="inject_template"> |
310 |
|
|
311 |
'; |
'; |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
# examples from http://www.xmldb.org/xupdate/xupdate-wd.html#N238f4 |
317 |
|
|
318 |
|
my $xml_document = qq(<?xml version="1.0"?> |
319 |
|
<addresses version="1.0"> |
320 |
|
<address id="1"> |
321 |
|
<fullname>Andreas Laux</fullname> |
322 |
|
<born day='1' month='12' year='1978'/> |
323 |
|
<town>Leipzig</town> |
324 |
|
<country>Germany</country> |
325 |
|
</address> |
326 |
|
</addresses> |
327 |
|
); |
328 |
|
|
329 |
|
# a stripped example (the "xupdate payload") |
330 |
|
my $xml_xupdate_stripped = qq( |
331 |
|
<xupdate:element name="address"> |
332 |
|
<xupdate:attribute name="id">2</xupdate:attribute> |
333 |
|
<fullname>Lars Martin</fullname> |
334 |
|
<born day='2' month='12' year='1974'/> |
335 |
|
<town>Leizig</town> |
336 |
|
<country>Germany</country> |
337 |
|
</xupdate:element> |
338 |
|
); |
339 |
|
|
340 |
|
# a full xupdate example (including the directive) |
341 |
|
my $xml_xupdate_full = qq(<?xml version="1.0"?> |
342 |
|
<xupdate:modifications version="1.0" xmlns:xupdate="http://www.xmldb.org/xupdate"> |
343 |
|
|
344 |
|
<xupdate:insert-after select="/addresses/address[1]" > |
345 |
|
|
346 |
|
<xupdate:element name="address"> |
347 |
|
<xupdate:attribute name="id">2</xupdate:attribute> |
348 |
|
<fullname>Lars Martin</fullname> |
349 |
|
<born day='2' month='12' year='1974'/> |
350 |
|
<town>Leizig</town> |
351 |
|
<country>Germany</country> |
352 |
|
</xupdate:element> |
353 |
|
|
354 |
|
</xupdate:insert-after> |
355 |
|
|
356 |
|
</xupdate:modifications> |
357 |
|
); |
358 |
|
|
359 |
|
sub main_old { |
360 |
|
#$xslt->open_xml( Source => f2s($file) ); |
361 |
|
#$xslt->open_xml( Source => $xml_xupdate_full ); |
362 |
|
#$xslt->xsl_ns('xupdate', 'xsl'); |
363 |
|
|
364 |
|
#$xslt->process(); |
365 |
|
#print $xslt->toString(), "\n"; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
sub main { |
sub main { |
370 |
|
|
371 |
my $xslt = XML::XSLT->new( |
use XML::XUpdate::XSLT; |
372 |
Source => $stylesheet, |
my $xupdate = XML::XUpdate::XSLT->new( |
|
#debug => 1, |
|
373 |
warnings => 1 |
warnings => 1 |
374 |
); |
); |
375 |
|
|
376 |
|
#print Dumper($xupdate); |
377 |
|
|
378 |
|
$xupdate->open_document( $xml_document ); |
379 |
|
#exit; |
380 |
|
|
381 |
#print f2s($file), "\n"; |
$xupdate->open_xupdate( $xml_xupdate_full ); |
382 |
|
|
383 |
$xslt->open_xml( Source => f2s($file) ); |
$xupdate->process(); |
384 |
|
my $result = $xupdate->toString(); |
385 |
$xslt->process(); |
if ($result) { |
386 |
print $xslt->toString(), "\n"; |
print $result, "\n"; |
387 |
|
} else { |
388 |
|
print "No result.", "\n"; |
389 |
|
} |
390 |
|
|
391 |
} |
} |
392 |
|
|
393 |
main(); |
main(); |
394 |
print "ready.", "\n"; |
print "ready.", "\n"; |
395 |
|
|
|
|
|
396 |
1; |
1; |
397 |
__END__ |
__END__ |