source: trunk/third/libxslt/doc/python.html @ 19102

Revision 19102, 14.6 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r19101, which included commits to RCS files with non-trunk default branches.
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5<style type="text/css"><!--
6TD {font-family: Verdana,Arial,Helvetica}
7BODY {font-family: Verdana,Arial,Helvetica; margin-top: 2em; margin-left: 0em; margin-right: 0em}
8H1 {font-family: Verdana,Arial,Helvetica}
9H2 {font-family: Verdana,Arial,Helvetica}
10H3 {font-family: Verdana,Arial,Helvetica}
11A:link, A:visited, A:active { text-decoration: underline }
12--></style>
13<title>Python and bindings</title>
14</head>
15<body bgcolor="#8b7765" text="#000000" link="#000000" vlink="#000000">
16<table border="0" width="100%" cellpadding="5" cellspacing="0" align="center"><tr>
17<td width="100">
18<a href="http://www.gnome.org/"><img src="gnome2.png" alt="Gnome2 Logo"></a><a href="http://www.redhat.com"><img src="redhat.gif" alt="Red Hat Logo"></a><div align="left"><a href="http://xmlsoft.org/XSLT/"><img src="Libxslt-Logo-180x168.gif" alt="Made with Libxslt Logo"></a></div>
19</td>
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>Python and bindings</h2>
23</td></tr></table></td></tr></table></td>
24</tr></table>
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">
30<form action="search.php" enctype="application/x-www-form-urlencoded" method="GET">
31<input name="query" type="TEXT" size="20" value=""><input name="submit" type="submit" value="Search ...">
32</form>
33<ul>
34<li><a href="index.html">Home</a></li>
35<li><a href="intro.html">Introduction</a></li>
36<li><a href="docs.html">Documentation</a></li>
37<li><a href="bugs.html">Reporting bugs and getting help</a></li>
38<li><a href="help.html">How to help</a></li>
39<li><a href="downloads.html">Downloads</a></li>
40<li><a href="FAQ.html">FAQ</a></li>
41<li><a href="news.html">News</a></li>
42<li><a href="xsltproc2.html">The xsltproc tool</a></li>
43<li><a href="docbook.html">DocBook</a></li>
44<li><a href="API.html">The programming API</a></li>
45<li><a href="python.html">Python and bindings</a></li>
46<li><a href="internals.html">Library internals</a></li>
47<li><a href="extensions.html">Writing extensions</a></li>
48<li><a href="contribs.html">Contributions</a></li>
49<li>
50<a href="xslt.html">flat page</a>, <a href="site.xsl">stylesheet</a>
51</li>
52</ul>
53</td></tr>
54</table>
55<table width="100%" border="0" cellspacing="1" cellpadding="3">
56<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
57<tr><td bgcolor="#fffacd"><ul>
58<li><a href="tutorial/libxslttutorial.html">Tutorial</a></li>
59<li><a href="xsltproc.html">Man page for xsltproc</a></li>
60<li><a href="http://mail.gnome.org/archives/xslt/">Mail archive</a></li>
61<li><a href="http://xmlsoft.org/">XML libxml</a></li>
62<li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
63<li><a href="ftp://xmlsoft.org/">FTP</a></li>
64<li><a href="http://www.zlatkovic.com/projects/libxml/">Windows binaries</a></li>
65<li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
66<li><a href="http://www.zveno.com/open_source/libxml2xslt.html">MacOsX binaries</a></li>
67<li><a href="http://sourceforge.net/projects/libxml2-pas/">Pascal bindings</a></li>
68<li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxslt">Bug Tracker</a></li>
69<li><a href="http://xsldbg.sourceforge.net/">Xsldbg Debugger</a></li>
70<li><a href="http://www.mod-xslt.com/mod-xslt/">Apache module</a></li>
71</ul></td></tr>
72</table>
73<table width="100%" border="0" cellspacing="1" cellpadding="3">
74<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>API Indexes</b></center></td></tr>
75<tr><td bgcolor="#fffacd"><ul>
76<li><a href="APIchunk0.html">Alphabetic</a></li>
77<li><a href="APIconstructors.html">Constructors</a></li>
78<li><a href="APIfunctions.html">Functions/Types</a></li>
79<li><a href="APIfiles.html">Modules</a></li>
80<li><a href="APIsymbols.html">Symbols</a></li>
81</ul></td></tr>
82</table>
83</td></tr></table></td>
84<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">
85<p>There is a number of language bindings and wrappers available for libxml2,
86the list below is not exhaustive. Please contact the <a href="http://mail.gnome.org/mailman/listinfo/xml-bindings">xml-bindings@gnome.org</a>
87(<a href="http://mail.gnome.org/archives/xml-bindings/">archives</a>) in
88order to get updates to this list or to discuss the specific topic of libxml2
89or libxslt wrappers or bindings:</p>
90<ul>
91<li>
92<a href="http://mail.gnome.org/archives/xml/2001-March/msg00014.html">Matt
93    Sergeant</a> developped <a href="http://axkit.org/download/">XML::LibXML
94    and XML::LibXSLT</a>, a perl wrapper for libxml2/libxslt as part of the
95    <a href="http://axkit.com/">AxKit XML application server</a>
96</li>
97  <li>
98<a href="mailto:dkuhlman@cutter.rexx.com">Dave Kuhlman</a> provides and
99    earlier version of the libxml/libxslt <a href="http://www.rexx.com/~dkuhlman">wrappers for Python</a>
100</li>
101  <li>Petr Kozelka provides <a href="http://sourceforge.net/projects/libxml2-pas">Pascal units to glue
102    libxml2</a> with Kylix, Delphi and other Pascal compilers</li>
103  <li>Wai-Sun &quot;Squidster&quot; Chia provides <a href="http://www.rubycolor.org/arc/redist/">bindings for Ruby</a>  and
104    libxml2 bindings are also available in Ruby through the <a href="http://libgdome-ruby.berlios.de/">libgdome-ruby</a> module
105    maintained by Tobias Peters.</li>
106  <li>Steve Ball and contributors maintains <a href="http://tclxml.sourceforge.net/">libxml2 and libxslt bindings for
107    Tcl</a>
108</li>
109  <li>
110<a href="mailto:xmlwrapp@pmade.org">Peter Jones</a> maintains C++
111    bindings for libxslt within <a href="http://pmade.org/pjones/software/xmlwrapp/">xmlwrapp</a>
112</li>
113  <li>
114<a href="phillim2@comcast.net">Mike Phillips</a> provides a module
115    using <a href="http://siasl.dyndns.org/projects/projects.html">libxslt
116    for PHP</a>.</li>
117</ul>
118<p>The libxslt Python module depends on the <a href="http://xmlsoft.org/python.html">libxml2 Python</a> module.</p>
119<p>The distribution includes a set of Python bindings, which are garanteed to
120be maintained as part of the library in the future, though the Python
121interface have not yet reached the maturity of the C API.</p>
122<p>
123<a href="mailto:stephane.bidoul@softwareag.com">Stéphane Bidoul</a>
124maintains <a href="http://users.skynet.be/sbi/libxml-python/">a Windows port
125of the Python bindings</a>.</p>
126<p>Note to people interested in building bindings, the API is formalized as
127<a href="libxslt-api.xml">an XML API description file</a> which allows to
128automate a large part of the Python bindings, this includes function
129descriptions, enums, structures, typedefs, etc... The Python script used to
130build the bindings is python/generator.py in the source distribution.</p>
131<p>To install the Python bindings there are 2 options:</p>
132<ul>
133<li>If you use an RPM based distribution, simply install the <a href="http://rpmfind.net/linux/rpm2html/search.php?query=libxml2-python">libxml2-python
134    RPM</a> and the <a href="http://rpmfind.net/linux/rpm2html/search.php?query=libxslt-python">libxslt-python
135    RPM</a>.</li>
136  <li>Otherwise use the <a href="ftp://xmlsoft.org/python/">libxml2-python
137    module distribution</a> corresponding to your installed version of
138    libxml2 and libxslt. Note that to install it you will need both libxml2
139    and libxslt installed and run &quot;python setup.py build install&quot; in the
140    module tree.</li>
141</ul>
142<p>The distribution includes a set of examples and regression tests for the
143python bindings in the <code>python/tests</code> directory. Here are some
144excepts from those tests:</p>
145<h3>basic.py:</h3>
146<p>This is a basic test of XSLT interfaces: loading a stylesheet and a
147document, transforming the document and saving the result.</p>
148<pre>import libxml2
149import libxslt
150
151styledoc = libxml2.parseFile(&quot;test.xsl&quot;)
152style = libxslt.parseStylesheetDoc(styledoc)
153doc = libxml2.parseFile(&quot;test.xml&quot;)
154result = style.applyStylesheet(doc, None)
155style.saveResultToFilename(&quot;foo&quot;, result, 0)
156style.freeStylesheet()
157doc.freeDoc()
158result.freeDoc()</pre>
159<p>The Python module is called libxslt, you will also need the libxml2 module
160for the operations on XML trees. Let's have a look at the objects manipulated
161in that example and how is the processing done:</p>
162<ul>
163<li>
164<code>styledoc</code> : is a libxml2 document tree. It is obtained by
165    parsing the XML file &quot;test.xsl&quot; containing the stylesheet.</li>
166  <li>
167<code>style</code> : this is a precompiled stylesheet ready to be used
168    by the following transformations (note the plural form, multiple
169    transformations can resuse the same stylesheet).</li>
170  <li>
171<code>doc</code> : this is the document to apply the transformation to.
172    In this case it is simply generated by parsing it from a file but any
173    other processing is possible as long as one get a libxml2 Doc. Note that
174    HTML tree are suitable for XSLT processing in libxslt. This is actually
175    how this page is generated !</li>
176  <li>
177<code>result</code> : this is a document generated by applying the
178    stylesheet to the document. Note that some of the stylesheet informations
179    may be related to the serialization of that document and as in this
180    example a specific saveResultToFilename() method of the stylesheet should
181    be used to save it to a file (in that case to &quot;foo&quot;).</li>
182</ul>
183<p>Also note the need to explicitely deallocate documents with freeDoc()
184except for the stylesheet document which is freed when its compiled form is
185garbage collected.</p>
186<h3>extfunc.py:</h3>
187<p>This one is a far more complex test. It shows how to modify the behaviour
188of an XSLT transformation by passing parameters and how to extend the XSLT
189engine with functions defined in python:</p>
190<pre>import libxml2
191import libxslt
192import string
193
194nodeName = None
195def f(ctx, str):
196    global nodeName
197
198    #
199    # Small check to verify the context is correcly accessed
200    #
201    try:
202        pctxt = libxslt.xpathParserContext(_obj=ctx)
203        ctxt = pctxt.context()
204        tctxt = ctxt.transformContext()
205        nodeName = tctxt.insertNode().name
206    except:
207        pass
208
209    return string.upper(str)
210
211libxslt.registerExtModuleFunction(&quot;foo&quot;, &quot;http://example.com/foo&quot;, f)</pre>
212<p>This code defines and register an extension function. Note that the
213function can be bound to any name (foo) and how the binding is also
214associated to a namespace name &quot;http://example.com/foo&quot;. From an XSLT point
215of view the function just returns an upper case version of the string passed
216as a parameter. But the first part of the function also read some contextual
217information from the current XSLT processing environement, in that case it
218looks for the current insertion node in the resulting output (either the
219resulting document or the Result Value Tree being generated), and saves it to
220a global variable for checking that the access actually worked.</p>
221<p>For more informations on the xpathParserContext and transformContext
222objects check the <a href="internals.html">libray internals description</a>.
223The pctxt is actually an object from a class derived from the
224libxml2.xpathParserContext() with just a couple more properties including the
225possibility to look up the XSLT transformation context from the XPath
226context.</p>
227<pre>styledoc = libxml2.parseDoc(&quot;&quot;&quot;
228&lt;xsl:stylesheet version='1.0'
229  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
230  xmlns:foo='http://example.com/foo'
231  xsl:exclude-result-prefixes='foo'&gt;
232
233  &lt;xsl:param name='bar'&gt;failure&lt;/xsl:param&gt;
234  &lt;xsl:template match='/'&gt;
235    &lt;article&gt;&lt;xsl:value-of select='foo:foo($bar)'/&gt;&lt;/article&gt;
236  &lt;/xsl:template&gt;
237&lt;/xsl:stylesheet&gt;
238&quot;&quot;&quot;)</pre>
239<p>Here is a simple example of how to read an XML document from a python
240string with libxml2. Note how this stylesheet:</p>
241<ul>
242<li>Uses a global parameter <code>bar</code>
243</li>
244  <li>Reference the extension function f</li>
245  <li>how the Namespace name &quot;http://example.com/foo&quot; has to be bound to a
246    prefix</li>
247  <li>how that prefix is excluded from the output</li>
248  <li>how the function is called from the select</li>
249</ul>
250<pre>style = libxslt.parseStylesheetDoc(styledoc)
251doc = libxml2.parseDoc(&quot;&lt;doc/&gt;&quot;)
252result = style.applyStylesheet(doc, { &quot;bar&quot;: &quot;'success'&quot; })
253style.freeStylesheet()
254doc.freeDoc()</pre>
255<p>that part is identical, to the basic example except that the
256transformation is passed a dictionnary of parameters. Note that the string
257passed &quot;success&quot; had to be quoted, otherwise it is interpreted as an XPath
258query for the childs of root named &quot;success&quot;.</p>
259<pre>root = result.children
260if root.name != &quot;article&quot;:
261    print &quot;Unexpected root node name&quot;
262    sys.exit(1)
263if root.content != &quot;SUCCESS&quot;:
264    print &quot;Unexpected root node content, extension function failed&quot;
265    sys.exit(1)
266if nodeName != 'article':
267    print &quot;The function callback failed to access its context&quot;
268    sys.exit(1)
269
270result.freeDoc()</pre>
271<p>That part just verifies that the transformation worked, that the parameter
272got properly passed to the engine, that the function f() got called and that
273it properly accessed the context to find the name of the insertion node.</p>
274<h3>pyxsltproc.py:</h3>
275<p>this module is a bit too long to be described there but it is basically a
276rewrite of the xsltproc command line interface of libxslt in Python. It
277provides nearly all the functionalities of xsltproc and can be used as a base
278module to write Python customized XSLT processors. One of the thing to notice
279are:</p>
280<pre>libxml2.lineNumbersDefault(1)
281libxml2.substituteEntitiesDefault(1)</pre>
282<p>those two calls in the main() function are needed to force the libxml2
283processor to generate DOM trees compliant with the XPath data model.</p>
284<p><a href="bugs.html">Daniel Veillard</a></p>
285</td></tr></table></td></tr></table></td></tr></table></td>
286</tr></table></td></tr></table>
287</body>
288</html>
Note: See TracBrowser for help on using the repository browser.