/[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.2 - (hide annotations)
Fri Apr 25 12:35:23 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.1: +78 -37 lines
File MIME type: text/plain
added some pod
revamped xsl

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

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