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 |
11 |
|
## first attempt to make up a crud template |
12 |
|
## |
13 |
## Revision 1.2 2003/04/25 12:35:23 joko |
## Revision 1.2 2003/04/25 12:35:23 joko |
14 |
## added some pod |
## added some pod |
15 |
## revamped xsl |
## revamped xsl |
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 |
|
|
115 |
|
|
116 |
<xsl:output method="raw"/> |
<xsl:output method="raw"/> |
117 |
|
|
118 |
<!-- 0. The root node dispatcher. --> |
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
|
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
|
|
<xsl:template match="source"><xsl:call-template name="lock_context" /></xsl:template> |
|
|
|
|
|
<!-- 1. This is the passthru logic (copy all). --> |
|
119 |
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
120 |
|
<!-- activate this --> |
121 |
|
<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 --> |
123 |
|
<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 |
|
|
127 |
<!-- 2.a. The context finder. --> |
<!-- 2.a. The context finder. --> |
128 |
|
|
129 |
<xsl:template name="lock_context"> |
<!-- query part one: the node name --> |
130 |
|
<xsl:template match="source"> |
131 |
<xsl:choose> |
<xsl:choose> |
132 |
|
<!-- query part two: the attrib key and value --> |
133 |
<xsl:when test="@key=expekt"> |
<xsl:when test="@key=expekt"> |
134 |
<xsl:call-template name="inject_kay_motl" /> |
<xsl:call-template name="crud" > |
135 |
|
<xsl:with-param name="action">CREATE</xsl:with-param> |
136 |
|
<xsl:with-param name="mode">callback</xsl:with-param> |
137 |
|
<xsl:with-param name="cbname">abc</xsl:with-param> |
138 |
|
</xsl:call-template> |
139 |
</xsl:when> |
</xsl:when> |
|
|
|
140 |
<xsl:otherwise> |
<xsl:otherwise> |
141 |
<xsl:call-template name="passthru_kay" /> |
<xsl:call-template name="passthru_kay" /> |
142 |
</xsl:otherwise> |
</xsl:otherwise> |
|
|
|
143 |
</xsl:choose> |
</xsl:choose> |
144 |
</xsl:template> |
</xsl:template> |
145 |
|
|
157 |
</xsl:template> |
</xsl:template> |
158 |
|
|
159 |
<xsl:template name="inject_kay_motl"> |
<xsl:template name="inject_kay_motl"> |
160 |
|
<xsl:param name="payload" /> |
161 |
|
<xsl:value-of select="$payload" /> |
162 |
<xsl:copy> |
<xsl:copy> |
163 |
<xsl:copy-of select="@*" /> |
<xsl:copy-of select="@*" /> |
164 |
<xsl:apply-templates /> |
<xsl:apply-templates /> |
172 |
</xsl:copy> |
</xsl:copy> |
173 |
</xsl:template> |
</xsl:template> |
174 |
|
|
175 |
|
<xsl:template name="crud"> |
176 |
|
|
177 |
|
<xsl:param name="action" /> |
178 |
|
<xsl:param name="payload" /> |
179 |
|
<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 |
|
<xsl:copy> |
194 |
|
|
195 |
|
<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> |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
<!-- 2.c. The element manipulators: They mungle a specific element inside the current context. --> |
<!-- 2.c. The element manipulators: They mungle a specific element inside the current context. --> |
241 |
|
|
242 |
<!-- copy over node 1:1 (use last element as a template) --> |
<!-- copy over node 1:1 (use last element as a template) --> |
243 |
<xsl:template name="clone_last_element"> |
<xsl:template name="clone_last_element"> |
244 |
<xsl:copy-of select="*[last()]" /> |
<xsl:copy-of select="*[last()]" /> |
245 |
</xsl:template> |
</xsl:template> |
246 |
|
|
247 |
<xsl:template name="clone_element_nr"> |
<xsl:template name="clone_element_nr"> |
248 |
<!-- TODO: let idx be passed in via "with-param" or s.th.l.th. --> |
<!-- TODO: let idx be passed in via "with-param" or s.th.l.th. --> |
249 |
<xsl:copy-of select="*[2]" /> |
<xsl:copy-of select="*[2]" /> |
250 |
</xsl:template> |
</xsl:template> |
251 |
|
|
252 |
<!-- creates instance of new element --> |
<!-- creates instance of new element --> |
253 |
<xsl:template name="create_fresh_element"> |
<xsl:template name="create_fresh_element"> |
254 |
<xsl:element name="new">content</xsl:element> |
<xsl:element name="new">content</xsl:element> |
255 |
|
</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> |
</xsl:template> |
271 |
|
|
272 |
|
|
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__ |