Bugzilla – Attachment 35566 Details for
Bug 13262
Add parameters to XSLT Handler transform method
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 13262 - Add parameters to XSLT Handler transform method
Bug-13262---Add-parameters-to-XSLT-Handler-transfo.patch (text/plain), 5.69 KB, created by
David Cook
on 2015-01-27 23:40:30 UTC
(
hide
)
Description:
Bug 13262 - Add parameters to XSLT Handler transform method
Filename:
MIME Type:
Creator:
David Cook
Created:
2015-01-27 23:40:30 UTC
Size:
5.69 KB
patch
obsolete
>From 1cce56c8a6fdc32855775e17a08e86c8c3c91161 Mon Sep 17 00:00:00 2001 >From: David Cook <dcook@prosentient.com.au> >Date: Mon, 17 Nov 2014 13:59:49 +1100 >Subject: [PATCH] Bug 13262 - Add parameters to XSLT Handler transform method > >This patch adds an optional hashref argument to the XSLT_Handler >transform() method. It allows you to send key => value pairs >parameters to the XML::LibXSLT object, which you can reference >in a XSLT via <xsl:param name="XXX" />. > >The parameter value is evaluated as an XPath query, so you can only >pass quoted strings (i.e. "'test'") or numbers. Otherwise, the >XSLT engine will interpret it as a Xpath query and will run it >on the XML that you're transforming. > >The most common use case is sending strings to a XSLT. In my case, >this is an OAI-PMH identifier that comes in a OAI response but not >the actual metadata. See the following link from the official POD: >http://search.cpan.org/~shlomif/XML-LibXSLT-1.92/LibXSLT.pm#Parameters > >_TEST PLAN_ > >1) Run "perl t/db_dependent/XSLT_Handler.t". If all tests pass, >you should be free to sign off. Feel free to inspect the last >test in XSLT_Handler.t and the XSL in test04.xsl to see how it >works. > >If you really want to be thorough, you could write your own test >cases using mine as an example. > >Alternatively, you could go into C4::XSLT, and try to pass a >value to a parameter in the search results or the detail page, >but that might be a bit over the top. > >It's a pretty simple patch. >--- > Koha/XSLT_Handler.pm | 16 +++++++++++++--- > t/db_dependent/XSLT_Handler.t | 25 ++++++++++++++++++++++++- > t/db_dependent/XSLT_Handler/test04.xsl | 18 ++++++++++++++++++ > 3 files changed, 55 insertions(+), 4 deletions(-) > create mode 100644 t/db_dependent/XSLT_Handler/test04.xsl > >diff --git a/Koha/XSLT_Handler.pm b/Koha/XSLT_Handler.pm >index 0779f2f..f4e9411 100644 >--- a/Koha/XSLT_Handler.pm >+++ b/Koha/XSLT_Handler.pm >@@ -132,8 +132,8 @@ __PACKAGE__->mk_accessors(qw( do_not_return_source print_warns )); > > my $output= $xslt_engine->transform( $xml, $xsltfilename ); > #Alternatively: >- #$output = $xslt_engine->transform({ xml => $xml, file => $file }); >- #$output = $xslt_engine->transform({ xml => $xml, code => $code }); >+ #$output = $xslt_engine->transform({ xml => $xml, file => $file, [parameters => $parameters] }); >+ #$output = $xslt_engine->transform({ xml => $xml, code => $code, [parameters => $parameters] }); > if( $xslt_engine->err ) { > #decide what to do on failure.. > } >@@ -156,10 +156,12 @@ sub transform { > # old style: $xml, $filename > # new style: $hashref > my ( $xml, $filename, $xsltcode ); >+ my $parameters = {}; > if( ref $_[0] eq 'HASH' ) { > $xml = $_[0]->{xml}; > $xsltcode = $_[0]->{code}; > $filename = $_[0]->{file} if !$xsltcode; #xsltcode gets priority >+ $parameters = $_[0]->{parameters} if ref $_[0]->{parameters} eq 'HASH'; > } else { > ( $xml, $filename ) = @_; > } >@@ -192,7 +194,15 @@ sub transform { > return $retval; > } > my $str = eval { >- my $result = $stsh->transform($source); >+ #$parameters is an optional hashref that contains >+ #key-value pairs to be sent to the XSLT. >+ #Numbers may be bare but strings must be double quoted >+ #(e.g. "'string'" or '"string"'). See XML::LibXSLT for >+ #more details. >+ >+ #NOTE: Parameters are not cached. They are provided for >+ #each different transform. >+ my $result = $stsh->transform($source, %$parameters); > $stsh->output_as_chars($result); > }; > if ($@) { >diff --git a/t/db_dependent/XSLT_Handler.t b/t/db_dependent/XSLT_Handler.t >index 9a6c2dc..572ee25 100644 >--- a/t/db_dependent/XSLT_Handler.t >+++ b/t/db_dependent/XSLT_Handler.t >@@ -21,7 +21,7 @@ use Modern::Perl; > > use FindBin; > use File::Slurp; >-use Test::More tests => 37; >+use Test::More tests => 40; > use Test::Warn; > > use Koha::XSLT_Handler; >@@ -156,4 +156,27 @@ $output= $engine->transform( $xml_2, $xsltfile_3 ); > is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); > is( $engine->refresh, 3, 'Final test on clearing cache' ); > >+my $xsltfile_4 = 'test04.xsl'; >+is( -e $path.$xsltfile_4, 1, "Found my test stylesheet $xsltfile_4" ); >+exit if !-e $path.$xsltfile_4; >+$xsltfile_4 = $path.$xsltfile_4; >+ >+my $parameters = { injected_variable => "'this is a test'",}; >+$output = $engine->transform({ >+ xml => $xml_1, >+ file => $xsltfile_4, >+ parameters => $parameters, >+ }); >+require XML::LibXML; >+my $dom = XML::LibXML->load_xml(string => $output); >+my $result = $dom->find( '/just_a_tagname' ); >+is ( $result->to_literal(), 'this is a test', "Successfully injected string into XSLT parameter/variable"); >+ >+$output = $engine->transform({ >+ xml => $xml_1, >+ file => $xsltfile_4, >+ }); >+my $dom = XML::LibXML->load_xml(string => $output); >+my $result = $dom->find( '/just_a_tagname' ); >+is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added"); > #End of tests >diff --git a/t/db_dependent/XSLT_Handler/test04.xsl b/t/db_dependent/XSLT_Handler/test04.xsl >new file mode 100644 >index 0000000..7b20ed7 >--- /dev/null >+++ b/t/db_dependent/XSLT_Handler/test04.xsl >@@ -0,0 +1,18 @@ >+<xsl:stylesheet version="1.0" >+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >+ xmlns:marc="http://www.loc.gov/MARC21/slim" >+> >+ <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/> >+ <xsl:param name="injected_variable" /> >+ >+ <xsl:template match="/"> >+ <xsl:apply-templates/> >+ </xsl:template> >+ >+ <xsl:template match="node()"> >+ <xsl:copy> >+ <xsl:value-of select="$injected_variable"/> >+ </xsl:copy> >+ </xsl:template> >+ >+</xsl:stylesheet> >-- >1.8.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 13262
:
33608
|
33609
|
33967
|
33978
|
34915
| 35566 |
35685