7 |
# $Id$ |
# $Id$ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
|
# Revision 1.3 2003/05/01 20:11:49 joko |
11 |
|
# * added pod from xupdate.pl |
12 |
|
# - extracted xml to external files |
13 |
|
# |
14 |
# Revision 1.1 2003/04/30 02:36:32 joko |
# Revision 1.1 2003/04/30 02:36:32 joko |
15 |
# initial commit |
# initial commit |
16 |
# |
# |
17 |
# |
# |
18 |
############################################################################### |
############################################################################### |
19 |
|
|
20 |
|
=pod |
21 |
|
|
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
24 |
|
|
25 |
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. |
26 |
|
|
27 |
|
|
28 |
|
=head3 Overview |
29 |
|
|
30 |
|
This is not the same xupdate currently available from CPAN at |
31 |
|
http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/xupdate . |
32 |
|
|
33 |
|
Its intention - however - is identical: |
34 |
|
xupdate - Process XUpdate commands against an XML document. |
35 |
|
|
36 |
|
|
37 |
|
=head3 Their implementations differ: |
38 |
|
|
39 |
|
1. xupdate (by Petr Pajas) uses ... |
40 |
|
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
41 |
|
|
42 |
|
... which is based on XML::LibXML which in turn is: |
43 |
|
[...] |
44 |
|
This module is an interface to the gnome libxml2 DOM parser (no SAX parser support yet), |
45 |
|
and the DOM tree. It also provides an XML::XPath-like findnodes() interface, providing |
46 |
|
access to the XPath API in libxml2. |
47 |
|
[...] |
48 |
|
|
49 |
|
2. This xupdate attempts to implement the XUpdate specs using XSLT only. |
50 |
|
|
51 |
|
|
52 |
|
=head3 Yet another xupdate - facts in short: |
53 |
|
|
54 |
|
S: It would be nice to have a pure perl thingy which does (almost) the same stuff.... |
55 |
|
|
56 |
|
Q: Can we achieve compliance with its (XML::XUpdate::LibXML) API? (or just a subset ....) |
57 |
|
|
58 |
|
Q: Can we achieve the processing using CPAN's XML::XSLT? |
59 |
|
S: Proposal: XML::XUpdate::XSLT!? |
60 |
|
|
61 |
|
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
62 |
|
|
63 |
|
Q: Should we follow the CRUD path first? (CRUD means: Create, Retrieve, Update, Delete) |
64 |
|
S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD |
65 |
|
|
66 |
|
|
67 |
|
=head4 Todo |
68 |
|
|
69 |
|
o What about proper encoding? (ISO-8859-1 or UTF-8) |
70 |
|
o Is it possible to create the required "xsl_template.xml" at runtime via XSL itself? |
71 |
|
o Cache contents of external files (*.xml). Performance! |
72 |
|
|
73 |
|
|
74 |
=cut |
=cut |
75 |
|
|
76 |
|
|
83 |
|
|
84 |
use XML::XSLT 0.41; |
use XML::XSLT 0.41; |
85 |
use Carp; |
use Carp; |
86 |
|
use File::Basename; |
87 |
use Data::Dumper; |
use Data::Dumper; |
88 |
|
|
89 |
# Namespace constants |
# Namespace constants |
92 |
|
|
93 |
use vars qw ( $VERSION ); |
use vars qw ( $VERSION ); |
94 |
|
|
95 |
$VERSION = '0.02'; |
$VERSION = '0.01'; |
96 |
|
|
97 |
|
|
98 |
###################################################################### |
###################################################################### |
103 |
my $self = bless {}, $class; |
my $self = bless {}, $class; |
104 |
my $args = $self->__parse_args(@_); |
my $args = $self->__parse_args(@_); |
105 |
|
|
106 |
$self->__init_default_macros(); |
$self->{DEBUG} = $args->{debug}; |
107 |
|
$self->{WARNINGS} = $args->{warnings}; |
108 |
|
|
109 |
#=pod |
$self->__init_default_stylesheets(); |
|
$self->{XSLT_ENGINE} = XML::XSLT->new( |
|
|
Source => $self->get_macro("xupdate2xsl"), |
|
|
debug => $args->{debug}, |
|
|
warnings => $args->{warnings} |
|
|
); |
|
|
#=cut |
|
110 |
|
|
|
#print Dumper($self); |
|
111 |
return $self; |
return $self; |
112 |
|
|
113 |
} |
} |
114 |
|
|
115 |
sub get_macro { |
sub get_stylesheet { |
116 |
|
my $self = shift; |
117 |
|
my $name = shift; |
118 |
|
return $self->{XML}->{xsl}->{$name}; |
119 |
|
} |
120 |
|
|
121 |
|
sub set_stylesheet { |
122 |
my $self = shift; |
my $self = shift; |
123 |
my $name = shift; |
my $name = shift; |
124 |
return $self->{MACROS}->{$name}; |
my $xml = shift; |
125 |
|
my $options = shift; |
126 |
|
if ($options->{encap}) { |
127 |
|
my $template = $self->__slurp_file("xsl_template.xml"); |
128 |
|
# FIXME! What about the quirky? Is there a better one? |
129 |
|
$template =~ s!<xsl:quirky_placeholder />!$xml!; |
130 |
|
$xml = $template; |
131 |
|
} |
132 |
|
$self->{XML}->{xsl}->{$name} = $xml; |
133 |
} |
} |
134 |
|
|
135 |
sub open_document { |
sub open_document { |
148 |
|
|
149 |
sub process { |
sub process { |
150 |
my $self = shift; |
my $self = shift; |
151 |
# first, translate xupdate to xsl |
$self->_calculate(); |
152 |
$self->{XSLT_ENGINE}->open_xml( $self->{XML}->{xupdate} ); |
$self->_apply(); |
|
$self->{XSLT_ENGINE}->process(); |
|
|
$self->{XML}->{XSL} = $self->{XSLT_ENGINE}->toString(); |
|
153 |
} |
} |
154 |
|
|
155 |
|
# First, translate the xupdate payload to xsl. |
156 |
|
# FIXME: do DOM only! |
157 |
|
sub _calculate { |
158 |
|
my $self = shift; |
159 |
|
$self->{XSLT_ENGINE_PREP} = XML::XSLT->new( |
160 |
|
Source => $self->get_stylesheet("xupdate2xsl"), |
161 |
|
debug => $self->{DEBUG}, |
162 |
|
warnings => $self->{WARNINGS} |
163 |
|
); |
164 |
|
$self->{XSLT_ENGINE_PREP}->open_xml( $self->{XML}->{xupdate} ); |
165 |
|
$self->{XSLT_ENGINE_PREP}->process(); |
166 |
|
$self->set_stylesheet( "_worker", $self->{XSLT_ENGINE_PREP}->toString(), { encap => 1 } ); |
167 |
|
} |
168 |
|
|
169 |
|
# After that, use this worker xsl to actually apply the changes to the original document. |
170 |
|
# FIXME: do DOM only! |
171 |
|
sub _apply { |
172 |
|
my $self = shift; |
173 |
|
#print $self->get_stylesheet("_worker"), "\n"; |
174 |
|
#return; |
175 |
|
$self->{XSLT_ENGINE_LIVE} = XML::XSLT->new( |
176 |
|
Source => $self->get_stylesheet("_worker"), |
177 |
|
debug => $self->{DEBUG}, |
178 |
|
warnings => $self->{WARNINGS} |
179 |
|
); |
180 |
|
$self->{XSLT_ENGINE_LIVE}->open_xml( $self->{XML}->{document} ); |
181 |
|
$self->{XSLT_ENGINE_LIVE}->process(); |
182 |
|
$self->{XML}->{result} = $self->{XSLT_ENGINE_LIVE}->toString(); |
183 |
|
} |
184 |
|
|
185 |
sub toString { |
sub toString { |
186 |
my $self = shift; |
my $self = shift; |
187 |
return $self->{XML}->{XSL}; |
return $self->{XML}->{result}; |
188 |
} |
} |
189 |
|
|
190 |
|
|
191 |
###################################################################### |
###################################################################### |
192 |
# AUXILIARY METHODS |
# AUXILIARY METHODS |
193 |
|
|
194 |
# Argument parsing with backwards compatibility. |
# Argument parsing (with backwards compatibility hook). |
195 |
sub __parse_args { |
sub __parse_args { |
196 |
my $self = shift; |
my $self = shift; |
197 |
my %args; |
my %args; |
207 |
} |
} |
208 |
|
|
209 |
|
|
210 |
sub __init_default_macros { |
sub __init_default_stylesheets { |
211 |
my $self = shift; |
my $self = shift; |
212 |
$self->{MACROS}->{xupdate2xsl} = qq(<?xml version="1.0" encoding="ISO-8859-1"?> |
if (my $payload = $self->__slurp_file("xupdate2xsl.xml")) { |
213 |
|
$self->{XML}->{xsl}->{xupdate2xsl} = $payload; |
214 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
} |
215 |
|
} |
216 |
|
|
217 |
|
sub __slurp_file { |
218 |
|
my $self = shift; |
219 |
|
my $filename = shift; |
220 |
|
|
221 |
<!-- |
# does file exist? |
222 |
Purpose of this XML Stylesheet is to implement a set of templates |
if (! -e $filename) { |
223 |
to translate XUpdate lingo into an intermediate xslt stylesheet |
$filename = dirname(__FILE__) . "/$filename"; |
224 |
which actually performs the update to the original xml document. |
if (! -e $filename) { |
225 |
--> |
croak("File $filename does not exist."); |
226 |
|
} |
227 |
<xsl:output method="xml" /> |
} |
|
|
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
|
|
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
|
|
<!-- activate this --> |
|
|
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
|
|
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
|
|
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
|
|
|
|
|
<!-- 2. This is the translation part: XUpdate becomes XSLT --> |
|
|
|
|
|
<!-- This node "encapsulates" common infrastructure. --> |
|
|
<xsl:template match="xupdate:modifications"> |
|
|
|
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
|
|
<!-- in fact this is the xsl from above translated to be able to be generated by xsl itself! --> |
|
|
<xsl:comment> 1. passthru logic </xsl:comment> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="name">passthru</xsl:attribute> |
|
|
<xsl:element name="xsl:copy"><xsl:element name="xsl:apply-templates" /></xsl:element> |
|
|
</xsl:element> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="match">*</xsl:attribute> |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
|
|
</xsl:element> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="match">comment()</xsl:attribute> |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
|
|
</xsl:element> |
|
|
|
|
|
<!-- continue with all inline nodes --> |
|
|
<xsl:apply-templates /> |
|
|
|
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node "encapsulates" infrastructure for handling the directives. --> |
|
|
<xsl:template match="xupdate:insert-after"> |
|
|
<xsl:comment> 2. context finder </xsl:comment> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
|
|
<xsl:template match="xupdate:element"> |
|
|
<xsl:comment> 3. rewrite / vivify elements/attributes </xsl:comment> |
|
|
<xsl:element name="xsl:element"> |
|
|
<xsl:copy-of select="@*"/> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:element> |
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
|
|
<xsl:template match="xupdate:attribute"> |
|
|
<xsl:element name="xsl:attribute"> |
|
|
<xsl:copy-of select="@*"/> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:element> |
|
|
</xsl:template> |
|
|
|
|
|
</xsl:stylesheet> |
|
228 |
|
|
229 |
); |
open(FH, "<" . $filename); |
230 |
|
my @lines = <FH>; |
231 |
|
my $content = join("", @lines); |
232 |
|
close(FH); |
233 |
|
return $content; |
234 |
} |
} |
235 |
|
|
236 |
1; |
1; |