XML::S...::XSLT2(3pm)
okay bestie, pull up a chair π«Ά this is the manual page for XML::S...::XSLT2(3pm) β yes it's User Contributed Perl Documentation, yes it's giving vintage CPAN energy, and yes we are going to make XSLT 2.0 pop off in Perl. let's go π
β¨ NAME β¨
XML::Saxon::XSLT2 β process XSLT 2.0 using Saxon 9.x. that's it, that's the whole job, and it slays π₯
π SYNOPSIS π
use XML::Saxon::XSLT2;
# make sure to open filehandle in right encoding
open(my $input, '<:encoding(UTF-8)', 'path/to/xml') or die $!;
open(my $xslt, '<:encoding(UTF-8)', 'path/to/xslt') or die $!;
my $trans = XML::Saxon::XSLT2->new($xslt, $baseurl);
my $output = $trans->transform($input);
print $output;
my $output2 = $trans->transform_document($input);
my @paragraphs = $output2->getElementsByTagName('p');
π DESCRIPTION π
so this module does XSLT 1.0 and 2.0 using Saxon 9.x, and it gets there via Inline::Java. iconic behavior honestly β¨
it expects Saxon to already be chilling in one of these two spots:
/usr/share/java/saxon9he.jar/usr/local/share/java/saxon9he.jar
future versions are supposed to be more flexible about this, but for now, that's the vibe. the saxon9he.jar file lives at <http://saxon.sourceforge.net/> β you just download the latest Java release of Saxon-HE 9.x, open the Zip archive, extract saxon9he.jar, and save it into one of those two directories above. boom, done, periodt π
π Import π
use XML::Saxon::XSLT2;
you can also throw in extra parameters and they'll be passed straight on through to Inline::Java, like this:
use XML::Saxon::XSLT2 EXTRA_JAVA_ARGS => '-Xmx256m';
β οΈ okay important one: the import function must be called. if you load this module without importing it, it will not work. not "might be weird", not "runs slow" β it will not work. π but don't panic, the good news is it won't pollute your namespace. bestie behavior fr fr.
π¦ Constructor π¦
XML::Saxon::XSLT2->new($xslt, [$baseurl])β creates a brand new transformation.$xsltcan be a string, a file handle, or anXML::LibXML::Document(so many options, we love a flexible queen).$baseurlis an optional base URL used for resolving relative URL references β think<xsl:import>links and such. if you don't hand one over, the current directory is assumed to be the base. (heads up: for base URIs that are filesystem directories, remember to include the trailing slash β she notices these things π )
π Methods π
$trans->parameters($key=>$value, $key2=>$value2, ...)β sets transformation parameters before you actually run the transformation.
every key is a parameter name.
every value is that parameter's value. and it can be a bunch of things:
- a plain scalar β treated as an
xs:string - a
DateTimeobject β treated as anxs:dateTime - a
URIobject βxs:anyURI - a
Math::BigIntβxs:long - or an arrayref, where the first element is the type and the second is the value
like so:
$trans->parameters(
now => DateTime->now,
madrid_is_capital_of_spain => [ boolean => 1 ],
price_of_fish => [ decimal => '1.99' ],
my_link => URI->new('http://example.com/'),
your_link => [ uri => 'http://example.net/' ],
);
and these are all the types you can use with that arrayref notation: float, double, long (with the aliases int and integer), decimal, bool (alias boolean), string, qname, uri, date, datetime. they're case-insensitive too, so go off π
$trans->transform($doc, [$output_method])β runs a transformation and hands you back the output as a string.$docmay be a string, a file handle, or anXML::LibXML::Document.
$output_method can be 'xml', 'xhtml', 'html', or 'text' to override the XSLT output method β or 'default' to just use whatever output method is specified in the XSLT file. 'default' is the default. and in the current release, 'default' is broken. π :-(
$trans->transform_document($doc, [$output_method])β same as<transform>, but it returns the output as anXML::LibXML::Documentinstead.
heads up: this method is slower than transform. π
$trans->messagesβ returns a list of string representations of the messages that<xsl:message>spat out during the last transformation run. a little recap of the drama π$trans->media_type($default)β returns the output media type for the transformation. if the transformation doesn't specify an output type, you get$defaultback.$trans->doctype_public($default)β returns the output DOCTYPE public identifier for the transformation. if the transformation doesn't specify a doctype, you get$defaultback.$trans->doctype_system($default)β returns the output DOCTYPE system identifier for the transformation. if the transformation doesn't specify a doctype, you get$defaultback.$trans->version($default)β returns the output XML version for the transformation. if the transformation doesn't specify a version, you get$defaultback.$trans->encoding($default)β returns the output encoding for the transformation. if the transformation doesn't specify an encoding, you get$defaultback.
π BUGS π
please report any bugs to <http://rt.cpan.org/>. we listen, we fix, we grow π±
π SEE ALSO π
XML::LibXSLT is probably more reliable when it comes to easy installation on a variety of platforms, and it also lets you define your own XSLT extension functions. however β the libxslt library that it's based on only supports XSLT 1.0. so, y'know, tradeoffs. π
this module uses Inline::Java.
<http://saxon.sourceforge.net/>.
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
π COPYRIGHT π
Copyright 2010-2012, 2014 Toby Inkster
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole thing bestie, you're officially caught up π«Ά this was XML::Saxon::XSLT2(3pm) β perl v5.32.1, dated 2021-09-11 β mwah, go transform something gorgeous β¨π
π¦ where this page comes from
- debian bookworm: libxml-saxon-xslt2-perl 0.010-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libxml-saxon-xslt2-perl 0.010-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-saxon-xslt2-perl 0.010-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms