7 |
# $Id$ |
# $Id$ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
# Revision 1.2 2003/05/01 17:03:52 joko |
# Revision 1.3 2003/05/01 20:11:49 joko |
11 |
# revamped processing: a) _calculate and b) _apply (NEW) |
# * 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 |
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 |
###################################################################### |
###################################################################### |
106 |
$self->{DEBUG} = $args->{debug}; |
$self->{DEBUG} = $args->{debug}; |
107 |
$self->{WARNINGS} = $args->{warnings}; |
$self->{WARNINGS} = $args->{warnings}; |
108 |
|
|
109 |
$self->__init_default_macros(); |
$self->__init_default_stylesheets(); |
110 |
|
|
111 |
return $self; |
return $self; |
112 |
|
|
124 |
my $xml = shift; |
my $xml = shift; |
125 |
my $options = shift; |
my $options = shift; |
126 |
if ($options->{encap}) { |
if ($options->{encap}) { |
127 |
$xml = qq(<?xml version="1.0" encoding="ISO-8859-1"?> |
my $template = $self->__slurp_file("xsl_template.xml"); |
128 |
|
# FIXME! What about the quirky? Is there a better one? |
129 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
$template =~ s!<xsl:quirky_placeholder />!$xml!; |
130 |
|
$xml = $template; |
|
<!-- |
|
|
This XML Stylesheet is the result of a translation from |
|
|
XUpdate lingo. It actually applies the payload to the |
|
|
original xml document. (it's the second processing step!) |
|
|
--> |
|
|
|
|
|
<xsl:output method="xml" /> |
|
|
|
|
|
$xml |
|
|
|
|
|
</xsl:stylesheet> |
|
|
); |
|
131 |
} |
} |
132 |
$self->{XML}->{xsl}->{$name} = $xml; |
$self->{XML}->{xsl}->{$name} = $xml; |
133 |
} |
} |
182 |
$self->{XML}->{result} = $self->{XSLT_ENGINE_LIVE}->toString(); |
$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}->{result}; |
return $self->{XML}->{result}; |
207 |
} |
} |
208 |
|
|
209 |
|
|
210 |
sub __init_default_macros { |
sub __init_default_stylesheets { |
211 |
my $self = shift; |
my $self = shift; |
212 |
$self->{XML}->{xsl}->{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 |
in a second step. |
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; |