Using a custom PHP function in an XSLT

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using a custom PHP function in an XSLT

Schulkins, Joe

Hi,

 

I’ve written a PHP function (called padStr) to pad strings with leading zeroes and I would like to use this in an XSLT I’ve written. Do I need to save it somewhere special? How do I then reference the function within the XSLT – do I put an include in with the xmlns?

 

Once it is imported would I then be able to use the function inline like:

 

<field name="hierarchy_sequence">    

<xsl:value-of select="php:function('padStr',('atom[@name=\'EADUnitID\']', ' .-'))" />

</field>

 

Or would I have to add it as a separate template entry?

 

The function looks like:

 

function padStr($string, $delim){

                $tok = strtok($string, $delim);

                $var = [];

               

                while ($tok !== false) {

                  if(is_numeric($tok) && strlen($tok) == 1){

                    $tok = str_pad($tok, 2, '0', STR_PAD_LEFT);

                  }

    array_push($var,"$tok");

    $tok = strtok($delim);

    }

               

    $imp = implode('.', $var);

    return $imp;

}

 

Thanks for any help.

 

Regards,

Joe Schulkins

 

Joseph Schulkins | Systems Librarian | Libraries, Museums and Galleries | PO Box 123 | Liverpool L69 3DA | [hidden email] T 0151 794 3844

 

Follow us@LivUniLibrary   Like usLivUniLibrary   Visit ushttp://www.liv.ac.uk/library

Special Collections & Archives bloghttp://manuscriptsandmore.liv.ac.uk

Victoria Galleries and Museumhttp://vgm.liverpool.ac.uk

Garstang Museumhttp://www.liverpool.ac.uk/garstang-museum

 

library-logo

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
VuFind-General mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/vufind-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using a custom PHP function in an XSLT

Demian Katz

Joe,

 

The properties files used by the XSLT importer support custom_class[] and truncate_custom_class settings that will probably help you with your needs. See the comments for some detailed documentation:

 

https://github.com/vufind-org/vufind/blob/master/import/dspace.properties#L9

 

(I’m linking to the DSpace example, but these work the same in all of the examples).

 

What you probably want to do is put your padStr function inside a class (if you have a local module configured, that’s the best place – something like MyModule\XSLT\MyXsltFunctions), point to the class from your XSLT properties file, and then refer to the function in the XSLT as ‘MyXsltFunctions::padStr’.

 

If you still have questions about any part of this, or if you run into trouble, please let me know and I’ll be happy to provide further assistance.

 

- Demian

 

From: Schulkins, Joe [mailto:[hidden email]]
Sent: Wednesday, January 18, 2017 5:37 AM
To: [hidden email]
Subject: [VuFind-General] Using a custom PHP function in an XSLT

 

Hi,

 

I’ve written a PHP function (called padStr) to pad strings with leading zeroes and I would like to use this in an XSLT I’ve written. Do I need to save it somewhere special? How do I then reference the function within the XSLT – do I put an include in with the xmlns?

 

Once it is imported would I then be able to use the function inline like:

 

<field name="hierarchy_sequence">    

<xsl:value-of select="php:function('padStr',('atom[@name=\'EADUnitID\']', ' .-'))" />

</field>

 

Or would I have to add it as a separate template entry?

 

The function looks like:

 

function padStr($string, $delim){

 

                $tok = strtok($string, $delim);

                $var = [];

               

                while ($tok !== false) {

                  if(is_numeric($tok) && strlen($tok) == 1){

                    $tok = str_pad($tok, 2, '0', STR_PAD_LEFT);

                  }

    array_push($var,"$tok");

    $tok = strtok($delim);

    }

               

    $imp = implode('.', $var);

    return $imp;

}

 

Thanks for any help.

 

Regards,

Joe Schulkins

 

Joseph Schulkins | Systems Librarian | Libraries, Museums and Galleries | PO Box 123 | Liverpool L69 3DA | [hidden email] T 0151 794 3844

 

Follow us@LivUniLibrary   Like usLivUniLibrary   Visit ushttp://www.liv.ac.uk/library

Special Collections & Archives bloghttp://manuscriptsandmore.liv.ac.uk

Victoria Galleries and Museumhttp://vgm.liverpool.ac.uk

Garstang Museumhttp://www.liverpool.ac.uk/garstang-museum

 

library-logo

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
VuFind-General mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/vufind-general
Loading...