Lines 130-139
__PACKAGE__->mk_accessors(qw( do_not_return_source print_warns ));
Link Here
|
130 |
|
130 |
|
131 |
=head2 transform |
131 |
=head2 transform |
132 |
|
132 |
|
133 |
my $output= $xslt_engine->transform( $xml, $xsltfilename ); |
133 |
my $output= $xslt_engine->transform( $xml, $xsltfilename, [$format] ); |
134 |
#Alternatively: |
134 |
#Alternatively: |
135 |
#$output = $xslt_engine->transform({ xml => $xml, file => $file, [parameters => $parameters] }); |
135 |
#$output = $xslt_engine->transform({ xml => $xml, file => $file, [parameters => $parameters], [format => ['chars'|'bytes'|'xmldoc']] }); |
136 |
#$output = $xslt_engine->transform({ xml => $xml, code => $code, [parameters => $parameters] }); |
136 |
#$output = $xslt_engine->transform({ xml => $xml, code => $code, [parameters => $parameters], [format => ['chars'|'bytes'|'xmldoc']] }); |
137 |
if( $xslt_engine->err ) { |
137 |
if( $xslt_engine->err ) { |
138 |
#decide what to do on failure.. |
138 |
#decide what to do on failure.. |
139 |
} |
139 |
} |
Lines 143-149
__PACKAGE__->mk_accessors(qw( do_not_return_source print_warns ));
Link Here
|
143 |
Instead of a filename, you may also pass a URL. |
143 |
Instead of a filename, you may also pass a URL. |
144 |
You may also pass the contents of a xsl file as a string like $code above. |
144 |
You may also pass the contents of a xsl file as a string like $code above. |
145 |
If you do not pass a filename, the last file used is assumed. |
145 |
If you do not pass a filename, the last file used is assumed. |
146 |
Returns the transformed string. |
146 |
Normally returns the transformed string; if you pass format => 'xmldoc' in |
|
|
147 |
the hash format, it returns a xml document object. |
147 |
Check the error number in err to know if something went wrong. |
148 |
Check the error number in err to know if something went wrong. |
148 |
In that case do_not_return_source did determine the return value. |
149 |
In that case do_not_return_source did determine the return value. |
149 |
|
150 |
|
Lines 153-169
sub transform {
Link Here
|
153 |
my $self = shift; |
154 |
my $self = shift; |
154 |
|
155 |
|
155 |
#check parameters |
156 |
#check parameters |
156 |
# old style: $xml, $filename |
157 |
# old style: $xml, $filename, $format |
157 |
# new style: $hashref |
158 |
# new style: $hashref |
158 |
my ( $xml, $filename, $xsltcode ); |
159 |
my ( $xml, $filename, $xsltcode, $format ); |
159 |
my $parameters = {}; |
160 |
my $parameters = {}; |
160 |
if( ref $_[0] eq 'HASH' ) { |
161 |
if( ref $_[0] eq 'HASH' ) { |
161 |
$xml = $_[0]->{xml}; |
162 |
$xml = $_[0]->{xml}; |
162 |
$xsltcode = $_[0]->{code}; |
163 |
$xsltcode = $_[0]->{code}; |
163 |
$filename = $_[0]->{file} if !$xsltcode; #xsltcode gets priority |
164 |
$filename = $_[0]->{file} if !$xsltcode; #xsltcode gets priority |
164 |
$parameters = $_[0]->{parameters} if ref $_[0]->{parameters} eq 'HASH'; |
165 |
$parameters = $_[0]->{parameters} if ref $_[0]->{parameters} eq 'HASH'; |
|
|
166 |
$format = $_[0]->{format} || 'chars'; |
165 |
} else { |
167 |
} else { |
166 |
( $xml, $filename ) = @_; |
168 |
( $xml, $filename, $format ) = @_; |
|
|
169 |
$format ||= 'chars'; |
167 |
} |
170 |
} |
168 |
|
171 |
|
169 |
#Initialized yet? |
172 |
#Initialized yet? |
Lines 193-199
sub transform {
Link Here
|
193 |
$self->_set_error( 5, $@ ); |
196 |
$self->_set_error( 5, $@ ); |
194 |
return $retval; |
197 |
return $retval; |
195 |
} |
198 |
} |
196 |
my $str = eval { |
199 |
my $result = eval { |
197 |
#$parameters is an optional hashref that contains |
200 |
#$parameters is an optional hashref that contains |
198 |
#key-value pairs to be sent to the XSLT. |
201 |
#key-value pairs to be sent to the XSLT. |
199 |
#Numbers may be bare but strings must be double quoted |
202 |
#Numbers may be bare but strings must be double quoted |
Lines 202-216
sub transform {
Link Here
|
202 |
|
205 |
|
203 |
#NOTE: Parameters are not cached. They are provided for |
206 |
#NOTE: Parameters are not cached. They are provided for |
204 |
#each different transform. |
207 |
#each different transform. |
205 |
my $result = $stsh->transform($source, %$parameters); |
208 |
my $transformed = $stsh->transform($source, %$parameters); |
206 |
$stsh->output_as_chars($result); |
209 |
$format eq 'bytes' |
|
|
210 |
? $stsh->output_as_bytes( $transformed ) |
211 |
: $format eq 'xmldoc' |
212 |
? $transformed |
213 |
: $stsh->output_as_chars( $transformed ); # default: chars |
207 |
}; |
214 |
}; |
208 |
if ($@) { |
215 |
if ($@) { |
209 |
$self->_set_error( 6, $@ ); |
216 |
$self->_set_error( 6, $@ ); |
210 |
return $retval; |
217 |
return $retval; |
211 |
} |
218 |
} |
212 |
$self->{last_xsltfile} = $key; |
219 |
$self->{last_xsltfile} = $key; |
213 |
return $str; |
220 |
return $result; |
214 |
} |
221 |
} |
215 |
|
222 |
|
216 |
=head2 refresh |
223 |
=head2 refresh |