implement-saxon-extension-functions.dita 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <topic id="implement-saxon-extension-functions">
  5. <title>Implementing Saxon extension functions</title>
  6. <titlealts>
  7. <navtitle>Saxon extensions</navtitle>
  8. </titlealts>
  9. <shortdesc>Plug-ins can contribute Saxon extension functions for use in XSLT transformations run by DITA Open
  10. Toolkit.</shortdesc>
  11. <prolog>
  12. <metadata>
  13. <keywords>
  14. <indexterm>Saxon
  15. <indexterm><xmlelement>service</xmlelement></indexterm></indexterm>
  16. <indexterm>Ant
  17. <indexterm><xmlelement>jar</xmlelement></indexterm></indexterm>
  18. <indexterm>XSLT
  19. <indexterm>Saxon</indexterm></indexterm>
  20. <indexterm>Java
  21. <indexterm>extension functions</indexterm></indexterm>
  22. </keywords>
  23. </metadata>
  24. </prolog>
  25. <body>
  26. <p>Starting with Saxon 9.2, the mechanism for implementing extension functions has changed such that Saxon HE, in
  27. particular, can no longer use the older “reflexive” mechanism for finding Java extension functions using a magic
  28. URL. Instead, you implement extension functions and then register them directly on the Saxon Configuration object.
  29. DITA-OT provides a dynamic mechanism to perform this registration for plug-in-provided extension functions.</p>
  30. <p>To implement extension functions, you must do the following:
  31. <ol>
  32. <li>Add your plug-in’s JAR file in the DITA-OT class path as described in
  33. <xref keyref="plugin-javalib"/>.</li>
  34. <li>For each function, implement a class that extends
  35. <codeph>net.sf.saxon.lib.ExtensionFunctionDefinition</codeph>. This class provides the namespace name and
  36. function name for the function as well as details about its arguments and so on. See
  37. <xref keyref="saxon-java-extensions-impl"/> in the Saxon documentation.</li>
  38. <li>Include a file named <filepath>net.sf.saxon.lib.ExtensionFunctionDefinition</filepath> in the directory
  39. <filepath>META-INF/services</filepath> in the compiled JAR that your plug-in provides. Each line of the file
  40. must be the name of a class that implements <codeph>net.sf.saxon.lib.ExtensionFunctionDefinition</codeph>: <codeblock>com.example.saxon.functions.Add
  41. com.example.saxon.functions.Substract</codeblock>
  42. <p>You can create the file using <xmlelement>service</xmlelement> elements in an Ant
  43. <xmlelement>jar</xmlelement>
  44. task:<codeblock outputclass="language-xml">&lt;jar destfile="${basedir}/target/lib/example-saxon.jar">
  45. &lt;service type="net.sf.saxon.lib.ExtensionFunctionDefinition">
  46. &lt;provider classname="com.example.saxon.functions.Add"/>
  47. &lt;provider classname="com.example.saxon.functions.Subtract"/>
  48. &lt;/service>
  49. &lt;/jar></codeblock></p></li>
  50. <li>In your XSLT transformations, declare the namespace the functions are bound
  51. to:<codeblock outputclass="language-xml">&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  52. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  53. <b>xmlns:eg="http://example.com/saxon-extensions"</b>
  54. version="2.0"></codeblock></li>
  55. </ol></p>
  56. <p>You should then be able to use the extension functions as you would any other
  57. function:<codeblock outputclass="language-xml">&lt;xsl:variable name="test" select="<b>eg:add(1, 2)</b>"/></codeblock></p>
  58. </body>
  59. </topic>