Creating extension functions |
This chapter describes how to create your own library of XPath extension functions which can be called within your XSLT transformations.
The first step is to write the Eiffel code for your extension functions. You do this by writing at least one Eiffel class. Usually you should create a separate class for each function, but if several functions are variations on a theme, it might be simpler for them all to share one class, and just use a different creation procedure for each implemented function. An example of this approach is the implementation of the core XPath functions fn:encode-for-uri(), fn:iri-to-uri() and fn:escape-html-uri(), which all share the same implementation code in XM_XPATH_ESCAPE_URI, the only difference between them being the set of reserved characters.
Your Eiffel class should be a descendant of XM_XPATH_SYSTEM_FUNCTION (if it is possible for your function to always be evaluated when the transformation is compiled, then it is more efficient to inherit fron XM_XPATH_COMPILE_TIME_FUNCTION instead).
Writing extension functions as descendants of XM_XPATH_SYSTEM_FUNCTION probably requires some understanding of how the XPath library works. This is not properly documented yet. If in doubt ask for help on one of the Gobo mailing lists.
You will usually want to redefine either evaluate_item or create_iterator, or both (if your function ever returns sequences of more than one item, then you will need to redefine create_iterator).You may also need to redefine other routines. These routines are likely candidates:
You will also need to implement the following routines:
You must also implement a creation routine with zero arguments. This must perform the following operations:
Next you should create a descendant of XM_XPATH_FUNCTION_LIBRARY. You need to implement two routines:
If the argument is_restricted is True, then your function is being bound within an [xsl:]use-when context. If it is sensible for your function to be callable in such a context, then by all means ignore this argument. Otherwise you should create an instance of XM_XSLT_DEFERRED_ERROR. See XM_XSLT_BUILTIN_EXTENSION_FUNCTIONS for an example.
There is one last step to take, you have to tell the library about the existence of your function library.
Just pass your library as the sole argument to add_extension_function_library of XM_XSLT_CONFIGURATION. After this call, your library will be searched whenever a transformation is compiled.
Copyright © 2005, Colin Adams and others mailto:colin@colina.demon.co.uk http://www.gobosoft.com Last Updated: Monday, November 21st, 2005 |