XML::XSLT - A perl module for processing XSLT
use XML::XSLT; my $parser = XML::XSLT->new ($xslfile, $xslflag, warnings => "Active"); $parser->transform_document ($xmlfile, $xmlflag); $parser->print_result; $parser->dispose (); The variables $xmlfile and $xslfile are filenames, e.g. "filename", regular Perl filehandles, pass those with *FILEHANDLE, or Perl streams. After dispose, the parser object is destroyed. The result thus also!
# Alternative sources
The stylesheets and the documents may be passed as filenames, file handles regular strings, string references or DOM-trees. The source type is identified by the flags "FILE" (filename and -handle), "STRING" (string and string ref) and "DOM" (DOM-tree). For example:
my $parser = XML::XSLT->new ($xslstring, "STRING", warnings => "Active"); $parser->transform_document ($xmldom_rootnode, "DOM"); $parser->print_result;
# Alternatives for print_result()
Instead of printing to STDOUT, a filename or file can be passed to print_result():
$parser->print_result($outputfile); The variable $outputfile is a filename, e.g. "filename" or a regular Perl filehandle. Pass the latter with *FILEHANDLE.
Instead of printing at all, the result can be requested as a string or a DOM tree as well with:
$parser->result_string; $parser->result_tree;
# Reusable parser objects
This approach attaches one stylesheet to one parser object. The stylesheet can be applied to multiple documents and multiple parser objects can co-exist:
my $parser1 = XML::XSLT->new ($xslfile1, $xslflag1, warnings => "Active"); my $parser2 = XML::XSLT->new ($xslfile2, $xslflag2, warnings => "Active"); $parser1->transform_document ($xmlfile1, $xmlflag1); $parser1->print_result ($xmlout1); $parser1->transform_document ($xmlfile2, $xmlflag2); $parser1->print_result ($xmlout2); $parser2->transform_document ($xmlfile1, $xmlflag1); $parser2->print_result ($xmlout3); $parser2->transform_document ($xmlfile2, $xmlflag2); $parser2->print_result ($xmlout4);
This way the stylesheet-file does not have to be parsed all over again, but is reused each time.
This module implements the W3C's XSLT specification. The goal is full implementation of this spec, but it isn't yet. However, it already works well. Below is given the set of working xslt commands.
XML::XSLT makes use of XML::DOM and LWP::UserAgent, while XML::DOM uses XML::Parser. Therefore XML::Parser, XML::DOM and LWP::UserAgent have to be installed properly for XML::XSLT to run.
Copyright (c) 1999 Geert Josten & Egon Willighagen. All Rights Reserverd. This module is free software, and may be distributed under the same terms and conditions as Perl.
Not supported yet.
Attribute 'select' is supported to the same extent as xsl:value-of supports path selections.
Not supported yet: - attribute 'mode' - xsl:sort and xsl:with-param in content
Adds an attribute named to the value of the attribute 'name' and as value the stringified content-template.
Not supported yet: - attribute 'namespace'
Takes attribute 'name' which selects xsl:template's by name.
Not supported yet: - xsl:sort and xsl:with-param in content
Tests sequentially all xsl:whens until one succeeds or until an xsl:otherwise is found. Limited test support, see xsl:when
Supported.
Not supported yet: - attribute 'use-attribute-sets'
Attribute 'select' functions as well as with xsl:value-of
Not supported yet: - xsl:sort in content
Identical to xsl:when, but outside xsl:choose context.
Takes attribute href, which can be relative-local, absolute-local as well as an URL (preceded by identifier http:).
Synonym for xsl:variable (currently). See xsl:variable for support.
Not supported yet. Whitespace is always preserved.
Not supported yet. No whitespace is stripped.
Minor namespace support: other namespace than 'xsl:' for xsl-commands is allowed if xmlns-attribute is present. xmlns URL is verified. Other attributes are ignored.
Attribute 'name' and 'match' are supported to minor extend. ('name' must match exactly and 'match' must match with full path or no path)
Not supported yet: - attributes 'priority' and 'mode'
Not supported yet: - attribute 'disable-output-escaping'
Synonym for xsl:stylesheet
Inserts attribute or element values. Limited support:
<xsl:value-of select="."/>
<xsl:value-of select="/root-elem"/>
<xsl:value-of select="elem"/>
<xsl:value-of select="//elem"/>
<xsl:value-of select="elem[n]"/>
<xsl:value-of select="//elem[n]"/>
<xsl:value-of select="@attr"/>
<xsl:value-of select="text()"/>
<xsl:value-of select="processing-instruction()"/>
<xsl:value-of select="comment()"/>
and combinations of these;
Very limited. It should be possible to define a variable and use it with <xsl:value select="$varname" /> within the same template.
Only inside xsl:choose. Limited test support:
<xsl:when test="@attr='value'">
<xsl:when test="elem='value'">
<xsl:when test="path/[@attr='value']">
<xsl:when test="path/[elem='value']">
<xsl:when test="path">
path is supported to the same extend as with xsl:value-of
It is currently not functioning. (or is it?)
Support can be obtained from the XML::XSLT mailling list:
http://xmlxslt.listbot.com/
General information, like bugs and current functionality, can be found at the XML::XSLT homepage:
http://www.sci.kun.nl/sigma/Persoonlijk/egonw/xslt/
To install XML::XSLT, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::XSLT
CPAN shell
perl -MCPAN -e shell install XML::XSLT
For more information on module installation, please visit the detailed CPAN module installation guide.