[Koha-devel] Random XSLT knowledge

Marcel de Rooy M.de.Rooy at rijksmuseum.nl
Mon Mar 11 08:36:39 CET 2024


Cool. Nice example. Thanks.

From: David Cook <dcook at prosentient.com.au>
Sent: Monday, March 11, 2024 12:27 AM
To: Marcel de Rooy <M.de.Rooy at rijksmuseum.nl>; 'Koha Devel' <koha-devel at lists.koha-community.org>
Subject: RE: [Koha-devel] Random XSLT knowledge

Hi Marcel,

Sure!

Before Koha started using “http://exslt.org/strings” to encode URIs, I used to use register_function to add my own function to do that.


  1.  vi C4/XSLT.pm
  2.  Add a line near the top like:
     *   XML::LibXSLT->register_function("http://wwww.prosentient.com.au/xsltperl", "uri-escape",\&Koha::Prosentient::Biblio::Urls::xslt_uri_escape);
  3.  Write a function like this:
sub xslt_uri_escape {
    my ($uri) = @_;
    #$uri should always be a XML::LibXML::Nodelist, even if that Nodelist just contains 1 URI
    if (ref $uri eq "XML::LibXML::NodeList"){
        $uri->foreach(
            sub {
                my ( $node ) = @_;
                #If the node has child (text) nodes
                if ($node->hasChildNodes()){
                    my @childNodes = $node->childNodes();
                    if (@childNodes){
                        foreach my $childNode (@childNodes){
                            if (my $textdata = $childNode->data){
                                #Trim whitespace
                                $textdata =~ s/^\s+|\s+$//g;
                                my $encoded_url = URI::Encode::uri_encode($textdata, { encode_reserved => 0 });
                                if ($encoded_url){
                                    #Replace the existing URI data with the encoded URI data
                                    $childNode->setData($encoded_url);
                                }
                            }
                        }
                    }
                }
            }
        );
    }
    return $uri;
}


  1.  Add the namespace at the top of your XSLT:
     *   xmlns:pro="http://wwww.prosentient.com.au/xsltperl"
  2.  Call the function on some XML:
     *   <xsl:value-of select="pro:uri-escape(//marc:datafield[@tag=856]/marc:subfield[@code='u'])"/>

--

More recently, I’ve done more complicated things like looking up item urls for that biblio record, deduplicating them against the 856$u, and adding them to the online access in the search results. I continue to take into account OpacHiddenItems and its associated preferences. Since it’s operating at the Perl level, I’m able to check the C4::Context->userenv for the patron details as well.

David Cook
Senior Software Engineer
Prosentient Systems
Suite 7.03
6a Glen St
Milsons Point NSW 2061
Australia

Office: 02 9212 0899
Online: 02 8005 0595

From: Marcel de Rooy <M.de.Rooy at rijksmuseum.nl<mailto:M.de.Rooy at rijksmuseum.nl>>
Sent: Friday, 8 March 2024 6:13 PM
To: 'Koha Devel' <koha-devel at lists.koha-community.org<mailto:koha-devel at lists.koha-community.org>>; David Cook <dcook at prosentient.com.au<mailto:dcook at prosentient.com.au>>
Subject: Re: [Koha-devel] Random XSLT knowledge

Hi David,
Thanks for sharing.
Would you have an example of how you use register_function with Koha functions to share?

Marcel

________________________________
Van: Koha-devel <koha-devel-bounces at lists.koha-community.org<mailto:koha-devel-bounces at lists.koha-community.org>> namens David Cook via Koha-devel <koha-devel at lists.koha-community.org<mailto:koha-devel at lists.koha-community.org>>
Verzonden: vrijdag 8 maart 2024 03:02
Aan: 'Koha Devel' <koha-devel at lists.koha-community.org<mailto:koha-devel at lists.koha-community.org>>
Onderwerp: [Koha-devel] Random XSLT knowledge


Hi all,



I’ve been working on performance issues, and in the process I got looking at XSLTs.



I just wanted to share that it’s possible to pass strings to the XSLT’s transform() method:



-    return $engine->transform($xmlrecord, $xslfilename ); #file or URL

+    return $engine->transform({

+        xml => $xmlrecord,

+        file => $xslfilename,

+        parameters => {

+            test => "'$test_str'",

+        },

+    }); #file or URL



It’s somewhat limited in that you can only pass strings and I think there’s a small limit on the number of parameters you can pass (not sure if it’s 32 or 255), but I thought it was interesting. It would allow you to pass some data that you have at hand on a per-XML record basis without having to mangle the XML record (like we do with items and system preferences).



In the end, I didn’t end up using it though. Instead, I use XML::LibXSLT->register_function() to provide access to Koha functions from the XSLT, and in this case that meets my needs.



Anyway, back to it..



David Cook

Senior Software Engineer

Prosentient Systems

Suite 7.03

6a Glen St

Milsons Point NSW 2061

Australia



Office: 02 9212 0899

Online: 02 8005 0595


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.koha-community.org/pipermail/koha-devel/attachments/20240311/1037b5d8/attachment-0001.htm>


More information about the Koha-devel mailing list