XML::S...::XSLT2(3pm)

πŸ“¦
version 0.010 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

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. $xslt can be a string, a file handle, or an XML::LibXML::Document (so many options, we love a flexible queen). $baseurl is 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 DateTime object β†’ treated as an xs:dateTime
  • a URI object β†’ 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. $doc may be a string, a file handle, or an XML::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 an XML::LibXML::Document instead.
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 $default back.
  • $trans->doctype_public($default) β€” returns the output DOCTYPE public identifier for the transformation. if the transformation doesn't specify a doctype, you get $default back.
  • $trans->doctype_system($default) β€” returns the output DOCTYPE system identifier for the transformation. if the transformation doesn't specify a doctype, you get $default back.
  • $trans->version($default) β€” returns the output XML version for the transformation. if the transformation doesn't specify a version, you get $default back.
  • $trans->encoding($default) β€” returns the output encoding for the transformation. if the transformation doesn't specify an encoding, you get $default back.

πŸ’€ 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 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