The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


XML::LibXSLT - Interface to the gnome libxslt library


  use XML::LibXSLT;
  use XML::LibXML;
  my $parser = XML::LibXML->new();
  my $xslt = XML::LibXSLT->new();
  my $source = $parser->parse_file('foo.xml');
  my $style_doc = $parser->parse_file('bar.xsl');
  my $stylesheet = $xslt->parse_stylesheet($style_doc);
  my $results = $stylesheet->transform($source);
  print $stylesheet->output_string($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.



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.



Sets a callback to be used for debug messages. If you don't set this, debug messages will be ignored.


  XML::LibXSLT->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 });

Will register a bar function in the urn:foo namespace (which you have to define in your XSLT using xmlns:...) that will return the current date and time as a string:

  <xsl:stylesheet version="1.0"
  <xsl:template match="/">
    The time is: <xsl:value-of select="foo:bar()"/>

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 XML::LibXML::Literal, XML::LibXML::Number and XML::LibXML::Number values respectively then set the variable $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).


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


$doc here is an XML::LibXML::Document object (see 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.


Exactly the same as the above, but parses the given filename directly.

Input Callbacks

To define XML::LibXSLT specific input callbacks, reuse the XML::LibXML input callback API as described in "Input Callbacks" in XML::LibXML::Document(3).


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.

transform(doc, %params)

  my $results = $stylesheet->transform($doc, foo => "value);

Transforms the passed in XML::LibXML::Document object, and returns a new XML::LibXML::Document. Extra hash entries are used as parameters.

transform_file(filename, %params)

  my $results = $stylesheet->transform_file($filename, bar => "value");


Returns a scalar that is the XSLT rendering of the XML::LibXML::Document object using the desired output format (specified in the xsl:output tag in the stylesheet). Note that you can also call $result->toString, but that will *always* output the document in XML format, and in UTF8, which may not be what you asked for in the xsl:output tag.

output_fh(result, fh)

Outputs the result to the filehandle given in $fh.

output_file(result, filename)

Outputs the result to the file named in $filename.


Returns the output encoding of the results. Defaults to "UTF-8".


Returns the output media_type of the results. Defaults to "text/html".


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.


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!


Matt Sergeant,

Copyright 2001, Ltd. All rights reserved.