Know-How für Ihr Projekt

Perl Documentation


XML::LibXSLT - Interface to the GNOME libxslt library


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);


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.


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.


The following methods are available on the new XML::LibXSLT object:

Input Callbacks

To define XML::LibXSLT or XML::LibXSLT::Stylesheet specific input callbacks, reuse the XML::LibXML input callback API as described in XML::LibXML::InputCallback(3).

Security Callbacks

To create security preferences for the transformation see 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 security_callbacks() method.


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.


LibXSLT expects parameters in XPath format. That is, if you wish to pass a string to the XSLT engine, you actually have to pass it as a quoted string:

$stylesheet->transform($doc, param => "'string'");

Note the quotes within quotes there!

Obviously this isn't much fun, so you can make it easy on yourself:

$stylesheet->transform($doc, XML::LibXSLT::xpath_to_string(
      param => "string"

The utility function does the right thing with respect to strings in XPath, including when you have quotes already embedded within your string.


Provides an interface to the libxslt security framework by allowing callbacks to be defined that can restrict access to various resources (files or URLs) during a transformation.

The libxslt security framework allows callbacks to be defined for certain actions that a stylesheet may attempt during a transformation. It may be desirable to restrict some of these actions (for example, writing a new file using exsl:document). The actions that may be restricted are:

Using XML::LibXSLT::Security

The interface for this module is similar to XML::LibXML::InputCallback. After creating a new instance you may register callbacks for each of the security options listed above. Then you apply the security preferences to the XML::LibXSLT or XML::LibXSLT::Stylesheet object using security_callbacks().

my $security = XML::LibXSLT::Security->new();
$security->register_callback( read_file  => $read_cb );
$security->register_callback( write_file => $write_cb );
$security->register_callback( create_dir => $create_cb );
$security->register_callback( read_net   => $read_net_cb );
$security->register_callback( write_net  => $write_net_cb );
$xslt->security_callbacks( $security );
$stylesheet->security_callbacks( $security );

The registered callback functions are called when access to a resource is requested. If the access should be allowed the callback should return 1, if not it should return 0. The callback functions should accept the following arguments:

If a particular option (except for create_dir) doesn't have a registered callback, then the stylesheet will have full access for that action.



Included in the distribution is a simple benchmark script, which has two drivers - one for LibXSLT and one for Sablotron. The benchmark requires the testcases files from the XSLTMark distribution which you can find at

Put the testcases directory in the directory created by this distribution, and then run:

perl -h

to get a list of options.

The benchmark requires XML::XPath at the moment, but I hope to factor that out of the equation fairly soon. It also requires Time::HiRes, which I could be persuaded to factor out, replacing it with, but I haven't done so yet.

I would love to get drivers for XML::XSLT and XML::Transformiix, if you would like to contribute them. Also if you get this running on Win32, I'd love to get a driver for MSXSLT via OLE, to see what we can do against those Redmond boys!


For debugging purposes, XML::LibXSLT provides version information about the libxslt C library (but do not confuse it with the version number of XML::LibXSLT module itself, i.e. with $XML::LibXSLT::VERSION). XML::LibXSLT issues a warning if the runtime version of the library is less then the compile-time version.


This is free software, you may use it and distribute it under the same terms as Perl itself.

Copyright 2001-2009, Ltd.


Matt Sergeant,

Security callbacks implementation contributed by Shane Corgatelli.


Petr Pajas ,


Please report bugs via