8f4284a1b919359f419204c9376a3a5cb5930ef9
[platform/upstream/libxslt.git] / doc / xslt.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2     "http://www.w3.org/TR/html4/loose.dtd">
3 <html>
4 <head>
5   <title>The XSLT C library for Gnome</title>
6   <meta name="GENERATOR" content="amaya 5.1">
7   <meta http-equiv="Content-Type" content="text/html">
8 </head>
9
10 <body bgcolor="#ffffff">
11 <h1 align="center">The XSLT C library for Gnome</h1>
12
13 <h1 style="text-align: center">libxslt</h1>
14
15 <p>Libxslt is the <a href="http://www.w3.org/TR/xslt">XSLT</a> C library
16 developed for the Gnome project. XSLT itself is a an XML language to define
17 transformation for XML. Libxslt is based on <a
18 href="http://xmlsoft.org/">libxml2</a> the XML C library developed for the
19 Gnome project. It also implements most of the EXSLT set of extensions
20 functions and some of Saxon's evaluate and expressions extensions.</p>
21
22 <p>People can either embed the library in their application or use xsltproc
23 the command line processing tool. This library is free software and can be
24 reused in commercial applications (see the <a href="intro.html">intro</a>)</p>
25
26 <p>External documents:</p>
27 <ul>
28   <li>John Fleck wrote <a href="tutorial/libxslttutorial.html">a tutorial for
29     libxslt</a></li>
30   <li><a href="xsltproc.html">xsltproc user manual</a></li>
31   <li><a href="http://xmlsoft.org/">the libxml documentation</a></li>
32 </ul>
33
34 <p></p>
35
36 <h2><a name="Introducti">Introduction</a></h2>
37
38 <p>This document describes <a href="http://xmlsoft.org/XSLT/">libxslt</a>,
39 the <a href="http://www.w3.org/TR/xslt">XSLT</a> C library developed for the
40 <a href="http://www.gnome.org/">Gnome</a> project.</p>
41
42 <p>Here are some key points about libxslt:</p>
43 <ul>
44   <li>Libxslt is a C implementation</li>
45   <li>Libxslt is based on libxml for XML parsing, tree manipulation and XPath
46     support</li>
47   <li>It is written in plain C, making as few assumptions as possible, and
48     sticking closely to ANSI C/POSIX for easy embedding. Should works on
49     Linux/Unix/Windows.</li>
50   <li>This library is released under the <a
51     href="http://www.opensource.org/licenses/mit-license.html">MIT
52   Licence</a></li>
53   <li>Though not designed primarily with performances in mind, libxslt seems
54     to be a relatively fast processor.</li>
55 </ul>
56
57 <h2><a name="Documentat">Documentation</a></h2>
58
59 <p>There are some on-line resources about using libxslt:</p>
60 <ol>
61   <li>Check the <a href="html/libxslt-lib.html#LIBXSLT-LIB">API
62     documentation</a> automatically extracted from code comments (using <a
63     href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&amp;dir=gtk-doc">gtk
64     doc</a>).</li>
65   <li>Look at the <a href="http://mail.gnome.org/archives/xslt/">mailing-list
66     archive</a>.</li>
67   <li>Of course since libxslt is based on libxml, it's a good idea to at
68     least read <a href="http://xmlsoft.org/">libxml description</a></li>
69 </ol>
70
71 <h2><a name="Reporting">Reporting bugs and getting help</a></h2>
72
73 <p>If you need help with the XSLT language itself, here are a number of
74 useful resources:</p>
75 <ul>
76   <li>I strongly suggest to subscribe to <a
77     href="http://www.mulberrytech.com/xsl/xsl-list">XSL-list</a>, check <a
78     href="http://www.biglist.com/lists/xsl-list/archives/">the XSL-list
79     archives</a></li>
80   <li>The <a href="http://www.dpawson.co.uk/xsl/xslfaq.html">XSL FAQ</a>.</li>
81   <li>The <a
82     href="http://www.nwalsh.com/docs/tutorials/xsl/xsl/slides.html">tutorial</a>
83     written by Paul Grosso and Norman Walsh is a very good on-line
84     introdution to the language.</li>
85   <li>The <a
86     href="http://www.zvon.org/xxl/XSLTutorial/Books/Book1/index.html">only
87     Zvon XSLT tutorial</a> details a lot of constructs with examples.</li>
88   <li><a href="http://www.jenitennison.com/xslt/index.html">Jeni Tennison's
89     XSLT</a> pages provide links to a lot of answers</li>
90   <li>the <a href="http://incrementaldevelopment.com/xsltrick/">Gallery of
91     XSLT Tricks</a> provides non-standard use case of XSLT</li>
92   <li>And I suggest to buy Michael Kay "XSLT Programmer's Reference" book
93     published by <a href="http://www.wrox.com/">Wrox</a> if you plan to work
94     seriously with XSLT in the future.</li>
95 </ul>
96
97 <p>Well, bugs or missing features are always possible, and I will make a
98 point of fixing them in a timely fashion. The best way to report a bug is to
99 use the <a href="http://bugzilla.gnome.org/buglist.cgi?product=libxslt">Gnome
100 bug tracking database</a> (make sure to use the "libxslt" module name). I
101 look at reports there regularly and it's good to have a reminder when a bug
102 is still open. Be sure to specify that the bug is for the package libxslt.</p>
103
104 <p>There is also a mailing-list <a
105 href="mailto:xslt@gnome.org">xslt@gnome.org</a> for libxslt, with an <a
106 href="http://mail.gnome.org/archives/xslt/">on-line archive</a>. To subscribe
107 to this list, please visit the <a
108 href="http://mail.gnome.org/mailman/listinfo/xslt">associated Web</a> page
109 and follow the instructions.</p>
110
111 <p>Alternatively, you can just send the bug to the <a
112 href="mailto:xslt@gnome.org">xslt@gnome.org</a> list, if it's really libxslt
113 related I will approve it.. Please do not send me mail directly especially
114 for portability problem, it makes things really harder to track and in some
115 cases I'm not the best person to answer a given question, ask the list
116 instead. <strong>Do not send code, I won't debug it</strong> (but patches are
117 really appreciated!).</p>
118
119 <p>Check the following too before posting:</p>
120 <ul>
121   <li>make sure you are <a href="ftp://xmlsoft.org/">using a recent
122     version</a>, and that the problem still shows up in those</li>
123   <li>check the <a href="http://mail.gnome.org/archives/xslt/">list
124     archives</a> to see if the problem was reported already, in this case
125     there is probably a fix available, similarly check the <a
126     href="http://bugzilla.gnome.org/buglist.cgi?product=libxslt">registered
127     open bugs</a></li>
128   <li>make sure you can reproduce the bug with xsltproc, a very useful thing
129     to do is run the transformation with -v argument and redirect the
130     standard error to a file, then search in this file for the transformation
131     logs just preceding the possible problem</li>
132   <li>Please send the command showing the error as well as the input and
133     stylesheet (as an attachment)</li>
134 </ul>
135
136 <p>Of course, bugs reports with a suggested patch for fixing them will
137 probably be processed faster.</p>
138
139 <p>If you're looking for help, a quick look at <a
140 href="http://mail.gnome.org/archives/xslt/">the list archive</a> may actually
141 provide the answer, I usually send source samples when answering libxslt
142 usage questions. The <a
143 href="html/libxslt-lib.html#LIBXSLT-LIB">auto-generated documentation</a> is
144 not as polished as I would like (I need to learn more about Docbook), but
145 it's a good starting point.</p>
146
147 <h2><a name="help">How to help</a></h2>
148
149 <p>You can help the project in various ways, the best thing to do first is to
150 subscribe to the mailing-list as explained before, check the <a
151 href="http://mail.gnome.org/archives/xslt/">archives </a>and the <a
152 href="http://bugzilla.gnome.org/buglist.cgi?product=libxslt">Gnome bug
153 database:</a>:</p>
154 <ol>
155   <li>provide patches when you find problems</li>
156   <li>provide the diffs when you port libxslt to a new platform. They may not
157     be integrated in all cases but help pinpointing portability problems
158   and</li>
159   <li>provide documentation fixes (either as patches to the code comments or
160     as HTML diffs).</li>
161   <li>provide new documentations pieces (translations, examples, etc ...)</li>
162   <li>Check the TODO file and try to close one of the items</li>
163   <li>take one of the points raised in the archive or the bug database and
164     provide a fix. <a href="mailto:daniel@veillard.com">Get in touch with me
165     </a>before to avoid synchronization problems and check that the suggested
166     fix will fit in nicely :-)</li>
167 </ol>
168
169 <h2><a name="Downloads">Downloads</a></h2>
170
171 <p>The latest versions of libxslt can be found on <a
172 href="ftp://xmlsoft.org/">xmlsoft.org</a> (<a
173 href="ftp://speakeasy.rpmfind.net/pub/libxml/">Seattle</a>, <a
174 href="ftp://fr.rpmfind.net/pub/libxml/">France</a>) or on the <a
175 href="ftp://ftp.gnome.org/pub/GNOME/MIRRORS.html">Gnome FTP server</a> either
176 as a <a href="ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxslt/">source
177 archive</a> or <a
178 href="ftp://ftp.gnome.org/pub/GNOME/contrib/redhat/SRPMS/">RPM packages</a>.
179 (NOTE that you need the <a
180 href="http://rpmfind.net/linux/RPM/libxml2.html">libxml2</a>, <a
181 href="http://rpmfind.net/linux/RPM/libxml2-devel.html">libxml2-devel</a>, <a
182 href="http://rpmfind.net/linux/RPM/libxslt.html">libxslt</a> and <a
183 href="http://rpmfind.net/linux/RPM/libxslt-devel.html">libxslt-devel</a>
184 packages installed to compile applications using libxslt.) <a
185 href="mailto:izlatkovic@daenet.de">Igor  Zlatkovic</a> is now the maintainer
186 of the Windows port, <a
187 href="http://www.fh-frankfurt.de/~igor/projects/libxml/index.html">he
188 provides binaries</a>. <a href="mailto:Gary.Pennington@sun.com">Gary
189 Pennington</a> provides <a href="http://garypennington.net/libxml2/">Solaris
190 binaries</a>.</p>
191
192 <p><a name="Contribs">Contribs:</a></p>
193
194 <p>I do accept external contributions, especially if compiling on another
195 platform, get in touch with me to upload the package. I will keep them in the
196 <a href="ftp://xmlsoft.org/contribs/">contrib directory</a></p>
197
198 <p>Libxslt is also available from CVS:</p>
199 <ul>
200   <li><p>The <a
201     href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&amp;dir=libxslt">Gnome
202     CVS base</a>. Check the <a
203     href="http://developer.gnome.org/tools/cvs.html">Gnome CVS Tools</a>
204     page; the CVS module is <b>libxslt</b>.</p>
205   </li>
206   <li><a
207     href="ftp://xmlsoft.org/XSLT/cvs-snapshot.tar.gzftp://xmlsoft.org/XSLT/cvs-snapshot.tar.gzftp://xmlsoft.org/XSLT/cvs-snapshot.tar.gz">daily
208     snapshots from CVS</a> are also provided</li>
209 </ul>
210
211 <h2><a name="FAQ">FAQ</a></h2>
212 <ol>
213   <li><em>passing parameters on the xsltproc command line doesn't work</em>
214     <p><em>xsltproc --param test alpha foo.xsl foo.xml</em></p>
215     <p><em>the param does not get passed and ends up as ""</em></p>
216     <p>In a nutshell do a double escaping at the shell prompt:</p>
217     <p>xsltproc --param test "'alpha'" foo.xsl foo.xml</p>
218     <p>i.e. the string value is surrounded by " and ' then terminated by '
219     and ". Libxslt interpret the parameter values as XPath expressions, so
220     the string -&gt;<code>alpha</code>&lt;- is intepreted as the node set
221     matching this string. You really want -&gt;<code>'alpha'</code>&lt;- to
222     be passed to the processor. And to allow this you need to escape the
223     quotes at the shell level using -&gt;<code>"'alpha'"</code>&lt;- .</p>
224     <p>or use</p>
225     <p>xsltproc --stringparam test alpha foo.xsl foo.xml</p>
226   </li>
227 </ol>
228
229 <h2><a name="News">News</a></h2>
230
231 <h3>CVS only : check the <a
232 href="http://cvs.gnome.org/lxr/source/libxslt/ChangeLog">Changelog</a> file
233 for a really accurate description</h3>
234
235 <h3>1.0.13: Mar 8 2002</h3>
236 <ul>
237   <li>a number of bug fixes including "namespace node have no parents"</li>
238   <li>Improvement of the Python bindings</li>
239   <li>Charles Bozeman provided fixes and regression tests for exslt date
240     functions.</li>
241 </ul>
242
243 <h3>1.0.12: Feb 11 2002</h3>
244 <ul>
245   <li>Fixed the makefiles especially the python module ones</li>
246   <li>half a dozen bugs fixes including 2 old ones</li>
247 </ul>
248
249 <h3>1.0.11: Feb 8 2002</h3>
250 <ul>
251   <li>Change of Licence to the <a
252     href="http://www.opensource.org/licenses/mit-license.html">MIT
253   Licence</a></li>
254   <li>Added a beta version of the Python bindings, including support to
255     extend the engine with functions written in Python</li>
256   <li>A number of bug fixes</li>
257   <li>Charlie Bozeman provided more EXSLT functions</li>
258   <li>Portability fixes</li>
259 </ul>
260
261 <h3>1.0.10: Jan 14 2002</h3>
262 <ul>
263   <li>Windows fixes for Win32 from Igor</li>
264   <li>Fixed the Solaris compilation trouble (Albert)</li>
265   <li>Documentation changes and updates: John Fleck</li>
266   <li>Added a stringparam option to avoid escaping hell at the shell
267   level</li>
268   <li>A few bug fixes</li>
269 </ul>
270
271 <h3>1.0.9: Dec 7 2001</h3>
272 <ul>
273   <li>Makefile patches from Peter Williams</li>
274   <li>attempt to fix the compilation problem associated to prelinking</li>
275   <li>obsoleted libxsltbreakpoint now deprecated and frozen to 1.0.8 API</li>
276   <li>xsltproc return codes are now significant, John Fleck updated the
277     documentation</li>
278   <li>patch to allow as much as 40 steps in patterns (Marc Tardif), should be
279     made dynamic really</li>
280   <li>fixed a bug raised by Nik Clayton when using doctypes with HTML
281   output</li>
282   <li>patches from Keith Isdale to interface with xsltdebugger</li>
283 </ul>
284
285 <h3>1.0.8: Nov 26 2001</h3>
286 <ul>
287   <li>fixed an annoying header problem, removed a few bugs and some code
288     cleanup</li>
289   <li>patches for Windows and update of Windows Makefiles by Igor</li>
290   <li>OpenVMS port instructions from John A Fotheringham</li>
291   <li>fixed some Makefiles annoyance and libraries prelinking
292   informations</li>
293 </ul>
294
295 <h3>1.0.7: Nov 10 2001</h3>
296 <ul>
297   <li>remove a compilation problem with LIBXSLT_PUBLIC</li>
298   <li>Finishing the integration steps for Keith Isdale debugger</li>
299   <li>fixes the handling of indent="no" on HTML output</li>
300   <li>fixes on the configure script and RPM spec file</li>
301 </ul>
302
303 <h3>1.0.6: Oct 30 2001</h3>
304 <ul>
305   <li>bug fixes on number formatting (Thomas), date/time functions (Bruce
306     Miller)</li>
307   <li>update of the Windows Makefiles (Igor)</li>
308   <li>fixed DOCTYPE generation rules for HTML output (me)</li>
309 </ul>
310
311 <h3>1.0.5: Oct 10 2001</h3>
312 <ul>
313   <li>some portability fixes, including Windows makefile updates from
314   Igor</li>
315   <li>fixed a dozen bugs on XSLT and EXSLT (me and Thomas Broyer)</li>
316   <li>support for Saxon's evaluate and expressions extensions added (initial
317     contribution from Darren Graves)</li>
318   <li>better handling of XPath evaluation errors</li>
319 </ul>
320
321 <h3>1.0.4: Sep 12 2001</h3>
322 <ul>
323   <li>Documentation updates from John fleck</li>
324   <li>bug fixes (DocBook  FO generation should be fixed)  and portability
325     improvements</li>
326   <li>Thomas Broyer improved the existing EXSLT support and added String,
327     Time and Date core functions support</li>
328 </ul>
329
330 <h3>1.0.3:  Aug 23 2001</h3>
331 <ul>
332   <li>XML Catalog support see the doc</li>
333   <li>New NaN/Infinity floating point code</li>
334   <li>A few bug fixes</li>
335 </ul>
336
337 <h3>1.0.2:  Aug 15 2001</h3>
338 <ul>
339   <li>lot of bug fixes, increased the testsuite</li>
340   <li>a large chunk of EXSLT is implemented</li>
341   <li>improvements on the extension framework</li>
342   <li>documentation improvements</li>
343   <li>Windows MSC projects files should be up-to-date</li>
344   <li>handle attributes inherited from the DTD by default</li>
345 </ul>
346
347 <h3>1.0.1:  July 24 2001</h3>
348 <ul>
349   <li>initial EXSLT framework</li>
350   <li>better error reporting</li>
351   <li>fixed the profiler on Windows</li>
352   <li>bug fixes</li>
353 </ul>
354
355 <h3>1.0.0: July 10 2001</h3>
356 <ul>
357   <li>a lot of cleanup, a lot of regression tests added or fixed</li>
358   <li>added a documentation for <a href="extensions.html">writing
359     extensions</a></li>
360   <li>fixed some variable evaluation problems (with William)</li>
361   <li>added profiling of stylesheet execution accessible as the xsltproc
362     --profile option</li>
363   <li>fixed element-available() and the implementation of the various
364     chunking methods present, Norm Walsh provided a lot of feedback</li>
365   <li>exclude-result-prefixes and namespaces output should now work as
366     expected</li>
367   <li>added support of embedded stylesheet as described in section 2.7 of the
368     spec</li>
369 </ul>
370
371 <h3>0.14.0: July 5 2001</h3>
372 <ul>
373   <li>lot of bug fixes, and code cleanup</li>
374   <li>completion of the little XSLT-1.0 features left unimplemented</li>
375   <li>Added and implemented the extension API suggested by Thomas Broyer</li>
376   <li>the Windows MSC environment should be complete</li>
377   <li>tested and optimized with a really large document (DocBook Definitive
378     Guide) libxml/libxslt should really be faster on serious workloads</li>
379 </ul>
380
381 <h3>0.13.0: June 26 2001</h3>
382 <ul>
383   <li>lots of cleanups</li>
384   <li>fixed a C++ compilation problem</li>
385   <li>couple of fixes to xsltSaveTo()</li>
386   <li>try to fix Docbook-xslt-1.4 and chunking, updated the regression test
387     with them</li>
388   <li>fixed pattern compilation and priorities problems</li>
389   <li>Patches for Windows and MSC project mostly contributed by Yon Derek</li>
390   <li>update to the Tutorial by John Fleck</li>
391   <li>William fixed bugs in templates and for-each functions</li>
392   <li>added a new interface xsltRunStylesheet() for a more flexible output
393     (incomplete), added -o option to xsltproc</li>
394 </ul>
395
396 <h3>0.12.0: June 18 2001</h3>
397 <ul>
398   <li>fixed a dozen of bugs reported</li>
399   <li>HTML generation should be quite better (requires libxml-2.3.11 upgrade
400     too)</li>
401   <li>William fixed some problems with document()</li>
402   <li>Fix namespace nodes selection and copy (requires libxml-2.3.11 upgrade
403     too)</li>
404   <li>John Fleck added a<a href="tutorial/libxslttutorial.html">
405   tutorial</a></li>
406   <li>Fixes for namespace handling when evaluating variables</li>
407   <li>XInclude global flag added to process XInclude on document() if
408     requested</li>
409   <li>made xsltproc --version more detailed</li>
410 </ul>
411
412 <h3>0.11.0: June 1 2001</h3>
413
414 <p>Mostly a bug fix release.</p>
415 <ul>
416   <li>integration of catalogs from xsltproc</li>
417   <li>added --version to xsltproc for bug reporting</li>
418   <li>fixed errors when handling ID in external parsed entities</li>
419   <li>document() should hopefully work correctly but ...</li>
420   <li>fixed bug with PI and comments processing</li>
421   <li>William fixed the XPath string functions when using unicode</li>
422 </ul>
423
424 <h3>0.10.0: May 19 2001</h3>
425 <ul>
426   <li>cleanups to make stylesheet read-only (not 100% complete)</li>
427   <li>fixed URI resolution in document()</li>
428   <li>force all XPath expression to be compiled at stylesheet parsing time,
429     even if unused ...</li>
430   <li>Fixed HTML default output detection</li>
431   <li>Fixed double attribute generation #54446</li>
432   <li>Fixed {{ handling in attributes #54451</li>
433   <li>More tests and speedups for DocBook document transformations</li>
434   <li>Fixed a really bad race like bug in xsltCopyTreeList()</li>
435   <li>added a documentation on the libxslt internals</li>
436   <li>William Brack and Bjorn Reese improved format-number()</li>
437   <li>Fixed multiple sort, it should really work now</li>
438   <li>added a --docbook option for SGML DocBook input (hackish)</li>
439   <li>a number of other bug fixes and regression test added as people were
440     submitting them</li>
441 </ul>
442
443 <h3>0.9.0: May 3 2001</h3>
444 <ul>
445   <li>lot of various bugfixes, extended the regression suite</li>
446   <li>xsltproc should work with multiple params</li>
447   <li>added an option to use xsltproc with HTML input</li>
448   <li>improved the stylesheet compilation, processing of complex stylesheets
449     should be faster</li>
450   <li>using the same stylesheet for concurrent processing on multithreaded
451     programs should work now</li>
452   <li>fixed another batch of namespace handling problems</li>
453   <li>Implemented multiple level of sorting</li>
454 </ul>
455
456 <h3>0.8.0: Apr 22 2001</h3>
457 <ul>
458   <li>fixed ansidecl.h problem</li>
459   <li>fixed unparsed-entity-uri() and generate-id()</li>
460   <li>sort semantic fixes and priority prob from William M. Brack</li>
461   <li>fixed namespace handling problems in XPath expression computations
462     (requires libxml-2.3.7)</li>
463   <li>fixes to current() and key()</li>
464   <li>other, smaller fixes, lots of testing with N Walsh DocBook HTML
465     stylesheets</li>
466 </ul>
467
468 <h3>0.7.0: Apr 10 2001</h3>
469 <ul>
470   <li>cleanup using stricter compiler flags</li>
471   <li>command line parameter passing</li>
472   <li>fix to xsltApplyTemplates from William M. Brack</li>
473   <li>added the XSLTMark in the regression tests as well as document()</li>
474 </ul>
475
476 <h3>0.6.0: Mar 22 2001</h3>
477 <ul>
478   <li>another beta</li>
479   <li>requires 2.3.5, which provide XPath expression compilation support</li>
480   <li>document() extension should function properly</li>
481   <li>fixed a number or reported bugs</li>
482 </ul>
483
484 <h3>0.5.0: Mar 10 2001</h3>
485 <ul>
486   <li>fifth beta</li>
487   <li>some optimization work, for the moment 2 XSLT transform cannot use the
488     same stylesheet at the same time (to be fixed)</li>
489   <li>fixed problems with handling of tree results</li>
490   <li>fixed a reported strip-spaces problem</li>
491   <li>added more reported/fixed bugs to the test suite</li>
492   <li>incorporated William M. Brack fix for imports and global variables as
493     well as patch for with-param support in apply-templates</li>
494   <li>a bug fix on for-each</li>
495 </ul>
496
497 <h3>0.4.0: Mar 1 2001</h3>
498 <ul>
499   <li>fourth beta test, released at the same time of libxml2-2.3.3</li>
500   <li>bug fixes</li>
501   <li>some optimization</li>
502   <li>started implement extension support, not finished</li>
503   <li>implemented but not tested multiple file output</li>
504 </ul>
505
506 <h3>0.3.0: Feb 24 2001</h3>
507 <ul>
508   <li>third beta test, released at the same time of libxml2-2.3.2</li>
509   <li>lot of bug fixes</li>
510   <li>some optimization</li>
511   <li>added DocBook XSL based testsuite</li>
512 </ul>
513
514 <h3>0.2.0: Feb 15 2001</h3>
515 <ul>
516   <li>second beta version, released at the same time as libxml2-2.3.1</li>
517   <li>getting close to feature completion, lot of bug fixes, some in the HTML
518     and XPath support of libxml</li>
519   <li>start becoming usable for real work. This version can now regenerate
520     the XML 2e HTML from the original XML sources and the associated
521     stylesheets (in <a
522     href="http://www.w3.org/TR/REC-xml#b4d250b6c21">section I of the XML
523     REC</a>)</li>
524   <li>Still misses extension element/function/prefixes support. Support of
525     key() and document() is not complete</li>
526 </ul>
527
528 <h3>0.1.0: Feb 8 2001</h3>
529 <ul>
530   <li>first beta version, released at the same time as libxml2-2.3.0</li>
531   <li>lots of bug fixes, first "testing" version, but incomplete</li>
532 </ul>
533
534 <h3>0.0.1: Jan 25 2001</h3>
535 <ul>
536   <li>first alpha version released at the same time as libxml2-2.2.12</li>
537   <li>Framework in place, should work on simple examples, but far from being
538     feature complete</li>
539 </ul>
540
541 <h2><a name="xsltproc">The xsltproc tool</a></h2>
542
543 <p>This program is the simplest way to use libxslt: from the command line. It
544 is also used for doing the regression tests of the library.</p>
545
546 <p>It takes as first argument the path or URL to an XSLT stylesheet, the next
547 arguments are filenames or URIs of the inputs to be processed. The output of
548 the processing is redirected on the standard output. There is actually a few
549 more options available:</p>
550 <pre>orchis:~ -&gt; xsltproc
551 Usage: xsltproc [options] stylesheet file [file ...]
552    Options:
553       --version or -V: show the version of libxml and libxslt used
554       --verbose or -v: show logs of what's happening
555       --output file or -o file: save to a given file
556       --timing: display the time used
557       --repeat: run the transformation 20 times
558       --debug: dump the tree of the result instead
559       --novalid: skip the Dtd loading phase
560       --noout: do not dump the result
561       --maxdepth val : increase the maximum depth
562       --html: the input document is(are) an HTML file(s)
563       --docbook: the input document is SGML docbook
564       --param name value : pass a (parameter,value) pair
565       --nonet refuse to fetch DTDs or entities over network
566       --warnnet warn against fetching over the network
567       --catalogs : use the catalogs from $SGML_CATALOG_FILES
568       --xinclude : do XInclude processing on document intput
569       --profile or --norman : dump profiling informations 
570 orchis:~ -&gt;</pre>
571
572 <h2><a name="DocBook">DocBook</a></h2>
573
574 <p><a href="http://www.oasis-open.org/committees/docbook/">DocBook</a> is an
575 XML/SGML vocabulary particularly well suited to books and papers about
576 computer hardware and software.</p>
577
578 <p>xsltproc and libxslt are not specifically dependant on DocBook, but since
579 a lot of people use xsltproc and libxml2 for DocBook formatting, here are a
580 few pointers and informations which may be helpful:</p>
581 <ul>
582   <li>The <a href="http://www.oasis-open.org/committees/docbook/">DocBook
583     homepage at Oasis</a> you should find pointers there on all the lastest
584     versions of the DTDs and XSLT stylesheets</li>
585   <li><a href="http://www.docbook.org/">DocBook: The Definitive Guide</a> is
586     the official reference documentation for DocBook.</li>
587   <li>Here is a <a href="/buildDocBookCatalog">shell script</a> to generate
588     XML Catalogs for DocBook 4.1.2 . If it can write to the /etc/xml/
589     directory, it will set-up /etc/xml/catalog and /etc/xml/docbook based on
590     the resources found on the system. Otherwise it will just create
591     ~/xmlcatalog and ~/dbkxmlcatalog and doing:
592     <p><code>export XMLCATALOG=$HOME/xmlcatalog</code></p>
593     <p>should allow to process DocBook documentations without requiring
594     network accesses for the DTd or stylesheets</p>
595   </li>
596   <li>I have uploaded <a href="ftp://xmlsoft.org/test/dbk412catalog.tar.gz">a
597     small tarball</a> containing XML Catalogs for DocBook 4.1.2 which seems
598     to work fine for me too</li>
599   <li>Informations on installing a <a
600     href="http://ourworld.compuserve.com/homepages/hoenicka_markus/cygc2057.html">Windows
601     DocBook processing setup</a> based on Cygwin (using the binaries from the
602     official Windows port should be possible too)</li>
603   <li>Alexander Kirillov's page on <a
604     href="http://www.math.sunysb.edu/~kirillov/dbxml/">Using DocBook XML
605     4.1.2</a> (RPM packages)</li>
606   <li>Tim Waugh's <a href="http://cyberelk.net/tim/xmlto/">xmlto front-end
607     conversion script</a></li>
608   <li>Linux Documentation Project <a
609     href="http://www.linuxdoc.org/HOWTO/mini/DocBook-Install/">
610     DocBook-Install-mini-HOWTO</a></li>
611   <li>ScrollKeeper the open documentation cataloging project has a <a
612     href="http://scrollkeeper.sourceforge.net/docbook.shtml">DocBook
613     section</a></li>
614 </ul>
615
616 <p>Do not use the --docbook option of xsltproc to process XML DocBook
617 documents, this option is only intended to provide some (limited) support of
618 the SGML version of DocBook.</p>
619
620 <p>Points which are not DocBook specific but still worth mentionning
621 again:</p>
622 <ul>
623   <li>if you think DocBook processing time is too slow, make sure you have
624     XML Catalogs pointing to a local installation of the DTD of DocBook.
625     Check the <a href="http://xmlsoft.org/catalog.html">XML Catalog page</a>
626     to understand more on this subject.</li>
627   <li>before processing a new document, use the command
628     <p><code>xmllint --valid --noout path_to_document</code></p>
629     <p>to make sure that your input is valid DocBook. And fixes the errors
630     before processing further. Note that XSLT processing may work correctly
631     with some forms of validity errors left, but in general it can give
632     troubles on output.</p>
633   </li>
634 </ul>
635
636 <h2><a name="API">The programming API</a></h2>
637
638 <p>Okay this section is clearly incomplete. But integrating libxslt into your
639 application should be relatively easy. First check the few steps described
640 below, then for more detailed informations, look at the<a
641 href="html/libxslt-lib.html"> generated pages</a> for the API and the source
642 of libxslt/xsltproc.c  and the  <a
643 href="tutorial/libxslttutorial.html">tutorial</a>.</p>
644
645 <p>Basically doing an XSLT transformation can be done in a few steps:</p>
646 <ol>
647   <li>configure the parser for XSLT:
648     <p>xmlSubstituteEntitiesDefault(1);</p>
649     <p>xmlLoadExtDtdDefaultValue = 1;</p>
650   </li>
651   <li>parse the stylesheet with xsltParseStylesheetFile()</li>
652   <li>parse the document with xmlParseFile()</li>
653   <li>apply the stylesheet using xsltApplyStylesheet()</li>
654   <li>save the result using xsltSaveResultToFile() if needed set
655     xmlIndentTreeOutput to 1</li>
656 </ol>
657
658 <p>Steps 2,3, and 5 will probably need to be changed depending on you
659 processing needs and environment for example if reading/saving from/to
660 memory, or if you want to apply XInclude processing to the stylesheet or
661 input documents.</p>
662
663 <h2><a name="Python">Python and bindings</a></h2>
664
665 <p>There is a number of language bindings and wrappers available for libxml2,
666 the list below is not exhaustive. Please contact the <a
667 href="http://mail.gnome.org/mailman/listinfo/xml-bindings">xml-bindings@gnome.org</a>
668 (<a href="http://mail.gnome.org/archives/xml-bindings/">archives</a>) in
669 order to get updates to this list or to discuss the specific topic of libxml2
670 or libxslt wrappers or bindings:</p>
671 <ul>
672   <li><a
673     href="http://mail.gnome.org/archives/xml/2001-March/msg00014.html">Matt
674     Sergeant</a> developped <a href="http://axkit.org/download/">XML::LibXML
675     and XML::LibXSLT</a>, a perl wrapper for libxml2/libxslt as part of the
676     <a href="http://axkit.com/">AxKit XML application server</a></li>
677   <li><a href="mailto:dkuhlman@cutter.rexx.com">Dave Kuhlman</a> provides and
678     earlier version of the libxml/libxslt <a
679     href="http://www.rexx.com/~dkuhlman">wrappers for Python</a></li>
680   <li>Petr Kozelka provides <a
681     href="http://sourceforge.net/projects/libxml2-pas">Pascal units to glue
682     libxml2</a> with Kylix, Delphi and other Pascal compilers</li>
683   <li>Wai-Sun "Squidster" Chia provides <a
684     href="http://www.rubycolor.org/arc/redist/">bindings for Ruby</a>  and
685     libxml2 bindings are also available in Ruby through the <a
686     href="http://libgdome-ruby.berlios.de/">libgdome-ruby</a> module
687     maintained by Tobias Peters.</li>
688   <li>Steve Ball and contributors maintains <a
689     href="http://tclxml.sourceforge.net/">libxml2 and libxslt bindings for
690     Tcl</a></li>
691 </ul>
692
693 <p>The libxslt Python module depends on the <a
694 href="http://xmlsoft.org/python.html">libxml2 Python</a> module.</p>
695
696 <p>The distribution includes a set of Python bindings, which are garanteed to
697 be maintained as part of the library in the future, though the Python
698 interface have not yet reached the maturity of the C API.</p>
699
700 <p>To install the Python bindings there are 2 options:</p>
701 <ul>
702   <li>If you use an RPM based distribution, simply install the <a
703     href="http://rpmfind.net/linux/rpm2html/search.php?query=libxml2-python">libxml2-python
704     RPM</a> and the <a
705     href="http://rpmfind.net/linux/rpm2html/search.php?query=libxslt-python">libxslt-python
706     RPM</a>.</li>
707   <li>Otherwise use the <a href="ftp://xmlsoft.org/python/">libxml2-python
708     module distribution</a> corresponding to your installed version of
709     libxml2 and libxslt. Note that to install it you will need both libxml2
710     and libxslt installed and run "python setup.py build install" in the
711     module tree.</li>
712 </ul>
713
714 <p>The distribution includes a set of examples and regression tests for the
715 python bindings in the <code>python/tests</code> directory. Here are some
716 excepts from those tests:</p>
717
718 <h3>basic.py:</h3>
719
720 <p>This is a basic test of XSLT interfaces: loading a stylesheet and a
721 document, transforming the document and saving the result.</p>
722 <pre>import libxml2
723 import libxslt
724
725 styledoc = libxml2.parseFile("test.xsl")
726 style = libxslt.parseStylesheetDoc(styledoc)
727 doc = libxml2.parseFile("test.xml")
728 result = style.applyStylesheet(doc, None)
729 style.saveResultToFilename("foo", result, 0)
730 style.freeStylesheet()
731 doc.freeDoc()
732 result.freeDoc()</pre>
733
734 <p>The Python module is called libxslt, you will also need the libxml2 module
735 for the operations on XML trees. Let's have a look at the objects manipulated
736 in that example and how is the processing done:</p>
737 <ul>
738   <li><code>styledoc</code> : is a libxml2 document tree. It is obtained by
739     parsing the XML file "test.xsl" containing the stylesheet.</li>
740   <li><code>style</code> : this is a precompiled stylesheet ready to be used
741     by the following transformations (note the plural form, multiple
742     transformations can resuse the same stylesheet).</li>
743   <li><code>doc</code> : this is the document to apply the transformation to.
744     In this case it is simply generated by parsing it from a file but any
745     other processing is possible as long as one get a libxml2 Doc. Note that
746     HTML tree are suitable for XSLT processing in libxslt. This is actually
747     how this page is generated !</li>
748   <li><code>result</code> : this is a document generated by applying the
749     stylesheet to the document. Note that some of the stylesheet informations
750     may be related to the serialization of that document and as in this
751     example a specific saveResultToFilename() method of the stylesheet should
752     be used to save it to a file (in that case to "foo").</li>
753 </ul>
754
755 <p>Also note the need to explicitely deallocate documents with freeDoc()
756 except for the stylesheet document which is freed when its compiled form is
757 garbage collected.</p>
758
759 <h3>extfunc.py:</h3>
760
761 <p>This one is a far more complex test. It shows how to modify the behaviour
762 of an XSLT transformation by passing parameters and how to extend the XSLT
763 engine with functions defined in python:</p>
764 <pre>import libxml2
765 import libxslt
766 import string
767
768 nodeName = None
769 def f(ctx, str):
770     global nodeName
771
772     #
773     # Small check to verify the context is correcly accessed
774     #
775     try:
776         pctxt = libxslt.xpathParserContext(_obj=ctx)
777         ctxt = pctxt.context()
778         tctxt = ctxt.transformContext()
779         nodeName = tctxt.insertNode().name
780     except:
781         pass
782
783     return string.upper(str)
784
785 libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f)</pre>
786
787 <p>This code defines and register an extension function. Note that the
788 function can be bound to any name (foo) and how the binding is also
789 associated to a namespace name "http://example.com/foo". From an XSLT point
790 of view the function just returns an upper case version of the string passed
791 as a parameter. But the first part of the function also read some contextual
792 information from the current XSLT processing environement, in that case it
793 looks for the current insertion node in the resulting output (either the
794 resulting document or the Result Value Tree being generated), and saves it to
795 a global variable for checking that the access actually worked.</p>
796
797 <p>For more informations on the xpathParserContext and transformContext
798 objects check the <a href="internals.html">libray internals description</a>.
799 The pctxt is actually an object from a class derived from the
800 libxml2.xpathParserContext() with just a couple more properties including the
801 possibility to look up the XSLT transformation context from the XPath
802 context.</p>
803 <pre>styledoc = libxml2.parseDoc("""
804 &lt;xsl:stylesheet version='1.0'
805   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
806   xmlns:foo='http://example.com/foo'
807   xsl:exclude-result-prefixes='foo'&gt;
808
809   &lt;xsl:param name='bar'&gt;failure&lt;/xsl:param&gt;
810   &lt;xsl:template match='/'&gt;
811     &lt;article&gt;&lt;xsl:value-of select='foo:foo($bar)'/&gt;&lt;/article&gt;
812   &lt;/xsl:template&gt;
813 &lt;/xsl:stylesheet&gt;
814 """)</pre>
815
816 <p>Here is a simple example of how to read an XML document from a python
817 string with libxml2. Note how this stylesheet:</p>
818 <ul>
819   <li>Uses a global parameter <code>bar</code></li>
820   <li>Reference the extension function f</li>
821   <li>how the Namespace name "http://example.com/foo" has to be bound to a
822     prefix</li>
823   <li>how that prefix is excluded from the output</li>
824   <li>how the function is called from the select</li>
825 </ul>
826 <pre>style = libxslt.parseStylesheetDoc(styledoc)
827 doc = libxml2.parseDoc("&lt;doc/&gt;")
828 result = style.applyStylesheet(doc, { "bar": "'success'" })
829 style.freeStylesheet()
830 doc.freeDoc()</pre>
831
832 <p>that part is identical, to the basic example except that the
833 transformation is passed a dictionnary of parameters. Note that the string
834 passed "success" had to be quoted, otherwise it is interpreted as an XPath
835 query for the childs of root named "success".</p>
836 <pre>root = result.children
837 if root.name != "article":
838     print "Unexpected root node name"
839     sys.exit(1)
840 if root.content != "SUCCESS":
841     print "Unexpected root node content, extension function failed"
842     sys.exit(1)
843 if nodeName != 'article':
844     print "The function callback failed to access its context"
845     sys.exit(1)
846
847 result.freeDoc()</pre>
848
849 <p>That part just verifies that the transformation worked, that the parameter
850 got properly passed to the engine, that the function f() got called and that
851 it properly accessed the context to find the name of the insertion node.</p>
852
853 <h3>pyxsltproc.py:</h3>
854
855 <p>this module is a bit too long to be described there but it is basically a
856 rewrite of the xsltproc command line interface of libxslt in Python. It
857 provides nearly all the functionalities of xsltproc and can be used as a base
858 module to write Python customized XSLT processors. One of the thing to notice
859 are:</p>
860 <pre>libxml2.lineNumbersDefault(1)
861 libxml2.substituteEntitiesDefault(1)</pre>
862
863 <p>those two calls in the main() function are needed to force the libxml2
864 processor to generate DOM trees compliant with the XPath data model.</p>
865
866 <h2><a name="Internals">Library internals</a></h2>
867
868 <h3>Table  of contents</h3>
869 <ul>
870   <li><a href="internals.html#Introducti">Introduction</a></li>
871   <li><a href="internals.html#Basics">Basics</a></li>
872   <li><a href="internals.html#Keep">Keep it simple stupid</a></li>
873   <li><a href="internals.html#libxml">The libxml nodes</a></li>
874   <li><a href="internals.html#XSLT">The XSLT processing steps</a></li>
875   <li><a href="internals.html#XSLT1">The XSLT stylesheet compilation</a></li>
876   <li><a href="internals.html#XSLT2">The XSLT template compilation</a></li>
877   <li><a href="internals.html#processing">The processing itself</a></li>
878   <li><a href="internals.html#XPath">XPath expressions compilation</a></li>
879   <li><a href="internals.html#XPath1">XPath interpretation</a></li>
880   <li><a href="internals.html#Descriptio">Description of XPath
881   Objects</a></li>
882   <li><a href="internals.html#XPath3">XPath functions</a></li>
883   <li><a href="internals.html#stack">The variables stack frame</a></li>
884   <li><a href="internals.html#Extension">Extension support</a></li>
885   <li><a href="internals.html#Futher">Further reading</a></li>
886   <li><a href="internals.html#TODOs">TODOs</a></li>
887 </ul>
888
889 <h3><a name="Introducti2">Introduction</a></h3>
890
891 <p>This document describes the processing of <a
892 href="http://xmlsoft.org/XSLT/">libxslt</a>, the <a
893 href="http://www.w3.org/TR/xslt">XSLT</a> C library developed for the <a
894 href="http://www.gnome.org/">Gnome</a> project.</p>
895
896 <p>Note: this documentation is by definition incomplete and I am not good at
897 spelling, grammar, so patches and suggestions are <a
898 href="mailto:veillard@redhat.com">really welcome</a>.</p>
899
900 <h3><a name="Basics1">Basics</a></h3>
901
902 <p>XSLT is a transformation language. It takes an input document and a
903 stylesheet document and generates an output document:</p>
904
905 <p align="center"><img src="processing.gif"
906 alt="the XSLT processing model"></p>
907
908 <p>Libxslt is written in C. It relies on <a
909 href="http://www.xmlsoft.org/">libxml</a>, the XML C library for Gnome, for
910 the following operations:</p>
911 <ul>
912   <li>parsing files</li>
913   <li>building the in-memory DOM structure associated with the documents
914     handled</li>
915   <li>the XPath implementation</li>
916   <li>serializing back the result document to XML and HTML. (Text is handled
917     directly.)</li>
918 </ul>
919
920 <h3><a name="Keep1">Keep it simple stupid</a></h3>
921
922 <p>Libxslt is not very specialized. It is built under the assumption that all
923 nodes from the source and output document can fit in the virtual memory of
924 the system. There is a big trade-off there. It is fine for reasonably sized
925 documents but may not be suitable for large sets of data. The gain is that it
926 can be used in a relatively versatile way. The input or output may never be
927 serialized, but the size of documents it can handle are limited by the size
928 of the memory available.</p>
929
930 <p>More specialized memory handling approaches are possible, like building
931 the input tree from a serialization progressively as it is consumed,
932 factoring repetitive patterns, or even on-the-fly generation of the output as
933 the input is parsed but it is possible only for a limited subset of the
934 stylesheets. In general the implementation of libxslt follows the following
935 pattern:</p>
936 <ul>
937   <li>KISS (keep it simple stupid)</li>
938   <li>when there is a clear bottleneck optimize on top of this simple
939     framework and refine only as much as is needed to reach the expected
940     result</li>
941 </ul>
942
943 <p>The result is not that bad, clearly one can do a better job but more
944 specialized too. Most optimization like building the tree on-demand would
945 need serious changes to the libxml XPath framework. An easy step would be to
946 serialize the output directly (or call a set of SAX-like output handler to
947 keep this a flexible interface) and hence avoid the memory consumption of the
948 result.</p>
949
950 <h3><a name="libxml">The libxml nodes</a></h3>
951
952 <p>DOM-like trees, as used and generated by libxml and libxslt, are
953 relatively complex. Most node types follow the given structure except a few
954 variations depending on the node type:</p>
955
956 <p align="center"><img src="node.gif" alt="description of a libxml node"></p>
957
958 <p>Nodes carry a <strong>name</strong> and the node <strong>type</strong>
959 indicates the kind of node it represents, the most common ones are:</p>
960 <ul>
961   <li>document nodes</li>
962   <li>element nodes</li>
963   <li>text nodes</li>
964 </ul>
965
966 <p>For the XSLT processing, entity nodes should not be generated (i.e. they
967 should be replaced by their content). Most nodes also contains the following
968 "navigation" informations:</p>
969 <ul>
970   <li>the containing <strong>doc</strong>ument</li>
971   <li>the <strong>parent</strong> node</li>
972   <li>the first <strong>children</strong> node</li>
973   <li>the <strong>last</strong> children node</li>
974   <li>the <strong>prev</strong>ious sibling</li>
975   <li>the following sibling (<strong>next</strong>)</li>
976 </ul>
977
978 <p>Elements nodes carries the list of attributes in the properties, an
979 attribute itself holds the navigation pointers and the children list (the
980 attribute value is not represented as a simple string to allow usage of
981 entities references).</p>
982
983 <p>The <strong>ns</strong> points to the namespace declaration for the
984 namespace associated to the node, <strong>nsDef</strong> is the linked list
985 of namespace declaration present on element nodes.</p>
986
987 <p>Most nodes also carry an <strong>_private</strong> pointer which can be
988 used by the application to hold specific data on this node.</p>
989
990 <h3><a name="XSLT">The XSLT processing steps</a></h3>
991
992 <p>There are a few steps which are clearly decoupled at the interface
993 level:</p>
994 <ol>
995   <li>parse the stylesheet and generate a DOM tree</li>
996   <li>take the stylesheet tree and build a compiled version of it (the
997     compilation phase)</li>
998   <li>take the input and generate a DOM tree</li>
999   <li>process the stylesheet against the input tree and generate an output
1000     tree</li>
1001   <li>serialize the output tree</li>
1002 </ol>
1003
1004 <p>A few things should be noted here:</p>
1005 <ul>
1006   <li>the steps 1/ 3/ and 5/ are optional</li>
1007   <li>the stylesheet obtained at 2/ can be reused by multiple processing 4/
1008     (and this should also work in threaded programs)</li>
1009   <li>the tree provided in 2/ should never be freed using xmlFreeDoc, but by
1010     freeing the stylesheet.</li>
1011   <li>the input tree 4/ is not modified except the _private field which may
1012     be used for labelling keys if used by the stylesheet</li>
1013 </ul>
1014
1015 <h3><a name="XSLT1">The XSLT stylesheet compilation</a></h3>
1016
1017 <p>This is the second step described. It takes a stylesheet tree, and
1018 "compiles" it. This associates to each node a structure stored in the
1019 _private field and containing information computed in the stylesheet:</p>
1020
1021 <p align="center"><img src="stylesheet.gif"
1022 alt="a compiled XSLT stylesheet"></p>
1023
1024 <p>One xsltStylesheet structure is generated per document parsed for the
1025 stylesheet. XSLT documents allow includes and imports of other documents,
1026 imports are stored in the <strong>imports</strong> list (hence keeping the
1027 tree hierarchy of includes which is very important for a proper XSLT
1028 processing model) and includes are stored in the <strong>doclist</strong>
1029 list. An imported stylesheet has a parent link to allow browsing of the
1030 tree.</p>
1031
1032 <p>The DOM tree associated to the document is stored in <strong>doc</strong>.
1033 It is preprocessed to remove ignorable empty nodes and all the nodes in the
1034 XSLT namespace are subject to precomputing. This usually consist of
1035 extracting all the context information from the context tree (attributes,
1036 namespaces, XPath expressions), and storing them in an xsltStylePreComp
1037 structure associated to the <strong>_private</strong> field of the node.</p>
1038
1039 <p>A couple of notable exceptions to this are XSLT template nodes (more on
1040 this later) and attribute value templates. If they are actually templates,
1041 the value cannot be computed at compilation time. (Some preprocessing could
1042 be done like isolation and preparsing of the XPath subexpressions but it's
1043 not done, yet.)</p>
1044
1045 <p>The xsltStylePreComp structure also allows storing of the precompiled form
1046 of an XPath expression that can be associated to an XSLT element (more on
1047 this later).</p>
1048
1049 <h3><a name="XSLT2">The XSLT template compilation</a></h3>
1050
1051 <p>A proper handling of templates lookup is one of the keys of fast XSLT
1052 processing. (Given a node in the source document this is the process of
1053 finding which templates should be applied to this node.) Libxslt follows the
1054 hint suggested in the <a href="http://www.w3.org/TR/xslt#patterns">5.2
1055 Patterns</a> section of the XSLT Recommendation, i.e. it doesn't evaluate it
1056 as an XPath expression but tokenizes it and compiles it as a set of rules to
1057 be evaluated on a candidate node. There usually is an indication of the node
1058 name in the last step of this evaluation and this is used as a key check for
1059 the match. As a result libxslt builds a relatively more complex set of
1060 structures for the templates:</p>
1061
1062 <p align="center"><img src="templates.gif"
1063 alt="The templates related structure"></p>
1064
1065 <p>Let's describe a bit more closely what is built. First the xsltStylesheet
1066 structure holds a pointer to the template hash table. All the XSLT patterns
1067 compiled in this stylesheet are indexed by the value of the the target
1068 element (or attribute, pi ...) name, so when a element or an attribute "foo"
1069 needs to be processed the lookup is done using the name as a key.</p>
1070
1071 <p>Each of the patterns is compiled into an xsltCompMatch structure. It holds
1072 the set of rules based on the tokenization of the pattern stored in reverse
1073 order (matching is easier this way). It also holds some information about the
1074 previous matches used to speed up the process when one iterates over a set of
1075 siblings. (This optimization may be defeated by trashing when running
1076 threaded computation, it's unclear that this is a big deal in practice.)
1077 Predicate expressions are not compiled at this stage, they may be at run-time
1078 if needed, but in this case they are compiled as full XPath expressions (the
1079 use of some fixed predicate can probably be optimized, they are not yet).</p>
1080
1081 <p>The xsltCompMatch are then stored in the hash table, the clash list is
1082 itself sorted by priority of the template to implement "naturally" the XSLT
1083 priority rules.</p>
1084
1085 <p>Associated to the compiled pattern is the xsltTemplate itself containing
1086 the information required for the processing of the pattern including, of
1087 course, a pointer to the list of elements used for building the pattern
1088 result.</p>
1089
1090 <p>Last but not least a number of patterns do not fit in the hash table
1091 because they are not associated to a name, this is the case for patterns
1092 applying to the root, any element, any attributes, text nodes, pi nodes, keys
1093 etc. Those are stored independently in the stylesheet structure as separate
1094 linked lists of xsltCompMatch.</p>
1095
1096 <h3><a name="processing">The processing itself</a></h3>
1097
1098 <p>The processing is defined by the XSLT specification (the basis of the
1099 algorithm is explained in <a
1100 href="http://www.w3.org/TR/xslt#section-Introduction">the Introduction</a>
1101 section). Basically it works by taking the root of the input document and
1102 applying the following algorithm:</p>
1103 <ol>
1104   <li>Finding the template applying to it. This is a lookup in the template
1105     hash table, walking the hash list until the node satisfies all the steps
1106     of the pattern, then checking the appropriate(s) global templates to see
1107     if there isn't a higher priority rule to apply</li>
1108   <li>If there is no template, apply the default rule (recurse on the
1109     children)</li>
1110   <li>else walk the content list of the selected templates, for each of them:
1111     <ul>
1112       <li>if the node is in the XSLT namespace then the node has a _private
1113         field pointing to the preprocessed values, jump to the specific
1114       code</li>
1115       <li>if the node is in an extension namespace, look up the associated
1116         behavior</li>
1117       <li>otherwise copy the node.</li>
1118     </ul>
1119     <p>The closure is usually done through the XSLT
1120     <strong>apply-templates</strong> construct recursing by applying the
1121     adequate template on the input node children or on the result of an
1122     associated XPath selection lookup.</p>
1123   </li>
1124 </ol>
1125
1126 <p>Note that large parts of the input tree may not be processed by a given
1127 stylesheet and that on the opposite some may be processed multiple times.
1128 (This often is the case when a Table of Contents is built).</p>
1129
1130 <p>The module <code>transform.c</code> is the one implementing most of this
1131 logic. <strong>xsltApplyStylesheet()</strong> is the entry point, it
1132 allocates an xsltTransformContext containing the following:</p>
1133 <ul>
1134   <li>a pointer to the stylesheet being processed</li>
1135   <li>a stack of templates</li>
1136   <li>a stack of variables and parameters</li>
1137   <li>an XPath context</li>
1138   <li>the template mode</li>
1139   <li>current document</li>
1140   <li>current input node</li>
1141   <li>current selected node list</li>
1142   <li>the current insertion points in the output document</li>
1143   <li>a couple of hash tables for extension elements and functions</li>
1144 </ul>
1145
1146 <p>Then a new document gets allocated (HTML or XML depending on the type of
1147 output), the user parameters and global variables and parameters are
1148 evaluated. Then <strong>xsltProcessOneNode()</strong> which implements the
1149 1-2-3 algorithm is called on the root element of the input. Step 1/ is
1150 implemented by calling <strong>xsltGetTemplate()</strong>, step 2/ is
1151 implemented by <strong>xsltDefaultProcessOneNode()</strong> and step 3/ is
1152 implemented by <strong>xsltApplyOneTemplate()</strong>.</p>
1153
1154 <h3><a name="XPath">XPath expression compilation</a></h3>
1155
1156 <p>The XPath support is actually implemented in the libxml module (where it
1157 is reused by the XPointer implementation). XPath is a relatively classic
1158 expression language. The only uncommon feature is that it is working on XML
1159 trees and hence has specific syntax and types to handle them.</p>
1160
1161 <p>XPath expressions are compiled using <strong>xmlXPathCompile()</strong>.
1162 It will take an expression string in input and generate a structure
1163 containing the parsed expression tree, for example the expression:</p>
1164 <pre>/doc/chapter[title='Introduction']</pre>
1165
1166 <p>will be compiled as</p>
1167 <pre>Compiled Expression : 10 elements
1168   SORT
1169     COLLECT  'child' 'name' 'node' chapter
1170       COLLECT  'child' 'name' 'node' doc
1171         ROOT
1172       PREDICATE
1173         SORT
1174           EQUAL =
1175             COLLECT  'child' 'name' 'node' title
1176               NODE
1177             ELEM Object is a string : Introduction
1178               COLLECT  'child' 'name' 'node' title
1179                 NODE</pre>
1180
1181 <p>This can be tested using the  <code>testXPath</code>  command (in the
1182 libxml codebase) using the <code>--tree</code> option.</p>
1183
1184 <p>Again, the KISS approach is used. No optimization is done. This could be
1185 an interesting thing to add. <a
1186 href="http://www-106.ibm.com/developerworks/library/x-xslt2/?dwzone=x?open&amp;l=132%2ct=gr%2c+p=saxon">Michael
1187 Kay describes</a> a lot of possible and interesting optimizations done in
1188 Saxon which would be possible at this level. I'm unsure they would provide
1189 much gain since the expressions tends to be relatively simple in general and
1190 stylesheets are still hand generated. Optimizations at the interpretation
1191 sounds likely to be more efficient.</p>
1192
1193 <h3><a name="XPath1">XPath interpretation</a></h3>
1194
1195 <p>The interpreter is implemented by <strong>xmlXPathCompiledEval()</strong>
1196 which is the front-end to <strong>xmlXPathCompOpEval()</strong> the function
1197 implementing the evaluation of the expression tree. This evaluation follows
1198 the KISS approach again. It's recursive and calls
1199 <strong>xmlXPathNodeCollectAndTest()</strong> to collect nodes set when
1200 evaluating a <code>COLLECT</code> node.</p>
1201
1202 <p>An evaluation is done within the framework of an XPath context stored in
1203 an <strong>xmlXPathContext</strong> structure, in the framework of a
1204 transformation the context is maintained within the XSLT context. Its content
1205 follows the requirements from the XPath specification:</p>
1206 <ul>
1207   <li>the current document</li>
1208   <li>the current node</li>
1209   <li>a hash table of defined variables (but not used by XSLT)</li>
1210   <li>a hash table of defined functions</li>
1211   <li>the proximity position (the place of the node in the current node
1212   list)</li>
1213   <li>the context size (the size of the current node list)</li>
1214   <li>the array of namespace declarations in scope (there also is a namespace
1215     hash table but it is not used in the XSLT transformation).</li>
1216 </ul>
1217
1218 <p>For the purpose of XSLT an <strong>extra</strong> pointer has been added
1219 allowing to retrieve the XSLT transformation context. When an XPath
1220 evaluation is about to be performed, an XPath parser context is allocated
1221 containing and XPath object stack (this is actually an XPath evaluation
1222 context, this is a remain of the time where there was no separate parsing and
1223 evaluation phase in the XPath implementation). Here is an overview of the set
1224 of contexts associated to an XPath evaluation within an XSLT
1225 transformation:</p>
1226
1227 <p align="center"><img src="contexts.gif"
1228 alt="The set of contexts associated "></p>
1229
1230 <p>Clearly this is a bit too complex and confusing and should be refactored
1231 at the next set of binary incompatible releases of libxml. For example the
1232 xmlXPathCtxt has a lot of unused parts and should probably be merged with
1233 xmlXPathParserCtxt.</p>
1234
1235 <h3><a name="Descriptio">Description of XPath Objects</a></h3>
1236
1237 <p>An XPath expression manipulates XPath objects. XPath defines the default
1238 types boolean, numbers, strings and node sets. XSLT adds the result tree
1239 fragment type which is basically an unmodifiable node set.</p>
1240
1241 <p>Implementation-wise, libxml follows again a KISS approach, the
1242 xmlXPathObject is a structure containing a type description and the various
1243 possibilities. (Using an enum could have gained some bytes.) In the case of
1244 node sets (or result tree fragments), it points to a separate xmlNodeSet
1245 object which contains the list of pointers to the document nodes:</p>
1246
1247 <p align="center"><img src="object.gif"
1248 alt="An Node set object pointing to "></p>
1249
1250 <p>The <a href="http://xmlsoft.org/html/libxml-xpath.html">XPath API</a> (and
1251 its <a href="http://xmlsoft.org/html/libxml-xpathinternals.html">'internal'
1252 part</a>) includes a number of functions to create, copy, compare, convert or
1253 free XPath objects.</p>
1254
1255 <h3><a name="XPath3">XPath functions</a></h3>
1256
1257 <p>All the XPath functions available to the interpreter are registered in the
1258 function hash table linked from the XPath context. They all share the same
1259 signature:</p>
1260 <pre>void xmlXPathFunc (xmlXPathParserContextPtr ctxt, int nargs);</pre>
1261
1262 <p>The first argument is the XPath interpretation context, holding the
1263 interpretation stack. The second argument defines the number of objects
1264 passed on the stack for the function to consume (last argument is on top of
1265 the stack).</p>
1266
1267 <p>Basically an XPath function does the following:</p>
1268 <ul>
1269   <li>check <code>nargs</code> for proper handling of errors or functions
1270     with variable numbers of parameters</li>
1271   <li>pop the parameters from the stack using <code>obj =
1272     valuePop(ctxt);</code></li>
1273   <li>do the function specific computation</li>
1274   <li>push the result parameter on the stack using <code>valuePush(ctxt,
1275     res);</code></li>
1276   <li>free up the input parameters with
1277   <code>xmlXPathFreeObject(obj);</code></li>
1278   <li>return</li>
1279 </ul>
1280
1281 <p>Sometime the work can be done directly by modifying in-situ the top object
1282 on the stack <code>ctxt-&gt;value</code>.</p>
1283
1284 <h3><a name="stack">The XSLT variables stack frame</a></h3>
1285
1286 <p>Not to be confused with XPath object stack, this stack holds the XSLT
1287 variables and parameters as they are defined through the recursive calls of
1288 call-template, apply-templates and default templates. This is used to define
1289 the scope of variables being called.</p>
1290
1291 <p>This part seems to be the most urgent attention right now, first it is
1292 done in a very inefficient way since the location of the variables and
1293 parameters within the stylesheet tree is still done at run time (it really
1294 should be done statically at compile time), and I am still unsure that my
1295 understanding of the template variables and parameter scope is actually
1296 right.</p>
1297
1298 <p>This part of the documentation is still to be written once this part of
1299 the code will be stable. <span
1300 style="background-color: #FF0000">TODO</span></p>
1301
1302 <h3><a name="Extension">Extension support</a></h3>
1303
1304 <p>There is a separate document explaining <a href="extensions.html">how the
1305 extension support works</a>.</p>
1306
1307 <h3><a name="Futher">Further reading</a></h3>
1308
1309 <p>Michael Kay wrote <a
1310 href="http://www-106.ibm.com/developerworks/library/x-xslt2/?dwzone=x?open&amp;l=132%2ct=gr%2c+p=saxon">a
1311 really interesting article on Saxon internals</a> and the work he did on
1312 performance issues. I wishes I had read it before starting libxslt design (I
1313 would probably have avoided a few mistakes and progressed faster). A lot of
1314 the ideas in his papers should be implemented or at least tried in
1315 libxslt.</p>
1316
1317 <p>The <a href="http://xmlsoft.org/">libxml documentation</a>, especially <a
1318 href="http://xmlsoft.org/xmlio.html">the I/O interfaces</a> and the <a
1319 href="http://xmlsoft.org/xmlmem.html">memory management</a>.</p>
1320
1321 <h3><a name="TODOs">TODOs</a></h3>
1322
1323 <p>redesign the XSLT stack frame handling. Far too much work is done at
1324 execution time. Similarly for the attribute value templates handling, at
1325 least the embedded subexpressions ought to be precompiled.</p>
1326
1327 <p>Allow output to be saved to a SAX like output (this notion of SAX like API
1328 for output should be added directly to libxml).</p>
1329
1330 <p>Implement and test some of the optimization explained by Michael Kay
1331 especially:</p>
1332 <ul>
1333   <li>static slot allocation on the stack frame</li>
1334   <li>specific boolean interpretation of an XPath expression</li>
1335   <li>some of the sorting optimization</li>
1336   <li>Lazy evaluation of location path. (this may require more changes but
1337     sounds really interesting. XT does this too.)</li>
1338   <li>Optimization of an expression tree (This could be done as a completely
1339     independent module.)</li>
1340 </ul>
1341
1342 <p></p>
1343
1344 <p>Error reporting, there is a lot of case where the XSLT specification
1345 specify that a given construct is an error are not checked adequately by
1346 libxslt. Basically one should do a complete pass on the XSLT spec again and
1347 add all tests to the stylesheet compilation. Using the DTD provided in the
1348 appendix and making direct checks using the libxml validation API sounds a
1349 good idea too (though one should take care of not raising errors for
1350 elements/attributes in different namespaces).</p>
1351
1352 <p>Double check all the places where the stylesheet compiled form might be
1353 modified at run time (extra removal of blanks nodes, hint on the
1354 xsltCompMatch).</p>
1355
1356 <p></p>
1357
1358 <h2><a name="Extensions">Writing extensions</a></h2>
1359
1360 <h3>Table  of content</h3>
1361 <ul>
1362   <li><a href="extensions.html#Introducti">Introduction</a></li>
1363   <li><a href="extensions.html#Basics">Basics</a></li>
1364   <li><a href="extensions.html#Keep">Extension modules</a></li>
1365   <li><a href="extensions.html#Registerin">Registering a module</a></li>
1366   <li><a href="extensions.html#module">Loading a module</a></li>
1367   <li><a href="extensions.html#Registerin1">Registering an extension
1368     function</a></li>
1369   <li><a href="extensions.html#Implementi">Implementing an extension
1370     function</a></li>
1371   <li><a href="extensions.html#Examples">Examples for extension
1372   functions</a></li>
1373   <li><a href="extensions.html#Registerin2">Registering an extension
1374     element</a></li>
1375   <li><a href="extensions.html#Implementi1">Implementing an extension
1376     element</a></li>
1377   <li><a href="extensions.html#Example">Example for extension
1378   elements</a></li>
1379   <li><a href="extensions.html#shutdown">The shutdown of a module</a></li>
1380   <li><a href="extensions.html#Future">Future work</a></li>
1381 </ul>
1382
1383 <h3><a name="Introducti1">Introduction</a></h3>
1384
1385 <p>This document describes the work needed to write extensions to the
1386 standard XSLT library for use with <a
1387 href="http://xmlsoft.org/XSLT/">libxslt</a>, the <a
1388 href="http://www.w3.org/TR/xslt">XSLT</a> C library developed for the <a
1389 href="http://www.gnome.org/">Gnome</a> project.</p>
1390
1391 <p>Before starting reading this document it is highly recommended to get
1392 familiar with <a href="internals.html">the libxslt internals</a>.</p>
1393
1394 <p>Note: this documentation is by definition incomplete and I am not good at
1395 spelling, grammar, so patches and suggestions are <a
1396 href="mailto:veillard@redhat.com">really welcome</a>.</p>
1397
1398 <h3><a name="Basics">Basics</a></h3>
1399
1400 <p>The <a href="http://www.w3.org/TR/xslt">XSLT specification</a> provides
1401 two <a href="http://www.w3.org/TR/xslt">ways to extend an XSLT engine</a>:</p>
1402 <ul>
1403   <li>providing <a href="http://www.w3.org/TR/xslt">new extension
1404     functions</a> which can be called from XPath expressions</li>
1405   <li>providing <a href="http://www.w3.org/TR/xslt">new extension
1406     elements</a> which can be inserted in stylesheets</li>
1407 </ul>
1408
1409 <p>In both cases the extensions need to be associated to a new namespace,
1410 i.e. an URI used as the name for the extension's namespace (there is no need
1411 to have a resource there for this to work).</p>
1412
1413 <p>libxslt provides a few extensions itself, either in libxslt namespace
1414 "http://xmlsoft.org/XSLT/" or in other namespace for well known extensions
1415 provided by other XSLT processors like Saxon, Xalan or XT.</p>
1416
1417 <h3><a name="Keep">Extension modules</a></h3>
1418
1419 <p>Since extensions are bound to a namespace name, usually sets of extensions
1420 coming from a given source are using the same namespace name defining in
1421 practice a group of extensions providing elements, functions or both. From
1422 libxslt point of view those are considered as an "extension module", and most
1423 of the APIs work at a module point of view.</p>
1424
1425 <p>Registration of new functions or elements are bound to the activation of
1426 the module, this is currently done by declaring the namespace as an extension
1427 by using the attribute  <code>extension-element-prefixes</code> on the
1428 <code><a href="http://www.w3.org/TR/xslt">xsl:stylesheet</a></code>
1429 element.</p>
1430
1431 <p>And extension module is defined by 3 objects:</p>
1432 <ul>
1433   <li>the namespace name associated</li>
1434   <li>an initialization function</li>
1435   <li>a shutdown function</li>
1436 </ul>
1437
1438 <h3><a name="Registerin">Registering a module</a></h3>
1439
1440 <p>Currently a libxslt module has to be compiled within the application using
1441 libxslt, there is no code to load dynamically shared libraries associated to
1442 namespace (this may be added but is likely to become a portability
1443 nightmare).</p>
1444
1445 <p>So the current way to register a module is to link the code implementing
1446 it with the application and to call a registration function:</p>
1447 <pre>int xsltRegisterExtModule(const xmlChar *URI,
1448                           xsltExtInitFunction initFunc,
1449                           xsltExtShutdownFunction shutdownFunc);</pre>
1450
1451 <p>The associated header is read by:</p>
1452 <pre>#include&lt;libxslt/extensions.h&gt;</pre>
1453
1454 <p>which also defines the type for the initialization and shutdown
1455 functions</p>
1456
1457 <h3><a name="module">Loading a module</a></h3>
1458
1459 <p>Once the module URI has been registered and if the XSLT processor detects
1460 that a given stylesheet needs the functionalities of an extended module, this
1461 one is initialized.</p>
1462
1463 <p>The xsltExtInitFunction type defines the interface for an initialization
1464 function:</p>
1465 <pre>/**
1466  * xsltExtInitFunction:
1467  * @ctxt:  an XSLT transformation context
1468  * @URI:  the namespace URI for the extension
1469  *
1470  * A function called at initialization time of an XSLT
1471  * extension module
1472  *
1473  * Returns a pointer to the module specific data for this
1474  * transformation
1475  */
1476 typedef void *(*xsltExtInitFunction)(xsltTransformContextPtr ctxt,
1477                                      const xmlChar *URI);</pre>
1478
1479 <p>There are 3 things to notice:</p>
1480 <ul>
1481   <li>the function gets passed the namespace name URI as an argument, this
1482     allow a single function to provide the initialization for multiple
1483     logical modules</li>
1484   <li>it also gets passed a transformation context, the initialization is
1485     done at run time before any processing occurs on the stylesheet but it
1486     will be invoked separately each time for each transformation</li>
1487   <li>it returns a pointer, this can be used to store module specific
1488     informations which can be retrieved later when a function or an element
1489     from the extension are used, an obvious example is a connection to a
1490     database which should be kept and reused along the transformation. NULL
1491     is a perfectly valid return, there is no way to indicate a failure at
1492     this level</li>
1493 </ul>
1494
1495 <p>What this function is expected to do is:</p>
1496 <ul>
1497   <li>prepare the context for this module (like opening the database
1498     connection)</li>
1499   <li>register the extensions specific to this module</li>
1500 </ul>
1501
1502 <h3><a name="Registerin1">Registering an extension function</a></h3>
1503
1504 <p>There is a single call to do this registration:</p>
1505 <pre>int xsltRegisterExtFunction(xsltTransformContextPtr ctxt,
1506                             const xmlChar *name,
1507                             const xmlChar *URI,
1508                             xmlXPathEvalFunc function);</pre>
1509
1510 <p>The registration is bound to a single transformation instance referred by
1511 ctxt, name is the UTF8 encoded name for the NCName of the function, and URI
1512 is the namespace name for the extension (no checking is done, a module could
1513 register functions or elements from a different namespace, but it is not
1514 recommended).</p>
1515
1516 <h3><a name="Implementi">Implementing an extension function</a></h3>
1517
1518 <p>The implementation of the function must have the signature of a libxml
1519 XPath function:</p>
1520 <pre>/**
1521  * xmlXPathEvalFunc:
1522  * @ctxt: an XPath parser context
1523  * @nargs: the number of arguments passed to the function
1524  *
1525  * an XPath evaluation function, the parameters are on the
1526  * XPath context stack
1527  */
1528
1529 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
1530                                  int nargs);</pre>
1531
1532 <p>The context passed to an XPath function is not an XSLT context but an <a
1533 href="internals.html#XPath1">XPath context</a>. However it is possible to
1534 find one from the other:</p>
1535 <ul>
1536   <li>The function xsltXPathGetTransformContext provide this lookup facility:
1537     <pre>xsltTransformContextPtr
1538          xsltXPathGetTransformContext
1539                           (xmlXPathParserContextPtr ctxt);</pre>
1540   </li>
1541   <li>The <code>xmlXPathContextPtr</code> associated to an
1542     <code>xsltTransformContext</code> is stored in the <code>xpathCtxt</code>
1543     field.</li>
1544 </ul>
1545
1546 <p>The first thing an extension function may want to do is to check the
1547 arguments passed on the stack, the <code>nargs</code> will precise how many
1548 of them were provided on the XPath expression. The macros valuePop will
1549 extract them from the XPath stack:</p>
1550 <pre>#include &lt;libxml/xpath.h&gt;
1551 #include &lt;libxml/xpathInternals.h&gt;
1552
1553 xmlXPathObjectPtr obj = valuePop(ctxt); </pre>
1554
1555 <p>Note that <code>ctxt</code> is the XPath context not the XSLT one. It is
1556 then possible to examine the content of the value. Check <a
1557 href="internals.html#Descriptio">the description of XPath objects</a> if
1558 necessary. The following is a common sequcnce checking whether the argument
1559 passed is a string and converting it using the built-in XPath
1560 <code>string()</code> function if this is not the case:</p>
1561 <pre>if (obj-&gt;type != XPATH_STRING) {
1562     valuePush(ctxt, obj);
1563     xmlXPathStringFunction(ctxt, 1);
1564     obj = valuePop(ctxt);
1565 }</pre>
1566
1567 <p>Most common XPath functions are available directly at the C level and are
1568 exported either in <code>&lt;libxml/xpath.h&gt;</code> or in
1569 <code>&lt;libxml/xpathInternals.h&gt;</code>.</p>
1570
1571 <p>The extension function may also need to retrieve the data associated to
1572 this module instance (the database connection in the previous example) this
1573 can be done using the xsltGetExtData:</p>
1574 <pre>void * xsltGetExtData(xsltTransformContextPtr ctxt,
1575                       const xmlChar *URI);</pre>
1576
1577 <p>again the URI to be provided is the one used which was used when
1578 registering the module.</p>
1579
1580 <p>Once the function finishes, don't forget to:</p>
1581 <ul>
1582   <li>push the return value on the stack using <code>valuePush(ctxt,
1583     obj)</code></li>
1584   <li>deallocate the parameters passed to the function using
1585     <code>xmlXPathFreeObject(obj)</code></li>
1586 </ul>
1587
1588 <h3><a name="Examples">Examples for extension functions</a></h3>
1589
1590 <p>The module libxslt/functions.c containsthe sources of the XSLT built-in
1591 functions, including document(), key(), generate-id(), etc. as well as a full
1592 example module at the end. Here is the test function implementation for the
1593 libxslt:test function:</p>
1594 <pre>/**
1595  * xsltExtFunctionTest:
1596  * @ctxt:  the XPath Parser context
1597  * @nargs:  the number of arguments
1598  *
1599  * function libxslt:test() for testing the extensions support.
1600  */
1601 static void
1602 xsltExtFunctionTest(xmlXPathParserContextPtr ctxt, int nargs)
1603 {
1604     xsltTransformContextPtr tctxt;
1605     void *data;
1606
1607     tctxt = xsltXPathGetTransformContext(ctxt);
1608     if (tctxt == NULL) {
1609         xsltGenericError(xsltGenericErrorContext,
1610             "xsltExtFunctionTest: failed to get the transformation context\n");
1611         return;
1612     }
1613     data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
1614     if (data == NULL) {
1615         xsltGenericError(xsltGenericErrorContext,
1616             "xsltExtFunctionTest: failed to get module data\n");
1617         return;
1618     }
1619 #ifdef WITH_XSLT_DEBUG_FUNCTION
1620     xsltGenericDebug(xsltGenericDebugContext,
1621                      "libxslt:test() called with %d args\n", nargs);
1622 #endif
1623 }</pre>
1624
1625 <h3><a name="Registerin2">Registering an extension element</a></h3>
1626
1627 <p>There is a single call to do this registration:</p>
1628 <pre>int xsltRegisterExtElement(xsltTransformContextPtr ctxt,
1629                            const xmlChar *name,
1630                            const xmlChar *URI,
1631                            xsltTransformFunction function);</pre>
1632
1633 <p>It is similar to the mechanism used to register an extension function,
1634 except that the signature of an extension element implementation is
1635 different.</p>
1636
1637 <p>The registration is bound to a single transformation instance referred by
1638 ctxt, name is the UTF8 encoded name for the NCName of the element, and URI is
1639 the namespace name for the extension (no checking is done, a module could
1640 register elements for a different namespace, but it is not recommended).</p>
1641
1642 <h3><a name="Implementi1">Implementing an extension element</a></h3>
1643
1644 <p>The implementation of the element must have the signature of an XSLT
1645 transformation function:</p>
1646 <pre>/** 
1647  * xsltTransformFunction: 
1648  * @ctxt: the XSLT transformation context
1649  * @node: the input node
1650  * @inst: the stylesheet node 
1651  * @comp: the compiled information from the stylesheet 
1652  * 
1653  * signature of the function associated to elements part of the
1654  * stylesheet language like xsl:if or xsl:apply-templates.
1655  */ 
1656 typedef void (*xsltTransformFunction)
1657                           (xsltTransformContextPtr ctxt,
1658                            xmlNodePtr node,
1659                            xmlNodePtr inst,
1660                            xsltStylePreCompPtr comp);</pre>
1661
1662 <p>The first argument is the XSLT transformation context. The second and
1663 third arguments are xmlNodePtr i.e. internal memory <a
1664 href="internals.html#libxml">representation of  XML nodes</a>. They are
1665 respectively <code>node</code> from the the input document being transformed
1666 by the stylesheet and <code>inst</code> the extension element in the
1667 stylesheet. The last argument is <code>comp</code> a pointer to a precompiled
1668 representation of <code>inst</code> but usually for extension function this
1669 value is <code>NULL</code> by default (it could be added and associated to
1670 the instruction in <code>inst-&gt;_private</code>).</p>
1671
1672 <p>The same functions are available from a function implementing an extension
1673 element as in an extension function, including
1674 <code>xsltGetExtData()</code>.</p>
1675
1676 <p>The goal of extension element being usually to enrich the generated
1677 output, it is expected that they will grow the currently generated output
1678 tree, this can be done by grabbing ctxt-&gt;insert which is the current
1679 libxml node being generated (Note this can also be the intermediate value
1680 tree being built for example to initialize a variable, the processing should
1681 be similar). The functions for libxml tree manipulation from <a
1682 href="http://xmlsoft.org/html/libxml-tree.html">&lt;libxml/tree.h&gt;</a> can
1683 be employed to extend or modify the tree, but it is required to preserve the
1684 insertion node and its ancestors since there is existing pointers to those
1685 elements still in use in the XSLT template execution stack.</p>
1686
1687 <h3><a name="Example">Example for extension elements</a></h3>
1688
1689 <p>The module libxslt/transform.c containsthe sources of the XSLT built-in
1690 elements, including xsl:element, xsl:attribute, xsl:if, etc. There is a small
1691 but full example in functions.c providing the implementation for the
1692 libxslt:test element, it will output a comment in the result tree:</p>
1693 <pre>/**
1694  * xsltExtElementTest:
1695  * @ctxt:  an XSLT processing context
1696  * @node:  The current node
1697  * @inst:  the instruction in the stylesheet
1698  * @comp:  precomputed informations
1699  *
1700  * Process a libxslt:test node
1701  */
1702 static void
1703 xsltExtElementTest(xsltTransformContextPtr ctxt, xmlNodePtr node,
1704                    xmlNodePtr inst,
1705                    xsltStylePreCompPtr comp)
1706 {
1707     xmlNodePtr comment;
1708
1709     if (ctxt == NULL) {
1710         xsltGenericError(xsltGenericErrorContext,
1711                          "xsltExtElementTest: no transformation context\n");
1712         return;
1713     }
1714     if (node == NULL) {
1715         xsltGenericError(xsltGenericErrorContext,
1716                          "xsltExtElementTest: no current node\n");
1717         return;
1718     }
1719     if (inst == NULL) {
1720         xsltGenericError(xsltGenericErrorContext,
1721                          "xsltExtElementTest: no instruction\n");
1722         return;
1723     }
1724     if (ctxt-&gt;insert == NULL) {
1725         xsltGenericError(xsltGenericErrorContext,
1726                          "xsltExtElementTest: no insertion point\n");
1727         return;
1728     }
1729     comment =
1730         xmlNewComment((const xmlChar *)
1731                       "libxslt:test element test worked");
1732     xmlAddChild(ctxt-&gt;insert, comment);
1733 }</pre>
1734
1735 <h3><a name="shutdown">The shutdown of a module</a></h3>
1736
1737 <p>When the XSLT processor ends a transformation, the shutdown function (if
1738 it exists) of all the modules initialized are called.The
1739 xsltExtShutdownFunction type defines the interface for a shutdown
1740 function:</p>
1741 <pre>/**
1742  * xsltExtShutdownFunction:
1743  * @ctxt:  an XSLT transformation context
1744  * @URI:  the namespace URI for the extension
1745  * @data:  the data associated to this module
1746  *
1747  * A function called at shutdown time of an XSLT extension module
1748  */
1749 typedef void (*xsltExtShutdownFunction) (xsltTransformContextPtr ctxt,
1750                                          const xmlChar *URI,
1751                                          void *data);</pre>
1752
1753 <p>this is really similar to a module initialization function except a third
1754 argument is passed, it's the value that was returned by the initialization
1755 function. This allow to deallocate resources from the module for example
1756 close the connection to the database to keep the same example.</p>
1757
1758 <h3><a name="Future">Future work</a></h3>
1759
1760 <p>Well some of the pieces missing:</p>
1761 <ul>
1762   <li>a way to load shared libraries to instanciate new modules</li>
1763   <li>a better detection of extension function usage and their registration
1764     without having to use the extension prefix which ought to be reserved to
1765     element extensions.</li>
1766   <li>more examples</li>
1767   <li>implementations of the <a href="http://www.exslt.org/">EXSLT</a> common
1768     extension libraries, Thomas Broyer nearly finished implementing them.</li>
1769 </ul>
1770
1771 <p></p>
1772
1773 <h2><a name="Contributi">Contributions</a></h2>
1774 <ul>
1775   <li>Bjorn Reese is the author of the number support and worked on the
1776     XSLTMark support</li>
1777   <li>William Brack was an early adopted, contributed a number of patches and
1778     spent quite some time debugging non-trivial problems in early versions of
1779     libxslt</li>
1780   <li><a href="mailto:izlatkovic@daenet.de">Igor  Zlatkovic</a> is now the
1781     maintainer of the Windows port, <a
1782     href="http://www.fh-frankfurt.de/~igor/projects/libxml/index.html">he
1783     provides binaries</a></li>
1784   <li>Thomas Broyer provided a lot of suggestions, and drafted most of the
1785     extension API</li>
1786   <li>John Fleck maintains <a href="tutorial/libxslttutorial.html">a tutorial
1787     for libxslt</a></li>
1788   <li><a
1789     href="http://mail.gnome.org/archives/xml/2001-March/msg00014.html">Matt
1790     Sergeant</a> developed <a
1791     href="http://axkit.org/download/">XML::LibXSLT</a>, a perl wrapper for
1792     libxml2/libxslt as part of the <a href="http://axkit.com/">AxKit XML
1793     application server</a></li>
1794   <li>there is a module for <a
1795     href="http://acs-misc.sourceforge.net/nsxml.html">libxml/libxslt support
1796     in OpenNSD/AOLServer</a></li>
1797   <li><a href="mailto:dkuhlman@cutter.rexx.com">Dave Kuhlman</a> provides
1798     libxml/libxslt <a href="http://www.rexx.com/~dkuhlman">wrappers for
1799     Python</a></li>
1800   <li>Steve Ball and contributors maintains <a
1801     href="http://tclxml.sourceforge.net/">libxml2 and libxslt bindings for
1802     Tcl</a></li>
1803 </ul>
1804
1805 <p>I'm still waiting for someone to contribute a simple XSLT processing
1806 module for Apache :-)</p>
1807
1808 <p></p>
1809
1810 <p><a href="mailto:daniel@veillard.com">Daniel Veillard</a></p>
1811 </body>
1812 </html>