implement-saxon-extension-functions.dita 3.5 KB

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