Help language development. Donate to The Perl Foundation

LibXSLT cpan:WARRINGD last updated on 2019-11-02


Build Status


LibXSLT - Interface to the GNOME libxslt library


use LibXSLT;
use LibXML::Document;

# process a document with an internal '<?xml-stylesheet ..>' processing-instruction
my LibXML::Document $doc .= parse(location => 'foo.xml');
my Str $result = LibXSLT.process: :$doc;  

# provide an external style-sheet
my LibXML::Document $xsl .= parse(location => 'bar.xsl', :!cdata);
my Str $result = LibXSLT.process: :$doc, :$xsl;

# OO interface
use LibXSLT::Document;
use LibXSLT::Stylesheet;
my LibXSLT $xslt .= new();

my LibXSLT::Stylesheet $stylesheet;
$stylesheet = $xslt.parse-stylesheet($xsl);
# -OR-
# get the stylesheet from a document's '<?xml-stylesheet ..>' processing-instruction
$stylesheet .= load-stylesheet-pi(:$doc);

my LibXSLT::Document::Xslt $results = $stylesheet.transform(:$doc).Xslt;
say $results.Str;


This module is an interface to the GNOME project's libxslt.


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 LibXSLT class or object:

Input Callbacks

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

Security Callbacks

To create security preferences for the transformation see LibXSLT::Security. Once the security preferences have been defined you can apply them to an LibXSLT or 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 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.


Unlike the Perl 5 module, Raku automatically formats keys and parameters for xpath.

If you wish to emulate the Perl 5 behavour and/or format arguments yourself, pass :raw to the transform() method. You can use xpath-to-string() function to do the formatting:

use LibXSLT::Stylesheet :&xpath-to-string;
my %params = xpath-to-string(param => "string");
$stylesheet.transform($doc, :raw, |%params);

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 LibXSLT::Security

The interface for this module is similar to 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 LibXSLT or LibXSLT::Stylesheet object using security_callbacks().

my LibXSLT::Security $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 );

$ $security );
$ $security );

The registered callback functions are called when access to a resource is requested. If the access should be allowed the callback should return True, if not it should return False. 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.



This module requires the libxslt library to be installed. Please follow the instructions below based on your platform:

Debian Linux

sudo apt-get install libxslt-dev

Mac OS X

brew update
brew install libxslt




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

Copyright 2001-2009, Ltd.


This Raku module is based on the Perl 5 XML::LibXSLT module. The process() method has been adapted from the Perl 5 XML::LibXSL::Easy module.

With thanks to: Matt Sergeant, Shane Corgatelli, Petr Pal's, Shlomi Fish, יובל קוג'מן (Yuval Kogman)