XML-LibXSLT
view release on metacpan or search on metacpan
lib/XML/LibXSLT.pm view on Meta::CPAN
if ( ref $callback eq 'CODE' )
{
$self->{_CALLBACKS}{ $OPTION_MAP{$option} } = $callback;
}
else
{
croak
"Invalid argument. The callback must be a reference to a subroutine";
}
}
# Removes the callback for the given security option. Causes the given option
# to use the default security handler (which always allows the action).
sub unregister_callback
{
my $self = shift;
my $option = shift;
unless ( exists $OPTION_MAP{$option} )
{
croak "Invalid security option '$option'. Must be one of: "
. join( ', ', keys %OPTION_MAP ) . ".";
}
delete $self->{_CALLBACKS}{ $OPTION_MAP{$option} };
}
# make it so libxslt can use the callbacks
sub init_callbacks
{
my $self = shift;
%_GLOBAL_CALLBACKS = %{ $self->{_CALLBACKS} };
}
# reset libxslt callbacks
sub cleanup_callbacks
{
my $self = shift;
%_GLOBAL_CALLBACKS = ();
}
1;
__END__
=head1 NAME
XML::LibXSLT - Interface to the GNOME libxslt library
=head1 SYNOPSIS
use XML::LibXSLT;
use XML::LibXML;
my $xslt = XML::LibXSLT->new();
my $source = XML::LibXML->load_xml(location => 'foo.xml');
my $style_doc = XML::LibXML->load_xml(location=>'bar.xsl', no_cdata=>1);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source);
print $stylesheet->output_as_bytes($results);
=head1 DESCRIPTION
This module is an interface to the GNOME project's libxslt. This is an
extremely good XSLT engine, highly compliant and also very fast. I have
tests showing this to be more than twice as fast as Sablotron.
=head1 OPTIONS
XML::LibXSLT has some global options. Note that these are probably not
thread or even fork safe - so only set them once per process. Each one
of these options can be called either as class methods, or as instance
methods. However either way you call them, it still sets global options.
Each of the option methods returns its previous value, and can be called
without a parameter to retrieve the current value.
=over
=item max_depth
XML::LibXSLT->max_depth(1000);
This option sets the maximum recursion depth for a stylesheet. See the
very end of section 5.4 of the XSLT specification for more details on
recursion and detecting it. If your stylesheet or XML file requires
seriously deep recursion, this is the way to set it. Default value is
250.
=item max_vars
XML::LibXSLT->max_vars(100_000);
This option sets the maximum number of variables for a stylesheet. If your
stylesheet or XML file requires many variables, this is the way to increase
their limit. Default value is system-specific and may vary.
=item debug_callback
XML::LibXSLT->debug_callback($subref);
Sets a callback to be used for debug messages. If you don't set this,
debug messages will be ignored.
=item register_function
XML::LibXSLT->register_function($uri, $name, $subref);
$stylesheet->register_function($uri, $name, $subref);
Registers an XSLT extension function mapped to the given URI. For example:
XML::LibXSLT->register_function("urn:foo", "bar",
sub { scalar localtime });
lib/XML/LibXSLT.pm view on Meta::CPAN
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="urn:foo">
<xsl:template match="/">
The time is: <xsl:value-of select="foo:bar()"/>
</xsl:template>
</xsl:stylesheet>
Parameters can be in whatever format you like. If you pass in a nodelist
it will be a XML::LibXML::NodeList object in your perl code, but ordinary
values (strings, numbers and booleans) will be ordinary perl scalars. If
you wish them to be C<XML::LibXML::Literal>, C<XML::LibXML::Number> and
C<XML::LibXML::Number> values respectively then set the variable
C<$XML::LibXSLT::USE_LIBXML_DATA_TYPES> to a true value. Return values can
be a nodelist or a plain value - the code will just do the right thing.
But only a single return value is supported (a list is not converted to
a nodelist).
=item register_element
$stylesheet->register_element($uri, $name, $subref)
Registers an XSLT extension element $name mapped to the given URI. For example:
$stylesheet->register_element("urn:foo", "hello", sub {
my $name = $_[2]->getAttribute( "name" );
return XML::LibXML::Text->new( "Hello, $name!" );
});
Will register a C<hello> element in the C<urn:foo> namespace that returns a "Hello, X!" text node. You must define this namespace in your XSLT and include its prefix in the C<extension-element-prefixes> list:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="urn:foo"
extension-element-prefixes="foo">
<xsl:template match="/">
<foo:hello name="bob"/>
</xsl:template>
</xsl:stylesheet>
The callback is passed the input document node as $_[1] and the stylesheet node as $_[2]. $_[0] is reserved for future use.
=back
=head1 API
The following methods are available on the new XML::LibXSLT object:
=over
=item parse_stylesheet($stylesheet_doc)
C<$stylesheet_doc> here is an XML::LibXML::Document object (see L<XML::LibXML>)
representing an XSLT file. This method will return a
XML::LibXSLT::Stylesheet object, or undef on failure. If the XSLT is
invalid, an exception will be thrown, so wrap the call to
parse_stylesheet in an eval{} block to trap this.
IMPORTANT: C<$stylesheet_doc> should not contain CDATA sections,
otherwise libxslt may misbehave. The best way to assure this is to
load the stylesheet with no_cdata flag, e.g.
my $stylesheet_doc = XML::LibXML->load_xml(location=>"some.xsl", no_cdata=>1);
=item parse_stylesheet_file($filename)
Exactly the same as the above, but parses the given filename directly.
=back
=head1 Input Callbacks
To define XML::LibXSLT or XML::LibXSLT::Stylesheet specific input
callbacks, reuse the XML::LibXML input callback API as described in
L<XML::LibXML::InputCallback(3)>.
=over 4
=item input_callbacks($icb)
Enable the callbacks in C<$icb> only for this XML::LibXSLT object.
C<$icb> should be a C<XML::LibXML::InputCallback> object. This will
call C<init_callbacks> and C<cleanup_callbacks> automatically during
parsing or transformation.
=back
=head1 Security Callbacks
To create security preferences for the transformation see
L<XML::LibXSLT::Security>. Once the security preferences have been defined you
can apply them to an XML::LibXSLT or XML::LibXSLT::Stylesheet instance using
the C<security_callbacks()> method.
=head1 XML::LibXSLT::Stylesheet
The main API is on the stylesheet, though it is fairly minimal.
One of the main advantages of XML::LibXSLT is that you have a generic
stylesheet object which you call the transform() method passing in a
document to transform. This allows you to have multiple transformations
happen with one stylesheet without requiring a reparse.
=over
=item transform(doc, %params)
my $results = $stylesheet->transform($doc, foo => "'bar'");
print $stylesheet->output_as_bytes($results);
Transforms the passed in XML::LibXML::Document object, and returns a
new XML::LibXML::Document. Extra hash entries are used as parameters.
Be sure to keep in mind the caveat with regard to quotes explained in
the section on L</"Parameters"> below.
=item transform_file(filename, %params)
my $results = $stylesheet->transform_file($filename, bar => "'baz'");
Note the string parameter caveat, detailed in the section on
L</"Parameters"> below.
=item output_as_bytes(result)
( run in 0.817 second using v1.01-cache-2.11-cpan-39bf76dae61 )