7 |
# $Id$ |
# $Id$ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
|
# Revision 1.6 2003/05/07 03:11:28 joko |
11 |
|
# updated pod: new section "References", added item to "Todo" section |
12 |
|
# |
13 |
|
# Revision 1.5 2003/05/06 14:24:06 joko |
14 |
|
# doesn't use Carp any more |
15 |
|
# updated pod |
16 |
|
# attempt to rewrite xml to make it more human readable using helper modules from CPAN (node indentation, etc.) |
17 |
|
# |
18 |
|
# Revision 1.4 2003/05/01 23:40:32 joko |
19 |
|
# minor update: commented debugging part |
20 |
|
# |
21 |
# Revision 1.3 2003/05/01 20:11:49 joko |
# Revision 1.3 2003/05/01 20:11:49 joko |
22 |
# * added pod from xupdate.pl |
# * added pod from xupdate.pl |
23 |
# - extracted xml to external files |
# - extracted xml to external files |
33 |
|
|
34 |
=head1 NAME |
=head1 NAME |
35 |
|
|
36 |
XML::XUpdate::XSLT - A perl module for updating xml documents using XUpdate via XSLT. |
XML::XUpdate::XSLT - A perl module for updating xml documents using XUpdate via XSLT. |
37 |
|
|
38 |
|
|
39 |
=head3 Overview |
=head3 Overview |
45 |
xupdate - Process XUpdate commands against an XML document. |
xupdate - Process XUpdate commands against an XML document. |
46 |
|
|
47 |
|
|
48 |
=head3 Their implementations differ: |
=head4 Their implementations differ: |
49 |
|
|
50 |
1. xupdate (by Petr Pajas) uses ... |
1. xupdate (by Petr Pajas) uses ... |
51 |
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
60 |
2. This xupdate attempts to implement the XUpdate specs using XSLT only. |
2. This xupdate attempts to implement the XUpdate specs using XSLT only. |
61 |
|
|
62 |
|
|
63 |
=head3 Yet another xupdate - facts in short: |
=head4 Yet another xupdate - facts in short: |
64 |
|
|
65 |
S: It would be nice to have a pure perl thingy which does (almost) the same stuff.... |
S: It would be nice to have a pure perl thingy which does (almost) the same stuff.... |
66 |
|
|
71 |
|
|
72 |
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
73 |
|
|
74 |
Q: Should we follow the CRUD path first? (CRUD means: Create, Retrieve, Update, Delete) |
Q: Should we follow the CRUD path first? |
75 |
|
(CRUD is the acronym for the datastore action primitives: Create, Retrieve, Update, Delete) |
76 |
S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD |
S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD |
77 |
|
|
78 |
|
|
79 |
=head4 Todo |
=head3 References |
80 |
|
|
81 |
|
- XUpdate: |
82 |
|
Requirements: http://www.xmldb.org/xupdate/xupdate-req.html |
83 |
|
Working Draft: http://www.xmldb.org/xupdate/xupdate-wd.html |
84 |
|
- XML API: |
85 |
|
- XML::XUpdate::LibXML: http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/lib/XML/XUpdate/LibXML.pm |
86 |
|
- XSL / XSLT: |
87 |
|
http://www.w3.org/TR/xslt |
88 |
|
http://www.xsl-rp.de/ |
89 |
|
http://xml.klute-thiemann.de/w3c-de/REC-xslt-20020318/ |
90 |
|
http://xmlxslt.sourceforge.net/ |
91 |
|
- misc pointers: |
92 |
|
"Re: Modify XML documents": http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/1265431 |
93 |
|
XSL Extensions: http://xmlsoft.org/XSLT/extensions.html |
94 |
|
EXSLT: http://www.exslt.org/set/functions/difference/index.html |
95 |
|
"how to insert element at required position in document tree?": http://p2p.wrox.com/archive/xslt/2001-06/98.asp |
96 |
|
XML APIs for Databases: http://www.javaworld.com/javaworld/jw-01-2000/jw-01-dbxml.html |
97 |
|
|
98 |
|
|
99 |
|
=head3 Todo |
100 |
|
|
101 |
o What about proper encoding? (ISO-8859-1 or UTF-8) |
o What about proper encoding? (ISO-8859-1 or UTF-8) |
102 |
o Is it possible to create the required "xsl_template.xml" at runtime via XSL itself? |
o Is it possible to create the required "xsl_template.xml" at runtime via XSL itself? |
103 |
o Cache contents of external files (*.xml). Performance! |
o Cache contents of external files (*.xml). Performance! |
104 |
|
o Can exception / error-code handling be improved somehow? |
105 |
|
Esp.: It would be interesting, if the XUpdate payload actually could be applied, or not... |
106 |
|
o <xupdate:if>, etc. |
107 |
|
|
108 |
|
|
109 |
=cut |
=cut |
116 |
use strict; |
use strict; |
117 |
use warnings; |
use warnings; |
118 |
|
|
|
use XML::XSLT 0.41; |
|
|
use Carp; |
|
|
use File::Basename; |
|
119 |
use Data::Dumper; |
use Data::Dumper; |
120 |
|
use File::Basename; |
121 |
|
use XML::XSLT 0.41; |
122 |
|
use XML::XUpdate::Rewrite; |
123 |
|
|
124 |
# Namespace constants |
# Namespace constants |
125 |
|
|
188 |
} |
} |
189 |
|
|
190 |
# First, translate the xupdate payload to xsl. |
# First, translate the xupdate payload to xsl. |
191 |
# FIXME: do DOM only! |
# FIXME: do DOM only! (don't use "->toString") |
192 |
sub _calculate { |
sub _calculate { |
193 |
my $self = shift; |
my $self = shift; |
194 |
$self->{XSLT_ENGINE_PREP} = XML::XSLT->new( |
$self->{XSLT_ENGINE_PREP} = XML::XSLT->new( |
205 |
# FIXME: do DOM only! |
# FIXME: do DOM only! |
206 |
sub _apply { |
sub _apply { |
207 |
my $self = shift; |
my $self = shift; |
208 |
#print $self->get_stylesheet("_worker"), "\n"; |
|
209 |
|
# debug - print the calculated xsl on STDERR |
210 |
|
print STDERR $self->get_stylesheet("_worker"), "\n"; |
211 |
|
|
212 |
#return; |
#return; |
213 |
$self->{XSLT_ENGINE_LIVE} = XML::XSLT->new( |
$self->{XSLT_ENGINE_LIVE} = XML::XSLT->new( |
214 |
Source => $self->get_stylesheet("_worker"), |
Source => $self->get_stylesheet("_worker"), |
222 |
|
|
223 |
sub toString { |
sub toString { |
224 |
my $self = shift; |
my $self = shift; |
225 |
return $self->{XML}->{result}; |
|
226 |
|
# use rest of argument list as hash of option values |
227 |
|
my $options = {@_}; |
228 |
|
|
229 |
|
# short circuit - just return what we have - don't modify anything |
230 |
|
return $self->{XML}->{result} unless $options and $options->{rewrite}; |
231 |
|
|
232 |
|
# Rewrite the xml document with certain engine to desired style. |
233 |
|
# 'engine' defaults to "XMLParser" if not specified. |
234 |
|
my $rewrite = XML::XUpdate::Rewrite->new( style => $options->{mode}, engine => $options->{using} ); |
235 |
|
$rewrite->set_document( $self->{XML}->{result} ); |
236 |
|
# TODO: Implement a configurable fallback here to return the un-rewritten payload if desired. |
237 |
|
$rewrite->process() or die(__PACKAGE__ . ": Error while rewriting XML document."); |
238 |
|
return $rewrite->get_document(); |
239 |
} |
} |
240 |
|
|
241 |
|
|
242 |
###################################################################### |
###################################################################### |
243 |
# AUXILIARY METHODS |
# AUXILIARY METHODS |
244 |
|
|
245 |
# Argument parsing (with backwards compatibility hook). |
# Argument list parsing. |
246 |
|
# ... from XML::XUpdate::LibXML (w/o backwards compatibility hook). |
247 |
|
# Could this make up a Class::AutoFill::__read_arglist which transparently |
248 |
|
# makes object attributes from constructor arguments in a configurable way? |
249 |
sub __parse_args { |
sub __parse_args { |
250 |
my $self = shift; |
my $self = shift; |
251 |
my %args; |
my %args; |
252 |
|
|
253 |
if(@_ % 2 ) { |
if ( @_ % 2 ) { |
254 |
$args{dummy} = shift; |
$args{dummy} = shift; |
255 |
%args = (%args, @_); |
%args = (%args, @_); |
256 |
} else { |
} else { |
276 |
if (! -e $filename) { |
if (! -e $filename) { |
277 |
$filename = dirname(__FILE__) . "/$filename"; |
$filename = dirname(__FILE__) . "/$filename"; |
278 |
if (! -e $filename) { |
if (! -e $filename) { |
279 |
croak("File $filename does not exist."); |
die("File $filename does not exist."); |
280 |
} |
} |
281 |
} |
} |
282 |
|
|