1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
5 <style type="text/css"><!--
6 TD {font-size: 10pt; font-family: Verdana,Arial,Helvetica}
7 BODY {font-size: 10pt; font-family: Verdana,Arial,Helvetica; margin-top: 5pt; margin-left: 0pt; margin-right: 0pt}
8 H1 {font-size: 16pt; font-family: Verdana,Arial,Helvetica}
9 H2 {font-size: 14pt; font-family: Verdana,Arial,Helvetica}
10 H3 {font-size: 12pt; font-family: Verdana,Arial,Helvetica}
11 A:link, A:visited, A:active { text-decoration: underline }
13 <title>Writing extensions</title>
15 <body bgcolor="#8b7765" text="#000000" link="#000000" vlink="#000000">
16 <table border="0" width="100%" cellpadding="5" cellspacing="0" align="center"><tr>
18 <a href="http://www.gnome.org/"><img src="smallfootonly.gif" alt="Gnome Logo"></a><a href="http://www.redhat.com"><img src="redhat.gif" alt="Red Hat Logo"></a>
20 <td><table border="0" width="90%" cellpadding="2" cellspacing="0" align="center" bgcolor="#000000"><tr><td><table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="#fffacd"><tr><td align="center">
21 <h1>The XSLT C library for Gnome</h1>
22 <h2>Writing extensions</h2>
23 </td></tr></table></td></tr></table></td>
25 <table border="0" cellpadding="4" cellspacing="0" width="100%" align="center"><tr><td bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr>
26 <td valign="top" width="200" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td>
27 <table width="100%" border="0" cellspacing="1" cellpadding="3">
28 <tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Main Menu</b></center></td></tr>
29 <tr><td bgcolor="#fffacd"><ul style="margin-left: -2pt">
30 <li><a href="index.html">Home</a></li>
31 <li><a href="intro.html">Introduction</a></li>
32 <li><a href="docs.html">Documentation</a></li>
33 <li><a href="bugs.html">Reporting bugs and getting help</a></li>
34 <li><a href="help.html">How to help</a></li>
35 <li><a href="downloads.html">Downloads</a></li>
36 <li><a href="FAQ.html">FAQ</a></li>
37 <li><a href="news.html">News</a></li>
38 <li><a href="xsltproc2.html">The xsltproc tool</a></li>
39 <li><a href="API.html">The programming API</a></li>
40 <li><a href="internals.html">Library internals</a></li>
41 <li><a href="extensions.html">Writing extensions</a></li>
42 <li><a href="contribs.html">Contributions</a></li>
44 <a href="xslt.html">flat page</a>, <a href="site.xsl">stylesheet</a>
48 <table width="100%" border="0" cellspacing="1" cellpadding="3">
49 <tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
50 <tr><td bgcolor="#fffacd"><ul style="margin-left: -2pt">
51 <li><a href="tutorial/libxslttutorial.html">Tutorial</a></li>
52 <li><a href="xsltproc.html">Man page for xsltproc</a></li>
53 <li><a href="http://mail.gnome.org/archives/xslt/">Mail archive</a></li>
54 <li><a href="http://xmlsoft.org/">XML libxml</a></li>
55 <li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
56 <li><a href="ftp://xmlsoft.org/">FTP</a></li>
57 <li><a href="http://www.fh-frankfurt.de/~igor/projects/libxml/">Windows binaries</a></li>
58 <li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
59 <li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxslt">Bug Tracker</a></li>
60 <li><a href="http://xsldbg.sourceforge.net/">Xsldbg Debugger</a></li>
63 </td></tr></table></td>
64 <td valign="top" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%"><tr><td><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td><table border="0" cellpadding="3" cellspacing="1" width="100%"><tr><td bgcolor="#fffacd">
65 <h3>Table of content</h3>
67 <li><a href="extensions.html#Introducti">Introduction</a></li>
68 <li><a href="extensions.html#Basics">Basics</a></li>
69 <li><a href="extensions.html#Keep">Extension modules</a></li>
70 <li><a href="extensions.html#Registerin">Registering a module</a></li>
71 <li><a href="extensions.html#module">Loading a module</a></li>
72 <li><a href="extensions.html#Registerin1">Registering an extension
74 <li><a href="extensions.html#Implementi">Implementing an extension
76 <li><a href="extensions.html#Examples">Examples for extension
78 <li><a href="extensions.html#Registerin2">Registering an extension
80 <li><a href="extensions.html#Implementi1">Implementing an extension
82 <li><a href="extensions.html#Example">Example for extension
84 <li><a href="extensions.html#shutdown">The shutdown of a module</a></li>
85 <li><a href="extensions.html#Future">Future work</a></li>
87 <h3><a name="Introducti1">Introduction</a></h3>
88 <p>This document describes the work needed to write extensions to the
89 standard XSLT library for use with <a href="http://xmlsoft.org/XSLT/">libxslt</a>, the <a href="http://www.w3.org/TR/xslt">XSLT</a> C library developped for the <a href="http://www.gnome.org/">Gnome</a> project.</p>
90 <p>Before starting reading this document it is highly recommended to get
91 familiar with <a href="internals.html">the libxslt internals</a>.</p>
92 <p>Note: this documentation is by definition incomplete and I am not good at
93 spelling, grammar, so patches and suggestions are <a href="mailto:veillard@redhat.com">really welcome</a>.</p>
94 <h3><a name="Basics">Basics</a></h3>
95 <p>The <a href="http://www.w3.org/TR/xslt">XSLT specification</a> provides
96 two <a href="http://www.w3.org/TR/xslt">ways to extend an XSLT engine</a>:</p>
98 <li>providing <a href="http://www.w3.org/TR/xslt">new extension
99 functions</a> which can be called from XPath expressions</li>
100 <li>providing <a href="http://www.w3.org/TR/xslt">new extension
101 elements</a> which can be inserted in stylesheets</li>
103 <p>In both cases the extensions need to be associated to a new namespace,
104 i.e. an URI used as the name for the extension's namespace (there is no need
105 to have a resource there for this to work).</p>
106 <p>libxslt provides a few extensions itself, either in libxslt namespace
107 "http://xmlsoft.org/XSLT/" or in other namespace for well known extensions
108 provided by other XSLT processors like Saxon, Xalan or XT.</p>
109 <h3><a name="Keep">Extension modules</a></h3>
110 <p>Since extensions are bound to a namespace name, usually sets of extensions
111 coming from a given source are using the same namespace name defining in
112 practice a group of extensions providing elements, functions or both. From
113 libxslt point of view those are considered as an "extension module", and most
114 of the APIs work at a module point of view.</p>
115 <p>Registration of new functions or elements are bound to the activation of
116 the module, this is currently done by declaring the namespace as an extension
117 by using the attribute <code>extension-element-prefixes</code> on the
118 <code><a href="http://www.w3.org/TR/xslt">xsl:stylesheet</a></code>
120 <p>And extension module is defined by 3 objects:</p>
122 <li>the namespace name associated</li>
123 <li>an initialization function</li>
124 <li>a shutdown function</li>
126 <h3><a name="Registerin">Registering a module</a></h3>
127 <p>Currently a libxslt module has to be compiled within the application using
128 libxslt, there is no code to load dynamically shared libraries associated to
129 namespace (this may be added but is likely to become a portability
131 <p>So the current way to register a module is to link the code implementing
132 it with the application and to call a registration function:</p>
133 <pre>int xsltRegisterExtModule(const xmlChar *URI,
134 xsltExtInitFunction initFunc,
135 xsltExtShutdownFunction shutdownFunc);</pre>
136 <p>The associated header is read by:</p>
137 <pre>#include<libxslt/extensions.h></pre>
138 <p>which also defines the type for the initialization and shutdown
140 <h3><a name="module">Loading a module</a></h3>
141 <p>Once the module URI has been registered and if the XSLT processor detects
142 that a given stylesheet needs the functionalities of an extended module, this
143 one is initialized.</p>
144 <p>The xsltExtInitFunction type defines the interface for an initialization
147 * xsltExtInitFunction:
148 * @ctxt: an XSLT transformation context
149 * @URI: the namespace URI for the extension
151 * A function called at initialization time of an XSLT
154 * Returns a pointer to the module specific data for this
157 typedef void *(*xsltExtInitFunction)(xsltTransformContextPtr ctxt,
158 const xmlChar *URI);</pre>
159 <p>There are 3 things to notice:</p>
161 <li>the function gets passed the namespace name URI as an argument, this
162 allow a single function to provide the initialization for multiple
164 <li>it also gets passed a transformation context, the initialization is
165 done at run time before any processing occurs on the stylesheet but it
166 will be invoked separately each time for each transformation</li>
167 <li>it returns a pointer, this can be used to store module specific
168 informations which can be retrieved later when a function or an element
169 from the extension are used, an obvious example is a connection to a
170 database which should be kept and reused along the transformation. NULL
171 is a perfectly valid return, there is no way to indicate a failure at
174 <p>What this function is expected to do is:</p>
176 <li>prepare the context for this module (like opening the database
178 <li>register the extensions specific to this module</li>
180 <h3><a name="Registerin1">Registering an extension function</a></h3>
181 <p>There is a single call to do this registration:</p>
182 <pre>int xsltRegisterExtFunction(xsltTransformContextPtr ctxt,
185 xmlXPathEvalFunc function);</pre>
186 <p>The registration is bound to a single transformation instance referred by
187 ctxt, name is the UTF8 encoded name for the NCName of the function, and URI
188 is the namespace name for the extension (no checking is done, a module could
189 register functions or elements from a different namespace, but it is not
191 <h3><a name="Implementi">Implementing an extension function</a></h3>
192 <p>The implementation of the function must have the signature of a libxml
196 * @ctxt: an XPath parser context
197 * @nargs: the number of arguments passed to the function
199 * an XPath evaluation function, the parameters are on the
200 * XPath context stack
203 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
205 <p>The context passed to an XPath function is not an XSLT context but an <a href="internals.html#XPath1">XPath context</a>. However it is possible to
206 find one from the other:</p>
208 <li>The function xsltXPathGetTransformContext provide this lookup facility:
209 <pre>xsltTransformContextPtr
210 xsltXPathGetTransformContext
211 (xmlXPathParserContextPtr ctxt);</pre>
213 <li>The <code>xmlXPathContextPtr</code> associated to an
214 <code>xsltTransformContext</code> is stored in the <code>xpathCtxt</code>
217 <p>The first thing an extension function may want to do is to check the
218 arguments passed on the stack, the <code>nargs</code> will precise how many
219 of them were provided on the XPath expression. The macros valuePop will
220 extract them from the XPath stack:</p>
221 <pre>#include <libxml/xpath.h>
222 #include <libxml/xpathInternals.h>
224 xmlXPathObjectPtr obj = valuePop(ctxt); </pre>
225 <p>Note that <code>ctxt</code> is the XPath context not the XSLT one. It is
226 then possible to examine the content of the value. Check <a href="internals.html#Descriptio">the description of XPath objects</a> if
227 necessary. The following is a common sequcnce checking whether the argument
228 passed is a string and converting it using the built-in XPath
229 <code>string()</code> function if this is not the case:</p>
230 <pre>if (obj->type != XPATH_STRING) {
231 valuePush(ctxt, obj);
232 xmlXPathStringFunction(ctxt, 1);
233 obj = valuePop(ctxt);
235 <p>Most common XPath functions are available directly at the C level and are
236 exported either in <code><libxml/xpath.h></code> or in
237 <code><libxml/xpathInternals.h></code>.</p>
238 <p>The extension function may also need to retrieve the data associated to
239 this module instance (the database connection in the previous example) this
240 can be done using the xsltGetExtData:</p>
241 <pre>void * xsltGetExtData(xsltTransformContextPtr ctxt,
242 const xmlChar *URI);</pre>
243 <p>again the URI to be provided is the one used which was used when
244 registering the module.</p>
245 <p>Once the function finishes, don't forget to:</p>
247 <li>push the return value on the stack using <code>valuePush(ctxt,
250 <li>deallocate the parameters passed to the function using
251 <code>xmlXPathFreeObject(obj)</code>
254 <h3><a name="Examples">Examples for extension functions</a></h3>
255 <p>The module libxslt/functions.c containsthe sources of the XSLT built-in
256 functions, including document(), key(), generate-id(), etc. as well as a full
257 example module at the end. Here is the test function implementation for the
258 libxslt:test function:</p>
260 * xsltExtFunctionTest:
261 * @ctxt: the XPath Parser context
262 * @nargs: the number of arguments
264 * function libxslt:test() for testing the extensions support.
267 xsltExtFunctionTest(xmlXPathParserContextPtr ctxt, int nargs)
269 xsltTransformContextPtr tctxt;
272 tctxt = xsltXPathGetTransformContext(ctxt);
274 xsltGenericError(xsltGenericErrorContext,
275 "xsltExtFunctionTest: failed to get the transformation context\n");
278 data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
280 xsltGenericError(xsltGenericErrorContext,
281 "xsltExtFunctionTest: failed to get module data\n");
284 #ifdef WITH_XSLT_DEBUG_FUNCTION
285 xsltGenericDebug(xsltGenericDebugContext,
286 "libxslt:test() called with %d args\n", nargs);
289 <h3><a name="Registerin2">Registering an extension function</a></h3>
290 <p>There is a single call to do this registration:</p>
291 <pre>int xsltRegisterExtElement(xsltTransformContextPtr ctxt,
294 xsltTransformFunction function);</pre>
295 <p>It is similar to the mechanism used to register an extension function,
296 except that the signature of an extension element implementation is
298 <p>The registration is bound to a single transformation instance referred by
299 ctxt, name is the UTF8 encoded name for the NCName of the element, and URI is
300 the namespace name for the extension (no checking is done, a module could
301 register elements for a different namespace, but it is not recommended).</p>
302 <h3><a name="Implementi1">Implementing an extension element</a></h3>
303 <p>The implementation of the element must have the signature of an XSLT
304 transformation function:</p>
306 * xsltTransformFunction:
307 * @ctxt: the XSLT transformation context
308 * @node: the input node
309 * @inst: the stylesheet node
310 * @comp: the compiled information from the stylesheet
312 * signature of the function associated to elements part of the
313 * stylesheet language like xsl:if or xsl:apply-templates.
315 typedef void (*xsltTransformFunction)
316 (xsltTransformContextPtr ctxt,
319 xsltStylePreCompPtr comp);</pre>
320 <p>The first argument is the XSLT transformation context. The second and
321 third arguments are xmlNodePtr i.e. internal memory <a href="internals.html#libxml">representation of XML nodes</a>. They are
322 respectively <code>node</code> from the the input document being transformed
323 by the stylesheet and <code>inst</code> the extension element in the
324 stylesheet. The last argument is <code>comp</code> a pointer to a precompiled
325 representation of <code>inst</code> but usually for extension function this
326 value is <code>NULL</code> by default (it could be added and associated to
327 the instruction in <code>inst->_private</code>).</p>
328 <p>The same functions are available from a function implementing an extension
329 element as in an extension function, including
330 <code>xsltGetExtData()</code>.</p>
331 <p>The goal of extension element being usually to enrich the generated
332 output, it is expected that they will grow the currently generated output
333 tree, this can be done by grabbing ctxt->insert which is the current
334 libxml node being generated (Note this can also be the intermediate value
335 tree being built for example to initialize a variable, the processing should
336 be similar). The functions for libxml tree manipulation from <a href="http://xmlsoft.org/html/libxml-tree.html"><libxml/tree.h></a> can
337 be employed to extend or modify the tree, but it is required to preserve the
338 insertion node and its ancestors since there is existing pointers to those
339 elements still in use in the XSLT template execution stack.</p>
340 <h3><a name="Example">Example for extension elements</a></h3>
341 <p>The module libxslt/transform.c containsthe sources of the XSLT built-in
342 elements, including xsl:element, xsl:attribute, xsl:if, etc. There is a small
343 but full example in functions.c providing the implementation for the
344 libxslt:test element, it will output a comment in the result tree:</p>
346 * xsltExtElementTest:
347 * @ctxt: an XSLT processing context
348 * @node: The current node
349 * @inst: the instruction in the stylesheet
350 * @comp: precomputed informations
352 * Process a libxslt:test node
355 xsltExtElementTest(xsltTransformContextPtr ctxt, xmlNodePtr node,
357 xsltStylePreCompPtr comp)
362 xsltGenericError(xsltGenericErrorContext,
363 "xsltExtElementTest: no transformation context\n");
367 xsltGenericError(xsltGenericErrorContext,
368 "xsltExtElementTest: no current node\n");
372 xsltGenericError(xsltGenericErrorContext,
373 "xsltExtElementTest: no instruction\n");
376 if (ctxt->insert == NULL) {
377 xsltGenericError(xsltGenericErrorContext,
378 "xsltExtElementTest: no insertion point\n");
382 xmlNewComment((const xmlChar *)
383 "libxslt:test element test worked");
384 xmlAddChild(ctxt->insert, comment);
386 <h3><a name="shutdown">The shutdown of a module</a></h3>
387 <p>When the XSLT processor ends a transformation, the shutdown function (if
388 it exists) of all the modules initialized are called.The
389 xsltExtShutdownFunction type defines the interface for a shutdown
392 * xsltExtShutdownFunction:
393 * @ctxt: an XSLT transformation context
394 * @URI: the namespace URI for the extension
395 * @data: the data associated to this module
397 * A function called at shutdown time of an XSLT extension module
399 typedef void (*xsltExtShutdownFunction) (xsltTransformContextPtr ctxt,
402 <p>this is really similar to a module initialization function except a third
403 argument is passed, it's the value that was returned by the initialization
404 function. This allow to deallocate resources from the module for example
405 close the connection to the database to keep the same example.</p>
406 <h3><a name="Future">Future work</a></h3>
407 <p>Well some of the pieces missing:</p>
409 <li>a way to load shared libraries to instanciate new modules</li>
410 <li>a better detection of extension function usage and their registration
411 without having to use the extension prefix which ought to be reserved to
412 element extensions.</li>
413 <li>more examples</li>
414 <li>implementations of the <a href="http://www.exslt.org/">EXSLT</a> common
415 extension libraries, Thomas Broyer nearly finished implementing them.</li>
418 <p><a href="mailto:daniel@veillard.com">Daniel Veillard</a></p>
419 </td></tr></table></td></tr></table></td></tr></table></td>
420 </tr></table></td></tr></table>