From 612495448f0aad591d8549e3baf60791c1c740f3 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 17 Nov 2014 13:59:49 +1100 Subject: [PATCH] Bug 13262 - Add parameters to XSLT Handler transform method Content-Type: text/plain; charset=utf-8 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 . 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. Signed-off-by: Marcel de Rooy --- Koha/XSLT_Handler.pm | 14 +++++++++++--- t/db_dependent/XSLT_Handler.t | 13 ++++++++++++- t/db_dependent/XSLT_Handler/test04.xsl | 18 ++++++++++++++++++ 3 files changed, 41 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 3b30ee8..f7b4f7d 100644 --- a/Koha/XSLT_Handler.pm +++ b/Koha/XSLT_Handler.pm @@ -128,7 +128,7 @@ __PACKAGE__->mk_accessors(qw( do_not_return_source print_warns )); =head2 transform - my $output= $xslt_engine->transform( $xml, $xsltfilename ); + my $output= $xslt_engine->transform( $xml, $xsltfilename, [$parameters] ); if( $xslt_engine->err ) { #decide what to do on failure.. } @@ -144,7 +144,7 @@ __PACKAGE__->mk_accessors(qw( do_not_return_source print_warns )); =cut sub transform { - my ( $self, $orgxml, $file ) = @_; + my ( $self, $orgxml, $file, $parameters ) = @_; #Initialized yet? if ( !$self->{xslt_hash} ) { @@ -182,7 +182,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 8bdfdef..2d4cda0 100644 --- a/t/db_dependent/XSLT_Handler.t +++ b/t/db_dependent/XSLT_Handler.t @@ -20,7 +20,7 @@ use Modern::Perl; use FindBin; -use Test::More tests => 24; +use Test::More tests => 26; use Koha::XSLT_Handler; @@ -116,4 +116,15 @@ $output= $engine->transform( $xml_2, $xsltfile_3 ); is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); is( $engine->refresh, 2, '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_1, $xsltfile_4, $parameters); +require XML::LibXML; +my $dom = XML::LibXML->load_xml(string => $output); +my $result = $dom->find( '/just_a_tagname' ); +is ( $result, 'this is a test', "Successfully injected string into XSLT parameter/variable"); + #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 @@ + + + + + + + + + + + + + + + -- 1.7.7.6