[libxml-saxon-xslt2-perl] 05/18: Release versions 0.00{1,2}; latter contains useful improvements.

Jonas Smedegaard dr at jones.dk
Mon Aug 3 23:46:35 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.007
in repository libxml-saxon-xslt2-perl.

commit 3baf59b17fb01778c7a12afd6412afeccc94bea3
Author: Toby Inkster <mail at tobyinkster.co.uk>
Date:   Thu Jul 15 22:47:17 2010 +0000

    Release versions 0.00{1,2}; latter contains useful improvements.
    
    --HG--
    branch : XML-Saxon-XSLT2
    extra : convert_revision : svn%3A3fe8e991-6959-4966-b76d-b07eca2b6e37/XML-Saxon-XSLT2%40425
---
 Changes                      |   6 ++
 Changes.ttl                  |  15 +++-
 Changes.xml                  |  26 +++++++
 META.yml                     |   2 +-
 README                       |  32 ++++-----
 XML-Saxon-XSLT2-0.001.tar.gz | Bin 0 -> 31162 bytes
 XML-Saxon-XSLT2-0.002.tar.gz | Bin 0 -> 31677 bytes
 example1.pl                  |   2 +-
 example2.pl                  |   5 +-
 example2.xslt                |   1 +
 example3.pl                  |  16 +++++
 lib/XML/Saxon/XSLT2.pm       | 167 +++++++++++++++++++++++++++----------------
 t/01basic.t                  |  45 +++++++++++-
 13 files changed, 231 insertions(+), 86 deletions(-)

diff --git a/Changes b/Changes
index ca94640..e4fe031 100644
--- a/Changes
+++ b/Changes
@@ -10,5 +10,11 @@ Home page:    <http://search.cpan.org/dist/XML-Saxon-XSLT2/>
 Bug tracker:  <http://rt.cpan.org/Dist/Display.html?Queue=XML-Saxon-XSLT2>
 Maintainer:   Toby Inkster <mailto:tobyink at cpan.org>
 
+0.002 [2010-07-15]
+ - (Addition) Can specify a base URI in the constructor.
+ - (Addition) Can capture output from <xsl:message>.
+ - No longer default to 'xml' output method, but respect XSLT's specified
+   output method.
+
 0.001 [2010-07-15] # Original version
 
diff --git a/Changes.ttl b/Changes.ttl
index a5afa05..2325105 100644
--- a/Changes.ttl
+++ b/Changes.ttl
@@ -28,6 +28,19 @@ my:v_0-001
 	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/XML-Saxon-XSLT2-0.001.tar.gz> ;
 	rdfs:label      "Original version"@en .
 
+my:v_0-002
+
+	a               :Version ;
+	dc:issued       "2010-07-15"^^xsd:date ;
+	:revision       "0.002"^^xsd:string ;
+	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/XML-Saxon-XSLT2-0.002.tar.gz> ;
+	dcs:changeset [
+		dcs:versus my:v_0-001 ;
+		dcs:item [ rdfs:label "Can capture output from <xsl:message>."@en ; a dcs:Addition ] ;
+		dcs:item [ rdfs:label "Can specify a base URI in the constructor."@en ; a dcs:Addition ] ;
+		dcs:item [ rdfs:label "No longer default to 'xml' output method, but respect XSLT's specified output method."@en ]
+		] .
+
 #############################################################
 
 my:project
@@ -46,7 +59,7 @@ my:project
 	:tester         toby:i ;
 	:created        "2010-07-14"^^xsd:date ;
 	:license        <http://dev.perl.org/licenses/> ;
-	:release        my:v_0-001 .
+	:release        my:v_0-001 , my:v_0-002 .
 
 #############################################################
 
diff --git a/Changes.xml b/Changes.xml
index ed3979f..e4fbb4c 100644
--- a/Changes.xml
+++ b/Changes.xml
@@ -21,6 +21,31 @@
     <revision rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.001</revision>
     <rdfs:label xml:lang="en">Original version</rdfs:label>
   </Version>
+  <Version rdf:about="http://purl.org/NET/cpan-uri/dist/XML-Saxon-XSLT2/v_0-002">
+    <dcs:changeset>
+      <rdf:Description>
+        <dcs:item>
+          <dcs:Addition>
+            <rdfs:label xml:lang="en">Can capture output from <xsl:message>.</rdfs:label>
+          </dcs:Addition>
+        </dcs:item>
+        <dcs:item>
+          <dcs:Addition>
+            <rdfs:label xml:lang="en">Can specify a base URI in the constructor.</rdfs:label>
+          </dcs:Addition>
+        </dcs:item>
+        <dcs:item>
+          <rdf:Description>
+            <rdfs:label xml:lang="en">No longer default to 'xml' output method, but respect XSLT's specified output method.</rdfs:label>
+          </rdf:Description>
+        </dcs:item>
+        <dcs:versus rdf:resource="http://purl.org/NET/cpan-uri/dist/XML-Saxon-XSLT2/v_0-001"/>
+      </rdf:Description>
+    </dcs:changeset>
+    <dc:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2010-07-15</dc:issued>
+    <file-release rdf:resource="http://backpan.cpan.org/authors/id/T/TO/TOBYINK/XML-Saxon-XSLT2-0.002.tar.gz"/>
+    <revision rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.002</revision>
+  </Version>
   <Project rdf:about="http://purl.org/NET/cpan-uri/dist/XML-Saxon-XSLT2/project">
     <bug-database rdf:resource="http://rt.cpan.org/Dist/Display.html?Queue=XML-Saxon-XSLT2"/>
     <created rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2010-07-14</created>
@@ -33,6 +58,7 @@
     <name>XML-Saxon-XSLT2</name>
     <programming-language>Perl</programming-language>
     <release rdf:resource="http://purl.org/NET/cpan-uri/dist/XML-Saxon-XSLT2/v_0-001"/>
+    <release rdf:resource="http://purl.org/NET/cpan-uri/dist/XML-Saxon-XSLT2/v_0-002"/>
     <repository>
       <SVNRepository>
         <browse rdf:resource="http://goddamn.co.uk/viewvc/perlmods/XML-Saxon-XSLT2/"/>
diff --git a/META.yml b/META.yml
index 798375b..4eb471b 100644
--- a/META.yml
+++ b/META.yml
@@ -31,4 +31,4 @@ resources:
   homepage: http://search.cpan.org/dist/XML-Saxon-XSLT2/
   license: http://dev.perl.org/licenses/
   repository: http://goddamn.co.uk/viewvc/perlmods/XML-Saxon-XSLT2/
-version: 0.001
+version: 0.002
diff --git a/README b/README
index bd2c766..ffab54b 100644
--- a/README
+++ b/README
@@ -2,7 +2,7 @@ NAME
     XML::Saxon::XSLT2 - process XSLT 2.0 using Saxon 9.x.
 
 SYNOPSIS
-     my $trans  = XML::Saxon::XSLT2->new( $xslt );
+     my $trans  = XML::Saxon::XSLT2->new($xslt, $baseurl);
      my $output = $trans->transform($input);
 
 DESCRIPTION
@@ -17,9 +17,11 @@ DESCRIPTION
     save it to one of the two directories above.
 
   Constructor
-    "XML::Saxon::XSLT2->new($xslt)"
+    "XML::Saxon::XSLT2->new($xslt, [$baseurl])"
         Creates a new transformation. $xslt may be a string, a file handle
-        or an XML::LibXML::Document.
+        or an XML::LibXML::Document. $baseurl is an optional base URL for
+        resolving relative URL references in, for instance, <xsl:import>
+        links. Otherwise, the current directory is assumed to be the base.
 
   Methods
     "$trans->parameters($key=>$value, $key2=>$value2, ...)"
@@ -45,36 +47,32 @@ DESCRIPTION
         double, long (alias int, integer), decimal, bool (alias boolean),
         string, qname, uri, date, datetime. These are case-insensitive.
 
-    "$trans->transform($doc, $type)"
+    "$trans->transform($doc, [$output_method])"
         Run a transformation, returning the output as a string.
 
         $doc may be a string, a file handle or an XML::LibXML::Document.
 
-        $type may be 'xml', 'xhtml', 'html' or 'text' to set the output
-        mode. 'xml' is the default.
+        $output_method may be 'xml', 'xhtml', 'html' or 'text' to override
+        the XSLT output method.
 
-    "$trans->transform_document($doc, $type)"
+    "$trans->transform_document($doc, [$output_method])"
         Run a transformation, returning the output as an
         XML::LibXML::Document.
 
         $doc may be a string, a file handle or an XML::LibXML::Document.
 
-        $type may be 'xml', 'xhtml', 'html' or 'text' to set the output
-        mode. 'xml' is the default.
+        $output_method may be 'xml', 'xhtml', 'html' or 'text' to override
+        the XSLT output method.
 
         This method is slower than "transform".
 
+    "$trans->messages"
+        Returns a list of string representations of messages output by
+        <xsl:message> during the last transformation run.
+
 BUGS
     Please report any bugs to <http://rt.cpan.org/>.
 
-    Known limitations:
-
-    *   xsl:message
-
-        Saxon outputs messages written via <xsl:message> to STDERR. There
-        doesn't seem to be any way to capture and return these messages (not
-        even using Capture::Tiny or its ilk).
-
 SEE ALSO
     "XML::LibXSLT" is probably more reliable, and allows you to define your
     own XSLT extension functions. However, the libxslt library that it's
diff --git a/XML-Saxon-XSLT2-0.001.tar.gz b/XML-Saxon-XSLT2-0.001.tar.gz
new file mode 100644
index 0000000..8ed8efb
Binary files /dev/null and b/XML-Saxon-XSLT2-0.001.tar.gz differ
diff --git a/XML-Saxon-XSLT2-0.002.tar.gz b/XML-Saxon-XSLT2-0.002.tar.gz
new file mode 100644
index 0000000..21a20b9
Binary files /dev/null and b/XML-Saxon-XSLT2-0.002.tar.gz differ
diff --git a/example1.pl b/example1.pl
index 71c4610..c05e469 100644
--- a/example1.pl
+++ b/example1.pl
@@ -35,4 +35,4 @@ XSLT
 
 my $transformation = XML::Saxon::XSLT2->new($xslt);
 $transformation->parameters('bar' => [date=>'2010-02-28']);
-print $transformation->transform($xml) . "\n";
\ No newline at end of file
+print $transformation->transform($xml) . "\n";
diff --git a/example2.pl b/example2.pl
index b10bf03..1f19223 100644
--- a/example2.pl
+++ b/example2.pl
@@ -3,10 +3,13 @@
 use lib "lib";
 use XML::LibXML;
 use XML::Saxon::XSLT2;
+use Data::Dumper;
 
 open my $xslt, 'example2.xslt';
 my $xml = XML::LibXML->new->parse_file('example2.xml');
 
 my $transformation = XML::Saxon::XSLT2->new($xslt);
 $transformation->parameters('bar' => [date=>'2010-02-28']);
-print $transformation->transform_document($xml)->toString . "\n";
\ No newline at end of file
+print $transformation->transform_document($xml, 'xhtml')->toString . "\n";
+
+print Dumper([$transformation->messages]) . "\n";
\ No newline at end of file
diff --git a/example2.xslt b/example2.xslt
index c1296bd..7946b18 100644
--- a/example2.xslt
+++ b/example2.xslt
@@ -9,6 +9,7 @@
             <author id="{@author}"/>
         </xsl:for-each>
     </authors>
+    <xsl:message><foo>Hello world</foo></xsl:message>
 </xsl:variable>
 <xsl:copy-of select="$allauthors"/>
 </xsl:template>
diff --git a/example3.pl b/example3.pl
new file mode 100755
index 0000000..86e350e
--- /dev/null
+++ b/example3.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+
+use lib "lib";
+use Data::Dumper;
+use XML::LibXML;
+use XML::Saxon::XSLT2;
+use LWP::Simple qw[get];
+
+my $atom = get("http://intertwingly.net/blog/index.atom");
+
+my $transformation = XML::Saxon::XSLT2->new(
+	get('http://bblfish.net/work/atom-owl/2006-06-06/transform/atom2turtle.xsl'),
+	'http://bblfish.net/work/atom-owl/2006-06-06/transform/atom2turtle.xsl'
+	);
+print $transformation->transform_document($atom)->toString . "\n";
+
diff --git a/lib/XML/Saxon/XSLT2.pm b/lib/XML/Saxon/XSLT2.pm
index 693f10e..f7f7034 100644
--- a/lib/XML/Saxon/XSLT2.pm
+++ b/lib/XML/Saxon/XSLT2.pm
@@ -2,11 +2,13 @@ package XML::Saxon::XSLT2;
 
 use 5.008;
 use common::sense;
+
 use Carp;
-use Scalar::Util qw[blessed];
 use IO::Handle;
+use Scalar::Util qw[blessed];
+use XML::LibXML;
 
-our $VERSION = '0.001';
+our $VERSION = '0.002';
 my $classpath;
 
 BEGIN
@@ -21,50 +23,17 @@ use Inline Java => 'DATA', CLASSPATH=>$classpath;
 
 sub new
 {
-	my ($class, $xslt) = @_;
+	my ($class, $xslt, $baseurl) = @_;
 	$xslt = $class->_xml($xslt);
-	return bless { 'transformer' => XML::Saxon::XSLT2::Transformer->new($xslt) }, $class;
-}
-
-sub transform
-{
-	my ($self, $doc, $type) = @_;
-	$type ||= 'xml';
-	$doc = $self->_xml($doc);
-	return $self->{'transformer'}->transform($doc, $type);
-}
-
-sub transform_document
-{
-	my $self = shift;
-	my $r    = $self->transform(@_);
 	
-	$self->{'parser'} ||= XML::LibXML->new;
-	return $self->{'parser'}->parse_string($r);
-}
-
-sub _xml
-{
-	my ($proto, $xml) = @_;
-	
-	if (blessed($xml) && $xml->isa('XML::LibXML::Document'))
+	if ($baseurl)
 	{
-		return $xml->toString;
+		return bless { 'transformer' => XML::Saxon::XSLT2::Transformer->new($xslt, $baseurl) }, $class;
 	}
-	elsif (blessed($xml) && $xml->isa('IO::Handle'))
+	else
 	{
-		local $/;
-		my $str = <$xml>;
-		return $str;
+		return bless { 'transformer' => XML::Saxon::XSLT2::Transformer->new($xslt) }, $class;
 	}
-	elsif (ref $xml eq 'GLOB')
-	{
-		local $/;
-		my $str = <$xml>;
-		return $str;
-	}
-	
-	return $xml;
 }
 
 sub parameters
@@ -115,6 +84,53 @@ sub parameters
 	return $self;
 }
 
+sub transform
+{
+	my ($self, $doc, $type) = @_;
+	$type = ($type =~ /^(text|html|xhtml|xml)$/i) ? (lc $type) : '';
+	$doc  = $self->_xml($doc);
+	return $self->{'transformer'}->transform($doc, $type);
+}
+
+sub transform_document
+{
+	my $self = shift;
+	my $r    = $self->transform(@_);
+	
+	$self->{'parser'} ||= XML::LibXML->new;
+	return $self->{'parser'}->parse_string($r);
+}
+
+sub messages
+{
+	my $self = shift;
+	return @{ $self->{'transformer'}->messages };
+}
+
+sub _xml
+{
+	my ($proto, $xml) = @_;
+	
+	if (blessed($xml) && $xml->isa('XML::LibXML::Document'))
+	{
+		return $xml->toString;
+	}
+	elsif (blessed($xml) && $xml->isa('IO::Handle'))
+	{
+		local $/;
+		my $str = <$xml>;
+		return $str;
+	}
+	elsif (ref $xml eq 'GLOB')
+	{
+		local $/;
+		my $str = <$xml>;
+		return $str;
+	}
+	
+	return $xml;
+}
+
 1;
 
 =head1 NAME
@@ -123,7 +139,7 @@ XML::Saxon::XSLT2 - process XSLT 2.0 using Saxon 9.x.
 
 =head1 SYNOPSIS
 
- my $trans  = XML::Saxon::XSLT2->new( $xslt );
+ my $trans  = XML::Saxon::XSLT2->new($xslt, $baseurl);
  my $output = $trans->transform($input);
 
 =head1 DESCRIPTION
@@ -140,10 +156,12 @@ extract saxon9he.jar and save it to one of the two directories above.
 
 =over 4
 
-=item C<< XML::Saxon::XSLT2->new($xslt) >>
+=item C<< XML::Saxon::XSLT2->new($xslt, [$baseurl]) >>
 
 Creates a new transformation. $xslt may be a string, a file handle or an
-L<XML::LibXML::Document>.
+L<XML::LibXML::Document>. $baseurl is an optional base URL for resolving
+relative URL references in, for instance, E<lt>xsl:importE<gt> links.
+Otherwise, the current directory is assumed to be the base.
 
 =back
 
@@ -175,44 +193,37 @@ The following types are supported via the arrayref notation: float, double,
 long (alias int, integer), decimal, bool (alias boolean), string, qname, uri,
 date, datetime. These are case-insensitive.
 
-=item C<< $trans->transform($doc, $type) >>
+=item C<< $trans->transform($doc, [$output_method]) >>
 
 Run a transformation, returning the output as a string.
 
 $doc may be a string, a file handle or an L<XML::LibXML::Document>.
 
-$type may be 'xml', 'xhtml', 'html' or 'text' to set the output mode. 'xml' is
-the default.
+$output_method may be 'xml', 'xhtml', 'html' or 'text' to override
+the XSLT output method.
 
-=item C<< $trans->transform_document($doc, $type) >>
+=item C<< $trans->transform_document($doc, [$output_method]) >>
 
 Run a transformation, returning the output as an L<XML::LibXML::Document>.
 
 $doc may be a string, a file handle or an L<XML::LibXML::Document>.
 
-$type may be 'xml', 'xhtml', 'html' or 'text' to set the output mode. 'xml' is
-the default.
+$output_method may be 'xml', 'xhtml', 'html' or 'text' to override
+the XSLT output method.
 
 This method is slower than C<transform>.
 
+=item C<< $trans->messages >>
+
+Returns a list of string representations of messages output by
+E<lt>xsl:messageE<gt> during the last transformation run.
+
 =back
 
 =head1 BUGS
 
 Please report any bugs to L<http://rt.cpan.org/>.
 
-Known limitations:
-
-=over 4
-
-=item * B<xsl:message>
-
-Saxon outputs messages written via E<lt>xsl:messageE<gt> to STDERR. There
-doesn't seem to be any way to capture and return these messages (not even using
-L<Capture::Tiny> or its ilk).
-
-=back
-
 =head1 SEE ALSO
 
 C<XML::LibXSLT> is probably more reliable, and allows you to define your
@@ -264,6 +275,7 @@ public class Transformer
 	private XsltExecutable xslt;
 	private Processor proc;
 	private HashMap<String, XdmAtomicValue> params;
+	public List messagelist;
 
 	public Transformer (String stylesheet)
 		throws SaxonApiException
@@ -272,6 +284,17 @@ public class Transformer
 		XsltCompiler comp = proc.newXsltCompiler();
 		xslt = comp.compile(new StreamSource(new StringReader(stylesheet)));
 		params = new HashMap<String, XdmAtomicValue>();
+		messagelist = new ArrayList();
+	}
+	
+	public Transformer (String stylesheet, String baseuri)
+		throws SaxonApiException
+	{
+		proc = new Processor(false);
+		XsltCompiler comp = proc.newXsltCompiler();
+		xslt = comp.compile(new StreamSource(new StringReader(stylesheet), baseuri));
+		params = new HashMap<String, XdmAtomicValue>();
+		messagelist = new ArrayList();
 	}
 	
 	public void paramAddString (String key, String value)
@@ -345,6 +368,11 @@ public class Transformer
 	{
 		params.clear();
 	}
+	
+	public Object[] messages ()
+	{
+		return messagelist.toArray();
+	}
 
 	public String transform (String doc, String method)
 		throws SaxonApiException
@@ -357,11 +385,15 @@ public class Transformer
 		trans.setInitialContextNode(source);
 
 		Serializer out = new Serializer();
-		out.setOutputProperty(Serializer.Property.METHOD, method);
 		StringWriter sw = new StringWriter();
 		out.setOutputWriter(sw);
 		trans.setDestination(out);
-		
+
+		if (method != "")
+		{
+			out.setOutputProperty(Serializer.Property.METHOD, method);
+		}
+
 		Iterator i = params.keySet().iterator();
 		while (i.hasNext())
 		{
@@ -371,6 +403,15 @@ public class Transformer
 			trans.setParameter(new QName(k.toString()), v);
 		}
 
+		messagelist.clear();
+		trans.setMessageListener(
+			new MessageListener() {
+				public void message(XdmNode content, boolean terminate, SourceLocator locator) {
+					messagelist.add(content.toString());
+				}
+			}
+		);
+
 		trans.transform();
 		
 		return sw.toString();
diff --git a/t/01basic.t b/t/01basic.t
index efd5560..f511172 100644
--- a/t/01basic.t
+++ b/t/01basic.t
@@ -1,3 +1,44 @@
-use Test::More tests => 1;
-BEGIN { use_ok('Example::Example') };
+use Test::More tests => 5;
+BEGIN { use_ok('XML::Saxon::XSLT2') };
 
+my $xslt = <<'XSLT';
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
+<xsl:param name="bar"/>
+<xsl:template match="/">
+<xsl:variable name="allauthors">
+    <authors foo="{$bar}">
+        <xsl:for-each select="/books/book">
+				<xsl:sort select="@author"/>
+            <author id="{@author}"/>
+        </xsl:for-each>
+    </authors>
+</xsl:variable>
+<xsl:copy-of select="$allauthors"/>
+</xsl:template>
+</xsl:stylesheet>
+XSLT
+
+my $input = <<'XML';
+<?xml version="1.0" encoding="UTF-8"?>
+<books>
+    <book name="Programming Ruby"
+      author="Dave Thomas"/>
+    <book name="Code Generation in Action"
+      author="Jack Herrington"/>
+    <book name="Pragmatic Programmer"
+      author="Dave Thomas"/>
+</books>
+XML
+
+my $transformation = XML::Saxon::XSLT2->new($xslt);
+$transformation->parameters('bar' => [date=>'2010-02-28']);
+my $output = $transformation->transform_document($input, 'xml');
+
+is($output->documentElement->getAttribute('foo'), '2010-02-28');
+
+my @authors = $output->getElementsByTagName('author');
+
+is($authors[0]->getAttribute('id'), 'Dave Thomas');
+is($authors[1]->getAttribute('id'), 'Dave Thomas');
+is($authors[2]->getAttribute('id'), 'Jack Herrington');

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libxml-saxon-xslt2-perl.git



More information about the Pkg-perl-cvs-commits mailing list