1 |
joko |
1.1 |
############################################################################## |
2 |
|
|
# |
3 |
|
|
# Perl module: XML::XUpdate::XSLT |
4 |
|
|
# |
5 |
|
|
# By Andreas Motl, andreas.motl@ilo.de |
6 |
|
|
# |
7 |
|
|
# $Id$ |
8 |
|
|
# |
9 |
|
|
# $Log: XSLT.pm,v $ |
10 |
|
|
# |
11 |
|
|
############################################################################### |
12 |
|
|
|
13 |
|
|
=head1 NAME |
14 |
|
|
|
15 |
|
|
XML::XUpdate::XSLT - A perl module for updating xml documents using XUpdate via XSLT. |
16 |
|
|
|
17 |
|
|
=cut |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
###################################################################### |
21 |
|
|
package XML::XUpdate::XSLT; |
22 |
|
|
###################################################################### |
23 |
|
|
|
24 |
|
|
use strict; |
25 |
|
|
use warnings; |
26 |
|
|
|
27 |
|
|
use XML::XSLT 0.41; |
28 |
|
|
use Carp; |
29 |
|
|
use Data::Dumper; |
30 |
|
|
|
31 |
|
|
# Namespace constants |
32 |
|
|
|
33 |
|
|
use constant NS_XUPDATE => 'http://www.xmldb.org/xupdate'; |
34 |
|
|
|
35 |
|
|
use vars qw ( $VERSION ); |
36 |
|
|
|
37 |
|
|
$VERSION = '0.02'; |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
###################################################################### |
41 |
|
|
# PUBLIC DEFINITIONS |
42 |
|
|
|
43 |
|
|
sub new { |
44 |
|
|
my $class = shift; |
45 |
|
|
my $self = bless {}, $class; |
46 |
|
|
my $args = $self->__parse_args(@_); |
47 |
|
|
|
48 |
|
|
$self->__init_default_macros(); |
49 |
|
|
|
50 |
|
|
#=pod |
51 |
|
|
$self->{XSLT_ENGINE} = XML::XSLT->new( |
52 |
|
|
Source => $self->get_macro("xupdate2xsl"), |
53 |
|
|
debug => $args->{debug}, |
54 |
|
|
warnings => $args->{warnings} |
55 |
|
|
); |
56 |
|
|
#=cut |
57 |
|
|
|
58 |
|
|
#print Dumper($self); |
59 |
|
|
return $self; |
60 |
|
|
|
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
sub get_macro { |
64 |
|
|
my $self = shift; |
65 |
|
|
my $name = shift; |
66 |
|
|
return $self->{MACROS}->{$name}; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
sub open_document { |
70 |
|
|
my $self = shift; |
71 |
|
|
my $xml = shift; |
72 |
|
|
# FIXME: check for filename, filehandle and URL (etc.) |
73 |
|
|
$self->{XML}->{document} = $xml; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
sub open_xupdate { |
77 |
|
|
my $self = shift; |
78 |
|
|
my $xml = shift; |
79 |
|
|
# FIXME: check for filename, filehandle and URL (etc.) |
80 |
|
|
$self->{XML}->{xupdate} = $xml; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
sub process { |
84 |
|
|
my $self = shift; |
85 |
|
|
# first, translate xupdate to xsl |
86 |
|
|
$self->{XSLT_ENGINE}->open_xml( $self->{XML}->{xupdate} ); |
87 |
|
|
$self->{XSLT_ENGINE}->process(); |
88 |
|
|
$self->{XML}->{XSL} = $self->{XSLT_ENGINE}->toString(); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
sub toString { |
92 |
|
|
my $self = shift; |
93 |
|
|
return $self->{XML}->{XSL}; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
###################################################################### |
98 |
|
|
# AUXILIARY METHODS |
99 |
|
|
|
100 |
|
|
# Argument parsing with backwards compatibility. |
101 |
|
|
sub __parse_args { |
102 |
|
|
my $self = shift; |
103 |
|
|
my %args; |
104 |
|
|
|
105 |
|
|
if(@_ % 2 ) { |
106 |
|
|
$args{dummy} = shift; |
107 |
|
|
%args = (%args, @_); |
108 |
|
|
} else { |
109 |
|
|
%args = @_; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
return \%args; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
sub __init_default_macros { |
117 |
|
|
my $self = shift; |
118 |
|
|
$self->{MACROS}->{xupdate2xsl} = qq(<?xml version="1.0" encoding="ISO-8859-1"?> |
119 |
|
|
|
120 |
|
|
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
121 |
|
|
|
122 |
|
|
<!-- |
123 |
|
|
Purpose of this XML Stylesheet is to implement a set of templates |
124 |
|
|
to translate XUpdate lingo into an intermediate xslt stylesheet |
125 |
|
|
which actually performs the update to the original xml document. |
126 |
|
|
--> |
127 |
|
|
|
128 |
|
|
<xsl:output method="xml" /> |
129 |
|
|
|
130 |
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
131 |
|
|
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
132 |
|
|
<!-- activate this --> |
133 |
|
|
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
134 |
|
|
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
135 |
|
|
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
136 |
|
|
|
137 |
|
|
<!-- 2. This is the translation part: XUpdate becomes XSLT --> |
138 |
|
|
|
139 |
|
|
<!-- This node "encapsulates" common infrastructure. --> |
140 |
|
|
<xsl:template match="xupdate:modifications"> |
141 |
|
|
|
142 |
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
143 |
|
|
<!-- in fact this is the xsl from above translated to be able to be generated by xsl itself! --> |
144 |
|
|
<xsl:comment> 1. passthru logic </xsl:comment> |
145 |
|
|
<xsl:element name="xsl:template"> |
146 |
|
|
<xsl:attribute name="name">passthru</xsl:attribute> |
147 |
|
|
<xsl:element name="xsl:copy"><xsl:element name="xsl:apply-templates" /></xsl:element> |
148 |
|
|
</xsl:element> |
149 |
|
|
<xsl:element name="xsl:template"> |
150 |
|
|
<xsl:attribute name="match">*</xsl:attribute> |
151 |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
152 |
|
|
</xsl:element> |
153 |
|
|
<xsl:element name="xsl:template"> |
154 |
|
|
<xsl:attribute name="match">comment()</xsl:attribute> |
155 |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
156 |
|
|
</xsl:element> |
157 |
|
|
|
158 |
|
|
<!-- continue with all inline nodes --> |
159 |
|
|
<xsl:apply-templates /> |
160 |
|
|
|
161 |
|
|
</xsl:template> |
162 |
|
|
|
163 |
|
|
<!-- This node "encapsulates" infrastructure for handling the directives. --> |
164 |
|
|
<xsl:template match="xupdate:insert-after"> |
165 |
|
|
<xsl:comment> 2. context finder </xsl:comment> |
166 |
|
|
<xsl:apply-templates /> |
167 |
|
|
</xsl:template> |
168 |
|
|
|
169 |
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
170 |
|
|
<xsl:template match="xupdate:element"> |
171 |
|
|
<xsl:comment> 3. rewrite / vivify elements/attributes </xsl:comment> |
172 |
|
|
<xsl:element name="xsl:element"> |
173 |
|
|
<xsl:copy-of select="@*"/> |
174 |
|
|
<xsl:apply-templates /> |
175 |
|
|
</xsl:element> |
176 |
|
|
</xsl:template> |
177 |
|
|
|
178 |
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
179 |
|
|
<xsl:template match="xupdate:attribute"> |
180 |
|
|
<xsl:element name="xsl:attribute"> |
181 |
|
|
<xsl:copy-of select="@*"/> |
182 |
|
|
<xsl:apply-templates /> |
183 |
|
|
</xsl:element> |
184 |
|
|
</xsl:template> |
185 |
|
|
|
186 |
|
|
</xsl:stylesheet> |
187 |
|
|
|
188 |
|
|
); |
189 |
|
|
|
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
1; |
193 |
|
|
__END__ |