XML::LibXSLT - Interface to the gnome libxslt library
use XML::LibXSLT; use XML::LibXML;
my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new();
my $source = $parser->parse_file('foo.xml'); my $style_doc = $parser->parse_file('bar.xsl');
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source);
print $stylesheet->output_string($results);
Each of the option methods returns its previous value, and can be called without a parameter to retrieve the current value.
XML::LibXSLT->max_depth(1000);
This option sets the maximum recursion depth for a stylesheet. See the very end of section 5.4 of the XSLT specification for more details on recursion and detecting it. If your stylesheet or XML file requires seriously deep recursion, this is the way to set it. Default value is 250.
XML::LibXSLT->debug_callback($subref);
Sets a callback to be used for debug messages. If you don't set this, debug messages will be ignored.
XML::LibXSLT->register_function($uri, $name, $subref);
Registers an XSLT extension function mapped to the given URI. For example:
XML::LibXSLT->register_function("urn:foo", "bar", sub { scalar localtime });
Will register a "bar" function in the "urn:foo" namespace (which you have to define in your XSLT using "xmlns:...") that will return the current date and time as a string:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="urn:foo"> <xsl:template match="/"> The time is: <xsl:value-of select="foo:bar()"/> </xsl:template> </xsl:stylesheet>
Parameters can be in whatever format you like. If you pass in a nodelist it will be a XML::LibXML::NodeList object in your perl code, but ordinary values (strings, numbers and booleans) will be ordinary perl scalars. If you wish them to be "XML::LibXML::Literal", "XML::LibXML::Number" and "XML::LibXML::Number" values respectively then set the variable $XML::LibXSLT::USE_LIBXML_DATA_TYPES to a true value. Return values can be a nodelist or a plain value - the code will just do the right thing. But only a single return value is supported (a list is not converted to a nodelist).
One of the main advantages of XML::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.
my $results = $stylesheet->transform($doc, foo => "value);
Transforms the passed in XML::LibXML::Document object, and returns a new XML::LibXML::Document. Extra hash entries are used as parameters.
my $results = $stylesheet->transform_file($filename, bar => "value");
$stylesheet->transform($doc, param => "'string'");
Note the quotes within quotes there!
Obviously this isn't much fun, so you can make it easy on yourself:
$stylesheet->transform($doc, XML::LibXSLT::xpath_to_string( param => "string" ));
The utility function does the right thing with respect to strings in XPath, including when you have quotes already embedded within your string.
Put the testcases directory in the directory created by this distribution, and then run:
perl benchmark.pl -h
to get a list of options.
The benchmark requires XML::XPath at the moment, but I hope to factor that out of the equation fairly soon. It also requires Time::HiRes, which I could be persuaded to factor out, replacing it with Benchmark.pm, but I haven't done so yet.
I would love to get drivers for XML::XSLT and XML::Transformiix, if you would like to contribute them. Also if you get this running on Win32, I'd love to get a driver for MSXSLT via OLE, to see what we can do against those Redmond boys!
Copyright 2001, AxKit.com Ltd. All rights reserved.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |