The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


XML::STX - a pure Perl STX processor


 use XML::STX;

 $stx = XML::STX->new();

 $transformer = $stx->new_transformer($stylesheet_uri);


XML::STX is a pure Perl implementation of STX processor. Streaming Transformations for XML (STX) is a one-pass transformation language for XML documents that builds on the Simple API for XML (SAX). See for more details.

Dependencies: XML::SAX, XML::NamespaceSupport and Clone.

The current version is unstable.


Shortcut TrAX-like API

Thanks to various shortcuts of the TrAX-like API, this is the simplest way to run transformations. This can be what you want if you are happy with just one transformation context per stylesheet, and your input data is in files. Otherwise, you may want to use some more features of this API (see Full TrAX-like API).

 use XML::STX;

 $stx = XML::STX->new();

 $transformer = $stx->new_transformer($stylesheet_uri);

Full TrAX-like API

This is the regular interface to XML::STX allowing to run independent transformations for single template, bind external parameters, and associate drivers/handlers with input/output channels.

See TrAX-like API Reference for more details.
 use XML::STX;

 $stx = XML::STX->new();

 $stylesheet = $stx->new_source($stylesheet_uri);
 $templates = $stx->new_templates($stylesheet);
 $transformer = $templates->new_transformer();

 $transformer->{Parameters} = {par1 => 5, par2 => 'foo'}';

 $source = $stx->new_source($source_uri);
 $result = $stx->new_result();

 $transformer->transform($source, $result);

SAX Filter

 use XML::STX;
 use SAX2Parser;
 use SAX2Handler;

 $stx = XML::STX->new();
 $stx_parser = XML::STX::Parser->new();
 $xml_parser1 = SAX2Parser->new(Handler => $stx_parser);
 $stylesheet =  $xml_parser1->parse_uri($templ_uri);

 $writer = XML::SAX::Writer->new();
 $stx = XML::STX->new(Handler => $writer, Sheet => $stylesheet );
 $xml_parser2 = SAX2Parser->new(Handler => $stx);

Legacy API (deprecated)

 use XML::STX;

 $stx = XML::STX->new();
 $parser_t = SAX2Parser->new();
 $stylesheet = $stx->get_stylesheet($parser_t, $templ_uri);

 $parser = SAX2Parser->new();
 $handler = SAX2Handler->new();
 $stx->transform($stylesheet, $parser, $data_uri, $handler);

Command-line Interface

XML::STX is shipped with script allowing to run STX transformations from the command line.

Usage: [OPTIONS] <stylesheet> <data> [PARAMS]

Run -h for more details.


Petr Cimprich (Ginger Alliance),


XML::SAX, XML::NamespaceSupport, perl(1).