xml.html revision 9ae4b7afa98d1904d786f5e10db49ada8a2945ad
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 XML C library for Gnome</title>
6  <meta name="GENERATOR" content="amaya V5.0">
7  <meta http-equiv="Content-Type" content="text/html">
8</head>
9
10<body bgcolor="#ffffff">
11<h1 align="center">The XML C library for Gnome</h1>
12
13<h1>Note: this is the flat content of the <a href="index.html">web
14site</a></h1>
15
16<h1 style="text-align: center">libxml, a.k.a. gnome-xml</h1>
17
18<p></p>
19
20<p>Libxml is the XML C library developped for the Gnome project.  XML itself
21is a metalanguage to design markup languages, i.e. text language where
22semantic and structure are added to the content using extra "markup"
23information enclosed between angle bracket. HTML is the most well-known
24markup language.</p>
25
26<p>Libxml2 implements a number of existing standards related to markup
27languages:</p>
28<ul>
29  <li>the XML standard: <a
30    href="http://www.w3.org/TR/REC-xml">http://www.w3.org/TR/REC-xml</a></li>
31  <li>Namespaces in XML: <a
32    href="http://www.w3.org/TR/REC-xml-names/">http://www.w3.org/TR/REC-xml-names/</a></li>
33  <li>XML Base: <a
34    href="http://www.w3.org/TR/xmlbase/">http://www.w3.org/TR/xmlbase/</a></li>
35  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc2396.txt">RFC 2396</a>
36     : Uniform Resource Identifiers <a
37    href="http://www.ietf.org/rfc/rfc2396.txt">http://www.ietf.org/rfc/rfc2396.txt</a></li>
38  <li>XML Path Language (XPath) 1.0: <a
39    href="http://www.w3.org/TR/xpath">http://www.w3.org/TR/xpath</a></li>
40  <li>HTML4 parser: <a
41    href="http://www.w3.org/TR/html401/">http://www.w3.org/TR/html401/</a></li>
42  <li>most of XML Pointer Language (XPointer) Version 1.0: <a
43    href="http://www.w3.org/TR/xptr">http://www.w3.org/TR/xptr</a></li>
44  <li>XML Inclusions (XInclude) Version 1.0: <a
45    href="http://www.w3.org/TR/xinclude/">http://www.w3.org/TR/xinclude/</a></li>
46  <li>[ISO-8859-1], <a
47    href="http://www.cis.ohio-state.edu/rfc/rfc2044.txt">rfc2044</a> [UTF-8]
48    and <a href="http://www.cis.ohio-state.edu/rfc/rfc2781.txt">rfc2781</a>
49    [UTF-16] core encodings</li>
50  <li>part of SGML Open Technical Resolution TR9401:1997</li>
51  <li>XML Catalogs Working Draft 06 August 2001: <a
52    href="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html">http://www.oasis-open.org/committees/entity/spec-2001-08-06.html</a></li>
53</ul>
54
55<p>In most cases libxml tries to implement the specifications in a relatively
56strict way. To some extent libxml2 provide some support for the following
57other specification but don't claim to implement them:</p>
58<ul>
59  <li>Document Object Model (DOM) <a
60    href="http://www.w3.org/TR/DOM-Level-2-Core/">http://www.w3.org/TR/DOM-Level-2-Core/</a>
61    it doesn't implement the API itself, gdome2 does this in top of
62  libxml2</li>
63  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc959.txt">RFC 959</a>
64     : libxml implements a basic FTP client code</li>
65  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc1945.txt">RFC 1945</a>
66     : HTTP/1.0, again a basic HTTP client code</li>
67  <li>SAX: a minimal SAX implementation compatible with early expat
68  versions</li>
69  <li>DocBook SGML v4: libxml2 includes a hackish parser to transition to
70  XML</li>
71</ul>
72
73<p></p>
74
75<p>Separate documents:</p>
76<ul>
77  <li><a href="http://xmlsoft.org/XSLT/">the libxslt page</a>
78     providing an implementation of XSLT 1.0 and extensions on top of
79  libxml2</li>
80  <li><a href="http://www.cs.unibo.it/~casarini/gdome2/">the gdome2 page</a>
81     : a standard DOM2 implementation based on libxml2</li>
82</ul>
83
84<h2><a name="Introducti">Introduction</a></h2>
85
86<p>This document describes libxml, the <a
87href="http://www.w3.org/XML/">XML</a> C library developped for the <a
88href="http://www.gnome.org/">Gnome</a> project. <a
89href="http://www.w3.org/XML/">XML is a standard</a> for building tag-based
90structured documents/data.</p>
91
92<p>Here are some key points about libxml:</p>
93<ul>
94  <li>Libxml exports Push and Pull type parser interfaces for both XML and
95    HTML.</li>
96  <li>Libxml can do DTD validation at parse time, using a parsed document
97    instance, or with an arbitrary DTD.</li>
98  <li>Libxml now includes nearly complete <a
99    href="http://www.w3.org/TR/xpath">XPath</a>, <a
100    href="http://www.w3.org/TR/xptr">XPointer</a> and <a
101    href="http://www.w3.org/TR/xinclude">XInclude</a> implementations.</li>
102  <li>It is written in plain C, making as few assumptions as possible, and
103    sticking closely to ANSI C/POSIX for easy embedding. Works on
104    Linux/Unix/Windows, ported to a number of other platforms.</li>
105  <li>Basic support for HTTP and FTP client allowing aplications to fetch
106    remote resources</li>
107  <li>The design is modular, most of the extensions can be compiled out.</li>
108  <li>The internal document repesentation is as close as possible to the <a
109    href="http://www.w3.org/DOM/">DOM</a> interfaces.</li>
110  <li>Libxml also has a <a href="http://www.megginson.com/SAX/index.html">SAX
111    like interface</a>; the interface is designed to be compatible with <a
112    href="http://www.jclark.com/xml/expat.html">Expat</a>.</li>
113  <li>This library is released both under the <a
114    href="http://www.w3.org/Consortium/Legal/copyright-software-19980720.html">W3C
115    IPR</a> and the <a href="http://www.gnu.org/copyleft/lesser.html">GNU
116    LGPL</a>. Use either at your convenience, basically this should make
117    everybody happy, if not, drop me a mail.</li>
118</ul>
119
120<p>Warning: unless you are forced to because your application links with a
121Gnome library requiring it,  <strong><span
122style="background-color: #FF0000">Do Not Use libxml1</span></strong>, use
123libxml2</p>
124
125<h2><a name="FAQ">FAQ</a></h2>
126
127<p>Table of Content:</p>
128<ul>
129  <li><a href="FAQ.html#Licence">Licence(s)</a></li>
130  <li><a href="FAQ.html#Installati">Installation</a></li>
131  <li><a href="FAQ.html#Compilatio">Compilation</a></li>
132  <li><a href="FAQ.html#Developer">Developer corner</a></li>
133</ul>
134
135<h3><a name="Licence">Licence</a>(s)</h3>
136<ol>
137  <li><em>Licensing Terms for libxml</em>
138    <p>libxml is released under 2 (compatible) licences:</p>
139    <ul>
140      <li>the <a href="http://www.gnu.org/copyleft/lgpl.html">LGPL</a>: GNU
141        Library General Public License</li>
142      <li>the <a
143        href="http://www.w3.org/Consortium/Legal/copyright-software-19980720.html">W3C
144        IPR</a>: very similar to the XWindow licence</li>
145    </ul>
146  </li>
147  <li><em>Can I embed libxml in a proprietary application ?</em>
148    <p>Yes. The W3C IPR allows you to also keep proprietary the changes you
149    made to libxml, but it would be graceful to provide back bugfixes and
150    improvements as patches for possible incorporation in the main
151    development tree</p>
152  </li>
153</ol>
154
155<h3><a name="Installati">Installation</a></h3>
156<ol>
157  <li>Unless you are forced to because your application links with a Gnome
158    library requiring it,  <strong><span style="background-color: #FF0000">Do
159    Not Use libxml1</span></strong>, use libxml2</li>
160  <li><em>Where can I get libxml</em>
161     ?
162    <p>The original distribution comes from <a
163    href="ftp://rpmfind.net/pub/libxml/">rpmfind.net</a> or <a
164    href="ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/">gnome.org</a></p>
165    <p>Most linux and Bsd distribution includes libxml, this is probably the
166    safer way for end-users</p>
167    <p>David Doolin provides precompiled Windows versions at <a
168    href="http://www.ce.berkeley.edu/~doolin/code/libxmlwin32/         ">http://www.ce.berkeley.edu/~doolin/code/libxmlwin32/</a></p>
169  </li>
170  <li><em>I see libxml and libxml2 releases, which one should I install ?</em>
171    <ul>
172      <li>If you are not concerned by any existing backward compatibility
173        with existing application, install libxml2 only</li>
174      <li>If you are not doing development, you can safely install both.
175        usually the packages <a
176        href="http://rpmfind.net/linux/RPM/libxml.html">libxml</a> and <a
177        href="http://rpmfind.net/linux/RPM/libxml2.html">libxml2</a> are
178        compatible (this is not the case for development packages)</li>
179      <li>If you are a developer and your system provides separate packaging
180        for shared libraries and the development components, it is possible
181        to install libxml and libxml2, and also <a
182        href="http://rpmfind.net/linux/RPM/libxml-devel.html">libxml-devel</a>
183        and <a
184        href="http://rpmfind.net/linux/RPM/libxml2-devel.html">libxml2-devel</a>
185        too for libxml2 &gt;= 2.3.0</li>
186      <li>If you are developing a new application, please develop against
187        libxml2(-devel)</li>
188    </ul>
189  </li>
190  <li><em>I can't install the libxml package it conflicts with libxml0</em>
191    <p>You probably have an old libxml0 package used to provide the shared
192    library for libxml.so.0, you can probably safely remove it. Anyway the
193    libxml packages provided on <a
194    href="ftp://rpmfind.net/pub/libxml/">rpmfind.net</a> provides
195    libxml.so.0</p>
196  </li>
197  <li><em>I can't install the libxml(2) RPM package due to failed
198    dependancies</em>
199    <p>The most generic solution is to refetch the latest src.rpm , and
200    rebuild it locally with</p>
201    <p><code>rpm --rebuild libxml(2)-xxx.src.rpm</code></p>
202    <p>if everything goes well it will generate two binary rpm (one providing
203    the shared libs and xmllint, and the other one, the -devel package
204    providing includes, static libraries and scripts needed to build
205    applications with libxml(2)) that you can install locally.</p>
206  </li>
207</ol>
208
209<h3><a name="Compilatio">Compilation</a></h3>
210<ol>
211  <li><em>What is the process to compile libxml ?</em>
212    <p>As most UNIX libraries libxml follows the "standard":</p>
213    <p><code>gunzip -c xxx.tar.gz | tar xvf -</code></p>
214    <p><code>cd libxml-xxxx</code></p>
215    <p><code>/configure --help</code></p>
216    <p>to see the options, then the compilation/installation proper</p>
217    <p><code>/configure [possible options]</code></p>
218    <p><code>make</code></p>
219    <p><code>make install</code></p>
220    <p>At that point you may have to rerun ldconfig or similar utility to
221    update your list of installed shared libs.</p>
222  </li>
223  <li><em>What other libraries are needed to compile/install libxml ?</em>
224    <p>Libxml does not requires any other library, the normal C ANSI API
225    should be sufficient (please report any violation to this rule you may
226    find).</p>
227    <p>However if found at configuration time libxml will detect and use the
228    following libs:</p>
229    <ul>
230      <li><a href="http://www.info-zip.org/pub/infozip/zlib/">libz</a>
231         : a highly portable and available widely compression library</li>
232      <li>iconv: a powerful character encoding conversion library. It's
233        included by default on recent glibc libraries, so it doesn't need to
234        be installed specifically on linux. It seems it's now <a
235        href="http://www.opennc.org/onlinepubs/7908799/xsh/iconv.html">part
236        of the official UNIX</a> specification. Here is one <a
237        href="http://clisp.cons.org/~haible/packages-libiconv.html">implementation
238        of the library</a> which source can be found <a
239        href="ftp://ftp.ilog.fr/pub/Users/haible/gnu/">here</a>.</li>
240    </ul>
241  </li>
242  <li><em>make check fails on some platforms</em>
243    <p>Sometime the regression tests results don't completely match the value
244    produced by the parser, and the makefile uses diff to print the delta. On
245    some platforms the diff return breaks the compilation process, if the
246    diff is small this is probably not a serious problem</p>
247  </li>
248  <li><em>I use the CVS version and there is no configure script</em>
249    <p>The configure (and other Makefiles) are generated. Use the autogen.sh
250    script to regenerate the configure and Makefiles, like:</p>
251    <p><code>/autogen.sh --prefix=/usr --disable-shared</code></p>
252  </li>
253  <li><em>I have troubles when running make tests with gcc-3.0</em>
254    <p>It seems the initial release of gcc-3.0 has a problem with the
255    optimizer which miscompiles the URI module. Please use another
256    compiler</p>
257  </li>
258</ol>
259
260<h3><a name="Developer">Developer</a> corner</h3>
261<ol>
262  <li><em>xmlDocDump() generates output on one line</em>
263    <p>libxml will not <strong>invent</strong> spaces in the content of a
264    document since <strong>all spaces in the content of a document are
265    significant</strong>. If you build a tree from the API and want
266    indentation:</p>
267    <ol>
268      <li>the correct way is to generate those yourself too</li>
269      <li>the dangerous way is to ask libxml to add those blanks to your
270        content <strong>modifying the content of your document in the
271        process</strong>. The result may not be what you expect. There is
272        <strong>NO</strong> way to guarantee that such a modification won't
273        impact other part of the content of your document. See <a
274        href="http://xmlsoft.org/html/libxml-parser.html#XMLKEEPBLANKSDEFAULT">xmlKeepBlanksDefault
275        ()</a> and <a
276        href="http://xmlsoft.org/html/libxml-tree.html#XMLSAVEFORMATFILE">xmlSaveFormatFile
277        ()</a></li>
278    </ol>
279  </li>
280  <li>Extra nodes in the document:
281    <p><em>For a XML file as below:</em></p>
282    <pre>&lt;?xml version="1.0"?&gt;
283&lt;PLAN xmlns="http://www.argus.ca/autotest/1.0/"&gt;
284&lt;NODE CommFlag="0"/&gt;
285&lt;NODE CommFlag="1"/&gt;
286&lt;/PLAN&gt;</pre>
287    <p><em>after parsing it with the function
288    pxmlDoc=xmlParseFile(...);</em></p>
289    <p><em>I want to the get the content of the first node (node with the
290    CommFlag="0")</em></p>
291    <p><em>so I did it as following;</em></p>
292    <pre>xmlNodePtr pode;
293pnode=pxmlDoc-&gt;children-&gt;children;</pre>
294    <p><em>but it does not work. If I change it to</em></p>
295    <pre>pnode=pxmlDoc-&gt;children-&gt;children-&gt;next;</pre>
296    <p><em>then it works.  Can someone explain it to me.</em></p>
297    <p></p>
298    <p>In XML all characters in the content of the document are significant
299    <strong>including blanks and formatting line breaks</strong>.</p>
300    <p>The extra nodes you are wondering about are just that, text nodes with
301    the formatting spaces wich are part of the document but that people tend
302    to forget. There is a function <a
303    href="http://xmlsoft.org/html/libxml-parser.html">xmlKeepBlanksDefault
304    ()</a>  to remove those at parse time, but that's an heuristic, and its
305    use should be limited to case where you are sure there is no
306    mixed-content in the document.</p>
307  </li>
308  <li><em>I get compilation errors of existing code like when accessing
309    <strong>root</strong> or <strong>childs fields</strong> of nodes</em>
310    <p>You are compiling code developed for libxml version 1 and using a
311    libxml2 development environment. Either switch back to libxml v1 devel or
312    even better fix the code to compile with libxml2 (or both) by <a
313    href="upgrade.html">following the instructions</a>.</p>
314  </li>
315  <li><em>I get compilation errors about non existing
316    <strong>xmlRootNode</strong> or <strong>xmlChildrenNode</strong>
317    fields</em>
318    <p>The source code you are using has been <a
319    href="upgrade.html">upgraded</a> to be able to compile with both libxml
320    and libxml2, but you need to install a more recent version:
321    libxml(-devel) &gt;= 1.8.8 or libxml2(-devel) &gt;= 2.1.0</p>
322  </li>
323  <li><em>XPath implementation looks seriously broken</em>
324    <p>XPath implementation prior to 2.3.0 was really incomplete, upgrade to
325    a recent version, the implementation and debug of libxslt generated fixes
326    for most obvious problems.</p>
327  </li>
328  <li><em>The example provided in the web page does not compile</em>
329    <p>It's hard to maintain the documentation in sync with the code
330    &lt;grin/&gt; ...</p>
331    <p>Check the previous points 1/ and 2/ raised before, and send
332    patches.</p>
333  </li>
334  <li><em>Where can I get more examples and informations than in the web
335    page</em>
336    <p>Ideally a libxml book would be nice. I have no such plan ... But you
337    can:</p>
338    <ul>
339      <li>check more deeply the <a href="html/libxml-lib.html">existing
340        generated doc</a></li>
341      <li>looks for examples of use for libxml function using the Gnome code
342        for example the following will query the full Gnome CVs base for the
343        use of the <strong>xmlAddChild()</strong> function:
344        <p><a
345        href="http://cvs.gnome.org/lxr/search?string=xmlAddChild">http://cvs.gnome.org/lxr/search?string=xmlAddChild</a></p>
346        <p>This may be slow, a large hardware donation to the gnome project
347        could cure this :-)</p>
348      </li>
349      <li><a
350        href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&dir=gnome-xml">Browse
351        the libxml source</a>
352         , I try to write code as clean and documented as possible, so
353        looking at it may be helpful</li>
354    </ul>
355  </li>
356  <li>What about C++ ?
357    <p>libxml is written in pure C in order to allow easy reuse on a number
358    of platforms, including embedded systems. I don't intend to convert to
359    C++.</p>
360    <p>There is however a C++ wrapper provided by Ari Johnson
361    &lt;ari@btigate.com&gt; which may fullfill your needs:</p>
362    <p>Website: <a
363    href="http://lusis.org/~ari/xml++/">http://lusis.org/~ari/xml++/</a></p>
364    <p>Download: <a
365    href="http://lusis.org/~ari/xml++/libxml++.tar.gz">http://lusis.org/~ari/xml++/libxml++.tar.gz</a></p>
366  </li>
367  <li>How to validate a document a posteriori ?
368    <p>It is possible to validate documents which had not been validated at
369    initial parsing time or documents who have been built from scratch using
370    the API. Use the <a
371    href="http://xmlsoft.org/html/libxml-valid.html#XMLVALIDATEDTD">xmlValidateDtd()</a>
372    function. It is also possible to simply add a Dtd to an existing
373    document:</p>
374    <pre>xmlDocPtr doc; /* your existing document */
375        xmlDtdPtr dtd = xmlParseDTD(NULL, filename_of_dtd); /* parse the DTD */
376        dtd-&gt;name = xmlStrDup((xmlChar*)"root_name"); /* use the given root */
377
378        doc-&gt;intSubset = dtd;
379        if (doc-&gt;children == NULL) xmlAddChild((xmlNodePtr)doc, (xmlNodePtr)dtd);
380        else xmlAddPrevSibling(doc-&gt;children, (xmlNodePtr)dtd);
381          </pre>
382  </li>
383  <li>etc ...</li>
384</ol>
385
386<p></p>
387
388<h2><a name="Documentat">Documentation</a></h2>
389
390<p>There are some on-line resources about using libxml:</p>
391<ol>
392  <li>Check the <a href="FAQ.html">FAQ</a></li>
393  <li>Check the <a href="http://xmlsoft.org/html/libxml-lib.html">extensive
394    documentation</a> automatically extracted from code comments (using <a
395    href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&dir=gtk-doc">gtk
396    doc</a>).</li>
397  <li>Look at the documentation about <a href="encoding.html">libxml
398    internationalization support</a></li>
399  <li>This page provides a global overview and <a href="#real">some
400    examples</a> on how to use libxml.</li>
401  <li><a href="mailto:james@daa.com.au">James Henstridge</a>
402     wrote <a
403    href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">some nice
404    documentation</a> explaining how to use the libxml SAX interface.</li>
405  <li>George Lebl wrote <a
406    href="http://www-4.ibm.com/software/developer/library/gnome3/">an article
407    for IBM developerWorks</a> about using libxml.</li>
408  <li>Check <a href="http://cvs.gnome.org/lxr/source/gnome-xml/TODO">the TODO
409    file</a></li>
410  <li>Read the <a href="upgrade.html">1.x to 2.x upgrade path</a>. If you are
411    starting a new project using libxml you should really use the 2.x
412  version.</li>
413  <li>And don't forget to look at the <a href="/messages/">mailing-list
414    archive</a>.</li>
415</ol>
416
417<h2><a name="Reporting">Reporting bugs and getting help</a></h2>
418
419<p>Well, bugs or missing features are always possible, and I will make a
420point of fixing them in a timely fashion. The best way to report a bug is to
421use the <a href="http://bugzilla.gnome.org/buglist.cgi?product=libxml">Gnome
422bug tracking database</a> (make sure to use the "libxml" module name). I look
423at reports there regularly and it's good to have a reminder when a bug is
424still open. Be sure to specify that the bug is for the package libxml.</p>
425
426<p>There is also a mailing-list <a
427href="mailto:xml@gnome.org">xml@gnome.org</a> for libxml, with an  <a
428href="http://mail.gnome.org/archives/xml/">on-line archive</a> (<a
429href="http://xmlsoft.org/messages">old</a>). To subscribe to this list,
430please visit the <a
431href="http://mail.gnome.org/mailman/listinfo/xml">associated Web</a> page and
432follow the instructions. <strong>Do not send code, I won't debug it</strong>
433(but patches are really appreciated!).</p>
434
435<p>Check the following <strong><span style="color: #FF0000">before
436posting</span></strong>:</p>
437<ul>
438  <li>read the <a href="FAQ.html">FAQ</a></li>
439  <li>make sure you are <a href="ftp://xmlsoft.org/">using a recent
440    version</a>, and that the problem still shows up in those</li>
441  <li>check the <a href="http://mail.gnome.org/archives/xml/">list
442    archives</a> to see if the problem was reported already, in this case
443    there is probably a fix available, similary check the <a
444    href="http://bugzilla.gnome.org/buglist.cgi?product=libxml">registered
445    open bugs</a></li>
446  <li>make sure you can reproduce the bug with xmllint or one of the test
447    programs found in source in the distribution</li>
448  <li>Please send the command showing the error as well as the input (as an
449    attachement)</li>
450</ul>
451
452<p>Then send the bug with associated informations to reproduce it to the <a
453href="mailto:xml@gnome.org">xml@gnome.org</a> list; if it's really libxml
454related I will approve it.. Please do not send me mail directly, it makes
455things really harder to track and in some cases I'm not the best person to
456answer a given question, ask the list instead.</p>
457
458<p>Of course, bugs reported with a suggested patch for fixing them will
459probably be processed faster.</p>
460
461<p>If you're looking for help, a quick look at <a
462href="http://mail.gnome.org/archives/xml/">the list archive</a> may actually
463provide the answer, I usually send source samples when answering libxml usage
464questions. The <a href="http://xmlsoft.org/html/book1.html">auto-generated
465documentantion</a> is not as polished as I would like (i need to learn more
466about Docbook), but it's a good starting point.</p>
467
468<h2><a name="help">How to help</a></h2>
469
470<p>You can help the project in various ways, the best thing to do first is to
471subscribe to the mailing-list as explained before, check the <a
472href="http://mail.gnome.org/archives/xml/">archives </a>and the <a
473href="http://bugzilla.gnome.org/buglist.cgi?product=libxml">Gnome bug
474database:</a>:</p>
475<ol>
476  <li>provide patches when you find problems</li>
477  <li>provide the diffs when you port libxml to a new platform. They may not
478    be integrated in all cases but help pinpointing portability problems
479  and</li>
480  <li>provide documentation fixes (either as patches to the code comments or
481    as HTML diffs).</li>
482  <li>provide new documentations pieces (translations, examples, etc ...)</li>
483  <li>Check the TODO file and try to close one of the items</li>
484  <li>take one of the points raised in the archive or the bug database and
485    provide a fix. <a href="mailto:daniel@veillard.com">Get in touch with me
486    </a>before to avoid synchronization problems and check that the suggested
487    fix will fit in nicely :-)</li>
488</ol>
489
490<h2><a name="Downloads">Downloads</a></h2>
491
492<p>The latest versions of libxml can be found on <a
493href="ftp://xmlsoft.org/">xmlsoft.org</a> (<a
494href="ftp://speakeasy.rpmfind.net/pub/libxml/">Seattle</a>, <a
495href="ftp://fr.rpmfind.net/pub/libxml/">France</a>) or on the <a
496href="ftp://ftp.gnome.org/pub/GNOME/MIRRORS.html">Gnome FTP server</a> either
497as a <a href="ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/">source
498archive</a> or <a
499href="ftp://ftp.gnome.org/pub/GNOME/stable/redhat/i386/libxml/">RPM
500packages</a>. (NOTE that you need both the <a
501href="http://rpmfind.net/linux/RPM/libxml2.html">libxml(2)</a> and <a
502href="http://rpmfind.net/linux/RPM/libxml2-devel.html">libxml(2)-devel</a>
503packages installed to compile applications using libxml.) <a
504href="mailto:izlatkovic@daenet.de">Igor  Zlatkovic</a> is now the maintainer
505of the Windows port, <a
506href="http://www.fh-frankfurt.de/~igor/projects/libxml/index.html">he
507provides binaries</a>. <a href="mailto:Gary.Pennington@sun.com">Gary
508Pennington</a> provides <a href="http://garypennington.net/libxml2/">Solaris
509binaries</a>.</p>
510
511<p><a name="Snapshot">Snapshot:</a></p>
512<ul>
513  <li>Code from the W3C cvs base libxml <a
514    href="ftp://xmlsoft.org/cvs-snapshot.tar.gz">cvs-snapshot.tar.gz</a></li>
515  <li>Docs, content of the web site, the list archive included <a
516    href="ftp://xmlsoft.org/libxml-docs.tar.gz">libxml-docs.tar.gz</a></li>
517</ul>
518
519<p><a name="Contribs">Contributions:</a></p>
520
521<p>I do accept external contributions, especially if compiling on another
522platform,  get in touch with me to upload the package, wrappers for various
523languages have been provided, and can be found in the <a
524href="contribs.html">contrib section</a></p>
525
526<p>Libxml is also available from CVS:</p>
527<ul>
528  <li><p>The <a
529    href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&dir=gnome-xml">Gnome
530    CVS base</a>. Check the <a
531    href="http://developer.gnome.org/tools/cvs.html">Gnome CVS Tools</a>
532    page; the CVS module is <b>gnome-xml</b>.</p>
533  </li>
534  <li>The <strong>libxslt</strong> module is also present there</li>
535</ul>
536
537<h2><a name="News">News</a></h2>
538
539<h3>CVS only : check the <a
540href="http://cvs.gnome.org/lxr/source/gnome-xml/ChangeLog">Changelog</a> file
541for a really accurate description</h3>
542
543<p>Items floating around but not actively worked on, get in touch with me if
544you want to test those</p>
545<ul>
546  <li>Implementing <a href="http://xmlsoft.org/XSLT">XSLT</a>, this is done
547    as a separate C library on top of libxml called libxslt</li>
548  <li>Finishing up <a href="http://www.w3.org/TR/xptr">XPointer</a> and <a
549    href="http://www.w3.org/TR/xinclude">XInclude</a></li>
550  <li>(seeems working but delayed from release) parsing/import of Docbook
551    SGML docs</li>
552</ul>
553
554<h3>2.4.12: Dec 7 2001</h3>
555<ul>
556  <li>a few bug fixes: thread (Gary Pennington), xmllint (Geert Kloosterman),
557    XML parser (Robin Berjon), XPointer (Danny Jamshy), I/O cleanups
558  (robert)</li>
559  <li>Eric Lavigne contributed project files for MacOS</li>
560  <li>some makefiles cleanups</li>
561</ul>
562
563<h3>2.4.11: Nov 26 2001</h3>
564<ul>
565  <li>fixed a couple of errors in the includes, fixed a few bugs, some code
566    cleanups</li>
567  <li>xmllint man pages improvement by Heiko Rupp</li>
568  <li>updated VMS build instructions from John A Fotheringham</li>
569  <li>Windows Makefiles updates from Igor</li>
570</ul>
571
572<h3>2.4.10: Nov 10 2001</h3>
573<ul>
574  <li>URI escaping fix (Joel Young)</li>
575  <li>added xmlGetNodePath() (for paths or XPointers generation)</li>
576  <li>Fixes namespace handling problems when using DTD and validation</li>
577  <li>improvements on xmllint: Morus Walter patches for --format and
578    --encode, Stefan Kost and Heiko Rupp improvements on the --shell</li>
579  <li>fixes for xmlcatalog linking pointed by Weiqi Gao</li>
580  <li>fixes to the HTML parser</li>
581</ul>
582
583<h3>2.4.9: Nov 6 2001</h3>
584<ul>
585  <li>fixes more catalog bugs</li>
586  <li>avoid a compilation problem, improve xmlGetLineNo()</li>
587</ul>
588
589<h3>2.4.8: Nov 4 2001</h3>
590<ul>
591  <li>fixed SGML catalogs broken in previous release, updated xmlcatalog
592  tool</li>
593  <li>fixed a compile errors and some includes troubles.</li>
594</ul>
595
596<h3>2.4.7: Oct 30 2001</h3>
597<ul>
598  <li>exported some debugging interfaces</li>
599  <li>serious rewrite of the catalog code</li>
600  <li>integrated Gary Pennington thread safety patch, added configure option
601    and regression tests</li>
602  <li>removed an HTML parser bug</li>
603  <li>fixed a couple of potentially serious validation bugs</li>
604  <li>integrated the SGML DocBook support in xmllint</li>
605  <li>changed the nanoftp anonymous login passwd</li>
606  <li>some I/O cleanup and a couple of interfaces for Perl wrapper</li>
607  <li>general bug fixes</li>
608  <li>updated xmllint man page by John Fleck</li>
609  <li>some VMS and Windows updates</li>
610</ul>
611
612<h3>2.4.6: Oct 10 2001</h3>
613<ul>
614  <li>added an updated man pages by John Fleck</li>
615  <li>portability and configure fixes</li>
616  <li>an infinite loop on the HTML parser was removed (William)</li>
617  <li>Windows makefile patches from Igor</li>
618  <li>fixed half a dozen bugs reported fof libxml or libxslt</li>
619  <li>updated xmlcatalog to be able to modify SGML super catalogs</li>
620</ul>
621
622<h3>2.4.5: Sep 14 2001</h3>
623<ul>
624  <li>Remove a few annoying bugs in 2.4.4</li>
625  <li>forces the HTML serializer to output decimal charrefs since some
626    version of Netscape can't handle hexadecimal ones</li>
627</ul>
628
629<h3>1.8.16: Sep 14 2001</h3>
630<ul>
631  <li>maintenance release of the old libxml1 branch, couple of bug and
632    portability fixes</li>
633</ul>
634
635<h3>2.4.4: Sep 12 2001</h3>
636<ul>
637  <li>added --convert to xmlcatalog, bug fixes and cleanups of XML
638  Catalog</li>
639  <li>a few bug fixes and some portability changes</li>
640  <li>some documentation cleanups</li>
641</ul>
642
643<h3>2.4.3:  Aug 23 2001</h3>
644<ul>
645  <li>XML Catalog support see the doc</li>
646  <li>New NaN/Infinity floating point code</li>
647  <li>A few bug fixes</li>
648</ul>
649
650<h3>2.4.2:  Aug 15 2001</h3>
651<ul>
652  <li>adds xmlLineNumbersDefault() to control line number generation</li>
653  <li>lot of bug fixes</li>
654  <li>the Microsoft MSC projects files shuld now be up to date</li>
655  <li>inheritance of namespaces from DTD defaulted attributes</li>
656  <li>fixes a serious potential security bug</li>
657  <li>added a --format option to xmllint</li>
658</ul>
659
660<h3>2.4.1:  July 24 2001</h3>
661<ul>
662  <li>possibility to keep line numbers in the tree</li>
663  <li>some computation NaN fixes</li>
664  <li>extension of the XPath API</li>
665  <li>cleanup for alpha and ia64 targets</li>
666  <li>patch to allow saving through HTTP PUT or POST</li>
667</ul>
668
669<h3>2.4.0: July 10 2001</h3>
670<ul>
671  <li>Fixed a few bugs in XPath, validation, and tree handling.</li>
672  <li>Fixed XML Base implementation, added a coupel of examples to the
673    regression tests</li>
674  <li>A bit of cleanup</li>
675</ul>
676
677<h3>2.3.14: July 5 2001</h3>
678<ul>
679  <li>fixed some entities problems and reduce mem requirement when
680    substituing them</li>
681  <li>lots of improvements in the XPath queries interpreter can be
682    substancially faster</li>
683  <li>Makefiles and configure cleanups</li>
684  <li>Fixes to XPath variable eval, and compare on empty node set</li>
685  <li>HTML tag closing bug fixed</li>
686  <li>Fixed an URI reference computating problem when validating</li>
687</ul>
688
689<h3>2.3.13: June 28 2001</h3>
690<ul>
691  <li>2.3.12 configure.in was broken as well as the push mode XML parser</li>
692  <li>a few more fixes for compilation on Windows MSC by Yon Derek</li>
693</ul>
694
695<h3>1.8.14: June 28 2001</h3>
696<ul>
697  <li>Zbigniew Chyla gave a patch to use the old XML parser in push mode</li>
698  <li>Small Makefile fix</li>
699</ul>
700
701<h3>2.3.12: June 26 2001</h3>
702<ul>
703  <li>lots of cleanup</li>
704  <li>a couple of validation fix</li>
705  <li>fixed line number counting</li>
706  <li>fixed serious problems in the XInclude processing</li>
707  <li>added support for UTF8 BOM at beginning of entities</li>
708  <li>fixed a strange gcc optimizer bugs in xpath handling of float, gcc-3.0
709    miscompile uri.c (William), Thomas Leitner provided a fix for the
710    optimizer on Tru64</li>
711  <li>incorporated Yon Derek and Igor Zlatkovic  fixes and improvements for
712    compilation on Windows MSC</li>
713  <li>update of libxml-doc.el (Felix Natter)</li>
714  <li>fixed 2 bugs in URI normalization code</li>
715</ul>
716
717<h3>2.3.11: June 17 2001</h3>
718<ul>
719  <li>updates to trio, Makefiles and configure should fix some portability
720    problems (alpha)</li>
721  <li>fixed some HTML serialization problems (pre, script, and block/inline
722    handling), added encoding aware APIs, cleanup of this code</li>
723  <li>added xmlHasNsProp()</li>
724  <li>implemented a specific PI for encoding support in the DocBook SGML
725    parser</li>
726  <li>some XPath fixes (-Infinity, / as a function parameter and namespaces
727    node selection)</li>
728  <li>fixed a performance problem and an error in the validation code</li>
729  <li>fixed XInclude routine to implement the recursive behaviour</li>
730  <li>fixed xmlFreeNode problem when libxml is included statically twice</li>
731  <li>added --version to xmllint for bug reports</li>
732</ul>
733
734<h3>2.3.10: June 1 2001</h3>
735<ul>
736  <li>fixed the SGML catalog support</li>
737  <li>a number of reported bugs got fixed, in XPath, iconv detection,
738    XInclude processing</li>
739  <li>XPath string function should now handle unicode correctly</li>
740</ul>
741
742<h3>2.3.9: May 19 2001</h3>
743
744<p>Lots of bugfixes, and added a basic SGML catalog support:</p>
745<ul>
746  <li>HTML push bugfix #54891 and another patch from Jonas Borgstr�m</li>
747  <li>some serious speed optimisation again</li>
748  <li>some documentation cleanups</li>
749  <li>trying to get better linking on solaris (-R)</li>
750  <li>XPath API cleanup from Thomas Broyer</li>
751  <li>Validation bug fixed #54631, added a patch from Gary Pennington, fixed
752    xmlValidGetValidElements()</li>
753  <li>Added an INSTALL file</li>
754  <li>Attribute removal added to API: #54433</li>
755  <li>added a basic support for SGML catalogs</li>
756  <li>fixed xmlKeepBlanksDefault(0) API</li>
757  <li>bugfix in xmlNodeGetLang()</li>
758  <li>fixed a small configure portability problem</li>
759  <li>fixed an inversion of SYSTEM and PUBLIC identifier in HTML document</li>
760</ul>
761
762<h3>1.8.13: May 14 2001</h3>
763<ul>
764  <li>bugfixes release of the old libxml1 branch used by Gnome</li>
765</ul>
766
767<h3>2.3.8: May 3 2001</h3>
768<ul>
769  <li>Integrated an SGML DocBook parser for the Gnome project</li>
770  <li>Fixed a few things in the HTML parser</li>
771  <li>Fixed some XPath bugs raised by XSLT use, tried to fix the floating
772    point portability issue</li>
773  <li>Speed improvement (8M/s for SAX, 3M/s for DOM, 1.5M/s for
774    DOM+validation using the XML REC as input and a 700MHz celeron).</li>
775  <li>incorporated more Windows cleanup</li>
776  <li>added xmlSaveFormatFile()</li>
777  <li>fixed problems in copying nodes with entities references (gdome)</li>
778  <li>removed some troubles surrounding the new validation module</li>
779</ul>
780
781<h3>2.3.7: April 22 2001</h3>
782<ul>
783  <li>lots of small bug fixes, corrected XPointer</li>
784  <li>Non determinist content model validation support</li>
785  <li>added xmlDocCopyNode for gdome2</li>
786  <li>revamped the way the HTML parser handles end of tags</li>
787  <li>XPath: corrctions of namespacessupport and number formatting</li>
788  <li>Windows: Igor Zlatkovic patches for MSC compilation</li>
789  <li>HTML ouput fixes from P C Chow and William M. Brack</li>
790  <li>Improved validation speed sensible for DocBook</li>
791  <li>fixed a big bug with ID declared in external parsed entities</li>
792  <li>portability fixes, update of Trio from Bjorn Reese</li>
793</ul>
794
795<h3>2.3.6: April 8 2001</h3>
796<ul>
797  <li>Code cleanup using extreme gcc compiler warning options, found and
798    cleared half a dozen potential problem</li>
799  <li>the Eazel team found an XML parser bug</li>
800  <li>cleaned up the user of some of the string formatting function. used the
801    trio library code to provide the one needed when the platform is missing
802    them</li>
803  <li>xpath: removed a memory leak and fixed the predicate evaluation
804    problem, extended the testsuite and cleaned up the result. XPointer seems
805    broken ...</li>
806</ul>
807
808<h3>2.3.5: Mar 23 2001</h3>
809<ul>
810  <li>Biggest change is separate parsing and evaluation of XPath expressions,
811    there is some new APIs for this too</li>
812  <li>included a number of bug fixes(XML push parser, 51876, notations,
813  52299)</li>
814  <li>Fixed some portability issues</li>
815</ul>
816
817<h3>2.3.4: Mar 10 2001</h3>
818<ul>
819  <li>Fixed bugs #51860 and #51861</li>
820  <li>Added a global variable xmlDefaultBufferSize to allow default buffer
821    size to be application tunable.</li>
822  <li>Some cleanup in the validation code, still a bug left and this part
823    should probably be rewritten to support ambiguous content model :-\</li>
824  <li>Fix a couple of serious bugs introduced or raised by changes in 2.3.3
825    parser</li>
826  <li>Fixed another bug in xmlNodeGetContent()</li>
827  <li>Bjorn fixed XPath node collection and Number formatting</li>
828  <li>Fixed a loop reported in the HTML parsing</li>
829  <li>blank space are reported even if the Dtd content model proves that they
830    are formatting spaces, this is for XmL conformance</li>
831</ul>
832
833<h3>2.3.3: Mar 1 2001</h3>
834<ul>
835  <li>small change in XPath for XSLT</li>
836  <li>documentation cleanups</li>
837  <li>fix in validation by Gary Pennington</li>
838  <li>serious parsing performances improvements</li>
839</ul>
840
841<h3>2.3.2: Feb 24 2001</h3>
842<ul>
843  <li>chasing XPath bugs, found a bunch, completed some TODO</li>
844  <li>fixed a Dtd parsing bug</li>
845  <li>fixed a bug in xmlNodeGetContent</li>
846  <li>ID/IDREF support partly rewritten by Gary Pennington</li>
847</ul>
848
849<h3>2.3.1: Feb 15 2001</h3>
850<ul>
851  <li>some XPath and HTML bug fixes for XSLT</li>
852  <li>small extension of the hash table interfaces for DOM gdome2
853    implementation</li>
854  <li>A few bug fixes</li>
855</ul>
856
857<h3>2.3.0: Feb 8 2001 (2.2.12 was on 25 Jan but I didn't kept track)</h3>
858<ul>
859  <li>Lots of XPath bug fixes</li>
860  <li>Add a mode with Dtd lookup but without validation error reporting for
861    XSLT</li>
862  <li>Add support for text node without escaping (XSLT)</li>
863  <li>bug fixes for xmlCheckFilename</li>
864  <li>validation code bug fixes from Gary Pennington</li>
865  <li>Patch from Paul D. Smith correcting URI path normalization</li>
866  <li>Patch to allow simultaneous install of libxml-devel and
867  libxml2-devel</li>
868  <li>the example Makefile is now fixed</li>
869  <li>added HTML to the RPM packages</li>
870  <li>tree copying bugfixes</li>
871  <li>updates to Windows makefiles</li>
872  <li>optimisation patch from Bjorn Reese</li>
873</ul>
874
875<h3>2.2.11: Jan 4 2001</h3>
876<ul>
877  <li>bunch of bug fixes (memory I/O, xpath, ftp/http, ...)</li>
878  <li>added htmlHandleOmittedElem()</li>
879  <li>Applied Bjorn Reese's IPV6 first patch</li>
880  <li>Applied Paul D. Smith patches for validation of XInclude results</li>
881  <li>added XPointer xmlns() new scheme support</li>
882</ul>
883
884<h3>2.2.10: Nov 25 2000</h3>
885<ul>
886  <li>Fix the Windows problems of 2.2.8</li>
887  <li>integrate OpenVMS patches</li>
888  <li>better handling of some nasty HTML input</li>
889  <li>Improved the XPointer implementation</li>
890  <li>integrate a number of provided patches</li>
891</ul>
892
893<h3>2.2.9: Nov 25 2000</h3>
894<ul>
895  <li>erroneous release :-(</li>
896</ul>
897
898<h3>2.2.8: Nov 13 2000</h3>
899<ul>
900  <li>First version of <a href="http://www.w3.org/TR/xinclude">XInclude</a>
901    support</li>
902  <li>Patch in conditional section handling</li>
903  <li>updated MS compiler project</li>
904  <li>fixed some XPath problems</li>
905  <li>added an URI escaping function</li>
906  <li>some other bug fixes</li>
907</ul>
908
909<h3>2.2.7: Oct 31 2000</h3>
910<ul>
911  <li>added message redirection</li>
912  <li>XPath improvements (thanks TOM !)</li>
913  <li>xmlIOParseDTD() added</li>
914  <li>various small fixes in the HTML, URI, HTTP and XPointer support</li>
915  <li>some cleanup of the Makefile, autoconf and the distribution content</li>
916</ul>
917
918<h3>2.2.6: Oct 25 2000:</h3>
919<ul>
920  <li>Added an hash table module, migrated a number of internal structure to
921    those</li>
922  <li>Fixed a posteriori validation problems</li>
923  <li>HTTP module cleanups</li>
924  <li>HTML parser improvements (tag errors, script/style handling, attribute
925    normalization)</li>
926  <li>coalescing of adjacent text nodes</li>
927  <li>couple of XPath bug fixes, exported the internal API</li>
928</ul>
929
930<h3>2.2.5: Oct 15 2000:</h3>
931<ul>
932  <li>XPointer implementation and testsuite</li>
933  <li>Lot of XPath fixes, added variable and functions registration, more
934    tests</li>
935  <li>Portability fixes, lots of enhancements toward an easy Windows build
936    and release</li>
937  <li>Late validation fixes</li>
938  <li>Integrated a lot of contributed patches</li>
939  <li>added memory management docs</li>
940  <li>a performance problem when using large buffer seems fixed</li>
941</ul>
942
943<h3>2.2.4: Oct 1 2000:</h3>
944<ul>
945  <li>main XPath problem fixed</li>
946  <li>Integrated portability patches for Windows</li>
947  <li>Serious bug fixes on the URI and HTML code</li>
948</ul>
949
950<h3>2.2.3: Sep 17 2000</h3>
951<ul>
952  <li>bug fixes</li>
953  <li>cleanup of entity handling code</li>
954  <li>overall review of all loops in the parsers, all sprintf usage has been
955    checked too</li>
956  <li>Far better handling of larges Dtd. Validating against Docbook XML Dtd
957    works smoothly now.</li>
958</ul>
959
960<h3>1.8.10: Sep 6 2000</h3>
961<ul>
962  <li>bug fix release for some Gnome projects</li>
963</ul>
964
965<h3>2.2.2: August 12 2000</h3>
966<ul>
967  <li>mostly bug fixes</li>
968  <li>started adding routines to access xml parser context options</li>
969</ul>
970
971<h3>2.2.1: July 21 2000</h3>
972<ul>
973  <li>a purely bug fixes release</li>
974  <li>fixed an encoding support problem when parsing from a memory block</li>
975  <li>fixed a DOCTYPE parsing problem</li>
976  <li>removed a bug in the function allowing to override the memory
977    allocation routines</li>
978</ul>
979
980<h3>2.2.0: July 14 2000</h3>
981<ul>
982  <li>applied a lot of portability fixes</li>
983  <li>better encoding support/cleanup and saving (content is now always
984    encoded in UTF-8)</li>
985  <li>the HTML parser now correctly handles encodings</li>
986  <li>added xmlHasProp()</li>
987  <li>fixed a serious problem with &amp;#38;</li>
988  <li>propagated the fix to FTP client</li>
989  <li>cleanup, bugfixes, etc ...</li>
990  <li>Added a page about <a href="encoding.html">libxml Internationalization
991    support</a></li>
992</ul>
993
994<h3>1.8.9:  July 9 2000</h3>
995<ul>
996  <li>fixed the spec the RPMs should be better</li>
997  <li>fixed a serious bug in the FTP implementation, released 1.8.9 to solve
998    rpmfind users problem</li>
999</ul>
1000
1001<h3>2.1.1: July 1 2000</h3>
1002<ul>
1003  <li>fixes a couple of bugs in the 2.1.0 packaging</li>
1004  <li>improvements on the HTML parser</li>
1005</ul>
1006
1007<h3>2.1.0 and 1.8.8: June 29 2000</h3>
1008<ul>
1009  <li>1.8.8 is mostly a comodity package for upgrading to libxml2 accoding to
1010    <a href="upgrade.html">new instructions</a>. It fixes a nasty problem
1011    about &amp;#38; charref parsing</li>
1012  <li>2.1.0 also ease the upgrade from libxml v1 to the recent version. it
1013    also contains numerous fixes and enhancements:
1014    <ul>
1015      <li>added xmlStopParser() to stop parsing</li>
1016      <li>improved a lot parsing speed when there is large CDATA blocs</li>
1017      <li>includes XPath patches provided by Picdar Technology</li>
1018      <li>tried to fix as much as possible DtD validation and namespace
1019        related problems</li>
1020      <li>output to a given encoding has been added/tested</li>
1021      <li>lot of various fixes</li>
1022    </ul>
1023  </li>
1024</ul>
1025
1026<h3>2.0.0: Apr 12 2000</h3>
1027<ul>
1028  <li>First public release of libxml2. If you are using libxml, it's a good
1029    idea to check the 1.x to 2.x upgrade instructions. NOTE: while initally
1030    scheduled for Apr 3 the relase occured only on Apr 12 due to massive
1031    workload.</li>
1032  <li>The include are now located under $prefix/include/libxml (instead of
1033    $prefix/include/gnome-xml), they also are referenced by
1034    <pre>#include &lt;libxml/xxx.h&gt;</pre>
1035    <p>instead of</p>
1036    <pre>#include "xxx.h"</pre>
1037  </li>
1038  <li>a new URI module for parsing URIs and following strictly RFC 2396</li>
1039  <li>the memory allocation routines used by libxml can now be overloaded
1040    dynamically by using xmlMemSetup()</li>
1041  <li>The previously CVS only tool tester has been renamed
1042    <strong>xmllint</strong> and is now installed as part of the libxml2
1043    package</li>
1044  <li>The I/O interface has been revamped. There is now ways to plug in
1045    specific I/O modules, either at the URI scheme detection level using
1046    xmlRegisterInputCallbacks()  or by passing I/O functions when creating a
1047    parser context using xmlCreateIOParserCtxt()</li>
1048  <li>there is a C preprocessor macro LIBXML_VERSION providing the version
1049    number of the libxml module in use</li>
1050  <li>a number of optional features of libxml can now be excluded at
1051    configure time (FTP/HTTP/HTML/XPath/Debug)</li>
1052</ul>
1053
1054<h3>2.0.0beta: Mar 14 2000</h3>
1055<ul>
1056  <li>This is a first Beta release of libxml version 2</li>
1057  <li>It's available only from<a href="ftp://xmlsoft.org/">xmlsoft.org
1058    FTP</a>, it's packaged as libxml2-2.0.0beta and available as tar and
1059  RPMs</li>
1060  <li>This version is now the head in the Gnome CVS base, the old one is
1061    available under the tag LIB_XML_1_X</li>
1062  <li>This includes a very large set of changes. Froma  programmatic point of
1063    view applications should not have to be modified too much, check the <a
1064    href="upgrade.html">upgrade page</a></li>
1065  <li>Some interfaces may changes (especially a bit about encoding).</li>
1066  <li>the updates includes:
1067    <ul>
1068      <li>fix I18N support. ISO-Latin-x/UTF-8/UTF-16 (nearly) seems correctly
1069        handled now</li>
1070      <li>Better handling of entities, especially well formedness checking
1071        and proper PEref extensions in external subsets</li>
1072      <li>DTD conditional sections</li>
1073      <li>Validation now correcly handle entities content</li>
1074      <li><a href="http://rpmfind.net/tools/gdome/messages/0039.html">change
1075        structures to accomodate DOM</a></li>
1076    </ul>
1077  </li>
1078  <li>Serious progress were made toward compliance, <a
1079    href="conf/result.html">here are the result of the test</a> against the
1080    OASIS testsuite (except the japanese tests since I don't support that
1081    encoding yet). This URL is rebuilt every couple of hours using the CVS
1082    head version.</li>
1083</ul>
1084
1085<h3>1.8.7: Mar 6 2000</h3>
1086<ul>
1087  <li>This is a bug fix release:</li>
1088  <li>It is possible to disable the ignorable blanks heuristic used by
1089    libxml-1.x, a new function  xmlKeepBlanksDefault(0) will allow this. Note
1090    that for adherence to XML spec, this behaviour will be disabled by
1091    default in 2.x . The same function will allow to keep compatibility for
1092    old code.</li>
1093  <li>Blanks in &lt;a&gt;  &lt;/a&gt; constructs are not ignored anymore,
1094    avoiding heuristic is really the Right Way :-\</li>
1095  <li>The unchecked use of snprintf which was breaking libxml-1.8.6
1096    compilation on some platforms has been fixed</li>
1097  <li>nanoftp.c nanohttp.c: Fixed '#' and '?' stripping when processing
1098  URIs</li>
1099</ul>
1100
1101<h3>1.8.6: Jan 31 2000</h3>
1102<ul>
1103  <li>added a nanoFTP transport module, debugged until the new version of <a
1104    href="http://rpmfind.net/linux/rpm2html/rpmfind.html">rpmfind</a> can use
1105    it without troubles</li>
1106</ul>
1107
1108<h3>1.8.5: Jan 21 2000</h3>
1109<ul>
1110  <li>adding APIs to parse a well balanced chunk of XML (production <a
1111    href="http://www.w3.org/TR/REC-xml#NT-content">[43] content</a> of the
1112    XML spec)</li>
1113  <li>fixed a hideous bug in xmlGetProp pointed by Rune.Djurhuus@fast.no</li>
1114  <li>Jody Goldberg &lt;jgoldberg@home.com&gt; provided another patch trying
1115    to solve the zlib checks problems</li>
1116  <li>The current state in gnome CVS base is expected to ship as 1.8.5 with
1117    gnumeric soon</li>
1118</ul>
1119
1120<h3>1.8.4: Jan 13 2000</h3>
1121<ul>
1122  <li>bug fixes, reintroduced xmlNewGlobalNs(), fixed xmlNewNs()</li>
1123  <li>all exit() call should have been removed from libxml</li>
1124  <li>fixed a problem with INCLUDE_WINSOCK on WIN32 platform</li>
1125  <li>added newDocFragment()</li>
1126</ul>
1127
1128<h3>1.8.3: Jan 5 2000</h3>
1129<ul>
1130  <li>a Push interface for the XML and HTML parsers</li>
1131  <li>a shell-like interface to the document tree (try tester --shell :-)</li>
1132  <li>lots of bug fixes and improvement added over XMas hollidays</li>
1133  <li>fixed the DTD parsing code to work with the xhtml DTD</li>
1134  <li>added xmlRemoveProp(), xmlRemoveID() and xmlRemoveRef()</li>
1135  <li>Fixed bugs in xmlNewNs()</li>
1136  <li>External entity loading code has been revamped, now it uses
1137    xmlLoadExternalEntity(), some fix on entities processing were added</li>
1138  <li>cleaned up WIN32 includes of socket stuff</li>
1139</ul>
1140
1141<h3>1.8.2: Dec 21 1999</h3>
1142<ul>
1143  <li>I got another problem with includes and C++, I hope this issue is fixed
1144    for good this time</li>
1145  <li>Added a few tree modification functions: xmlReplaceNode,
1146    xmlAddPrevSibling, xmlAddNextSibling, xmlNodeSetName and
1147    xmlDocSetRootElement</li>
1148  <li>Tried to improve the HTML output with help from <a
1149    href="mailto:clahey@umich.edu">Chris Lahey</a></li>
1150</ul>
1151
1152<h3>1.8.1: Dec 18 1999</h3>
1153<ul>
1154  <li>various patches to avoid troubles when using libxml with C++ compilers
1155    the "namespace" keyword and C escaping in include files</li>
1156  <li>a problem in one of the core macros IS_CHAR was corrected</li>
1157  <li>fixed a bug introduced in 1.8.0 breaking default namespace processing,
1158    and more specifically the Dia application</li>
1159  <li>fixed a posteriori validation (validation after parsing, or by using a
1160    Dtd not specified in the original document)</li>
1161  <li>fixed a bug in</li>
1162</ul>
1163
1164<h3>1.8.0: Dec 12 1999</h3>
1165<ul>
1166  <li>cleanup, especially memory wise</li>
1167  <li>the parser should be more reliable, especially the HTML one, it should
1168    not crash, whatever the input !</li>
1169  <li>Integrated various patches, especially a speedup improvement for large
1170    dataset from <a href="mailto:cnygard@bellatlantic.net">Carl Nygard</a>,
1171    configure with --with-buffers to enable them.</li>
1172  <li>attribute normalization, oops should have been added long ago !</li>
1173  <li>attributes defaulted from Dtds should be available, xmlSetProp() now
1174    does entities escapting by default.</li>
1175</ul>
1176
1177<h3>1.7.4: Oct 25 1999</h3>
1178<ul>
1179  <li>Lots of HTML improvement</li>
1180  <li>Fixed some errors when saving both XML and HTML</li>
1181  <li>More examples, the regression tests should now look clean</li>
1182  <li>Fixed a bug with contiguous charref</li>
1183</ul>
1184
1185<h3>1.7.3: Sep 29 1999</h3>
1186<ul>
1187  <li>portability problems fixed</li>
1188  <li>snprintf was used unconditionnally, leading to link problems on system
1189    were it's not available, fixed</li>
1190</ul>
1191
1192<h3>1.7.1: Sep 24 1999</h3>
1193<ul>
1194  <li>The basic type for strings manipulated by libxml has been renamed in
1195    1.7.1 from <strong>CHAR</strong> to <strong>xmlChar</strong>. The reason
1196    is that CHAR was conflicting with a predefined type on Windows. However
1197    on non WIN32 environment, compatibility is provided by the way of  a
1198    <strong>#define </strong>.</li>
1199  <li>Changed another error : the use of a structure field called errno, and
1200    leading to troubles on platforms where it's a macro</li>
1201</ul>
1202
1203<h3>1.7.0: sep 23 1999</h3>
1204<ul>
1205  <li>Added the ability to fetch remote DTD or parsed entities, see the <a
1206    href="html/libxml-nanohttp.html">nanohttp</a> module.</li>
1207  <li>Added an errno to report errors by another mean than a simple printf
1208    like callback</li>
1209  <li>Finished ID/IDREF support and checking when validation</li>
1210  <li>Serious memory leaks fixed (there is now a <a
1211    href="html/libxml-xmlmemory.html">memory wrapper</a> module)</li>
1212  <li>Improvement of <a href="http://www.w3.org/TR/xpath">XPath</a>
1213    implementation</li>
1214  <li>Added an HTML parser front-end</li>
1215</ul>
1216
1217<h2><a name="XML">XML</a></h2>
1218
1219<p><a href="http://www.w3.org/TR/REC-xml">XML is a standard</a> for
1220markup-based structured documents. Here is <a name="example">an example XML
1221document</a>:</p>
1222<pre>&lt;?xml version="1.0"?&gt;
1223&lt;EXAMPLE prop1="gnome is great" prop2="&amp;amp; linux too"&gt;
1224  &lt;head&gt;
1225   &lt;title&gt;Welcome to Gnome&lt;/title&gt;
1226  &lt;/head&gt;
1227  &lt;chapter&gt;
1228   &lt;title&gt;The Linux adventure&lt;/title&gt;
1229   &lt;p&gt;bla bla bla ...&lt;/p&gt;
1230   &lt;image href="linus.gif"/&gt;
1231   &lt;p&gt;...&lt;/p&gt;
1232  &lt;/chapter&gt;
1233&lt;/EXAMPLE&gt;</pre>
1234
1235<p>The first line specifies that it's an XML document and gives useful
1236information about its encoding. Then the document is a text format whose
1237structure is specified by tags between brackets. <strong>Each tag opened has
1238to be closed</strong>. XML is pedantic about this. However, if a tag is empty
1239(no content), a single tag can serve as both the opening and closing tag if
1240it ends with <code>/&gt;</code> rather than with <code>&gt;</code>. Note
1241that, for example, the image tag has no content (just an attribute) and is
1242closed by ending the tag with <code>/&gt;</code>.</p>
1243
1244<p>XML can be applied sucessfully to a wide range of uses, from long term
1245structured document maintenance (where it follows the steps of SGML) to
1246simple data encoding mechanisms like configuration file formatting (glade),
1247spreadsheets (gnumeric), or even shorter lived documents such as WebDAV where
1248it is used to encode remote calls between a client and a server.</p>
1249
1250<h2><a name="XSLT">XSLT</a></h2>
1251
1252<p>Check <a href="http://xmlsoft.org/XSLT">the separate libxslt page</a></p>
1253
1254<p><a href="http://www.w3.org/TR/xslt">XSL Transformations</a>,  is a
1255language for transforming XML documents into other XML documents (or
1256HTML/textual output).</p>
1257
1258<p>A separate library called libxslt is being built on top of libxml2. This
1259module "libxslt" can be found in the Gnome CVS base too.</p>
1260
1261<p>You can check the <a
1262href="http://cvs.gnome.org/lxr/source/libxslt/FEATURES">features</a>
1263supported and the progresses on the <a
1264href="http://cvs.gnome.org/lxr/source/libxslt/ChangeLog">Changelog</a></p>
1265
1266<h2><a name="architecture">libxml architecture</a></h2>
1267
1268<p>Libxml is made of multiple components; some of them are optional, and most
1269of the block interfaces are public. The main components are:</p>
1270<ul>
1271  <li>an Input/Output layer</li>
1272  <li>FTP and HTTP client layers (optional)</li>
1273  <li>an Internationalization layer managing the encodings support</li>
1274  <li>a URI module</li>
1275  <li>the XML parser and its basic SAX interface</li>
1276  <li>an HTML parser using the same SAX interface (optional)</li>
1277  <li>a SAX tree module to build an in-memory DOM representation</li>
1278  <li>a tree module to manipulate the DOM representation</li>
1279  <li>a validation module using the DOM representation (optional)</li>
1280  <li>an XPath module for global lookup in a DOM representation
1281  (optional)</li>
1282  <li>a debug module (optional)</li>
1283</ul>
1284
1285<p>Graphically this gives the following:</p>
1286
1287<p><img src="libxml.gif" alt="a graphical view of the various"></p>
1288
1289<p></p>
1290
1291<h2><a name="tree">The tree output</a></h2>
1292
1293<p>The parser returns a tree built during the document analysis. The value
1294returned is an <strong>xmlDocPtr</strong> (i.e., a pointer to an
1295<strong>xmlDoc</strong> structure). This structure contains information such
1296as the file name, the document type, and a <strong>children</strong> pointer
1297which is the root of the document (or more exactly the first child under the
1298root which is the document). The tree is made of <strong>xmlNode</strong>s,
1299chained in double-linked lists of siblings and with a children&lt;-&gt;parent
1300relationship. An xmlNode can also carry properties (a chain of xmlAttr
1301structures). An attribute may have a value which is a list of TEXT or
1302ENTITY_REF nodes.</p>
1303
1304<p>Here is an example (erroneous with respect to the XML spec since there
1305should be only one ELEMENT under the root):</p>
1306
1307<p><img src="structure.gif" alt=" structure.gif "></p>
1308
1309<p>In the source package there is a small program (not installed by default)
1310called <strong>xmllint</strong> which parses XML files given as argument and
1311prints them back as parsed. This is useful for detecting errors both in XML
1312code and in the XML parser itself. It has an option <strong>--debug</strong>
1313which prints the actual in-memory structure of the document; here is the
1314result with the <a href="#example">example</a> given before:</p>
1315<pre>DOCUMENT
1316version=1.0
1317standalone=true
1318  ELEMENT EXAMPLE
1319    ATTRIBUTE prop1
1320      TEXT
1321      content=gnome is great
1322    ATTRIBUTE prop2
1323      ENTITY_REF
1324      TEXT
1325      content= linux too 
1326    ELEMENT head
1327      ELEMENT title
1328        TEXT
1329        content=Welcome to Gnome
1330    ELEMENT chapter
1331      ELEMENT title
1332        TEXT
1333        content=The Linux adventure
1334      ELEMENT p
1335        TEXT
1336        content=bla bla bla ...
1337      ELEMENT image
1338        ATTRIBUTE href
1339          TEXT
1340          content=linus.gif
1341      ELEMENT p
1342        TEXT
1343        content=...</pre>
1344
1345<p>This should be useful for learning the internal representation model.</p>
1346
1347<h2><a name="interface">The SAX interface</a></h2>
1348
1349<p>Sometimes the DOM tree output is just too large to fit reasonably into
1350memory. In that case (and if you don't expect to save back the XML document
1351loaded using libxml), it's better to use the SAX interface of libxml. SAX is
1352a <strong>callback-based interface</strong> to the parser. Before parsing,
1353the application layer registers a customized set of callbacks which are
1354called by the library as it progresses through the XML input.</p>
1355
1356<p>To get more detailed step-by-step guidance on using the SAX interface of
1357libxml, see the <a
1358href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">nice
1359documentation</a>.written by <a href="mailto:james@daa.com.au">James
1360Henstridge</a>.</p>
1361
1362<p>You can debug the SAX behaviour by using the <strong>testSAX</strong>
1363program located in the gnome-xml module (it's usually not shipped in the
1364binary packages of libxml, but you can find it in the tar source
1365distribution). Here is the sequence of callbacks that would be reported by
1366testSAX when parsing the example XML document shown earlier:</p>
1367<pre>SAX.setDocumentLocator()
1368SAX.startDocument()
1369SAX.getEntity(amp)
1370SAX.startElement(EXAMPLE, prop1='gnome is great', prop2='&amp;amp; linux too')
1371SAX.characters(   , 3)
1372SAX.startElement(head)
1373SAX.characters(    , 4)
1374SAX.startElement(title)
1375SAX.characters(Welcome to Gnome, 16)
1376SAX.endElement(title)
1377SAX.characters(   , 3)
1378SAX.endElement(head)
1379SAX.characters(   , 3)
1380SAX.startElement(chapter)
1381SAX.characters(    , 4)
1382SAX.startElement(title)
1383SAX.characters(The Linux adventure, 19)
1384SAX.endElement(title)
1385SAX.characters(    , 4)
1386SAX.startElement(p)
1387SAX.characters(bla bla bla ..., 15)
1388SAX.endElement(p)
1389SAX.characters(    , 4)
1390SAX.startElement(image, href='linus.gif')
1391SAX.endElement(image)
1392SAX.characters(    , 4)
1393SAX.startElement(p)
1394SAX.characters(..., 3)
1395SAX.endElement(p)
1396SAX.characters(   , 3)
1397SAX.endElement(chapter)
1398SAX.characters( , 1)
1399SAX.endElement(EXAMPLE)
1400SAX.endDocument()</pre>
1401
1402<p>Most of the other interfaces of libxml are based on the DOM tree-building
1403facility, so nearly everything up to the end of this document presupposes the
1404use of the standard DOM tree build. Note that the DOM tree itself is built by
1405a set of registered default callbacks, without internal specific
1406interface.</p>
1407
1408<h2><a name="Validation">Validation &amp; DTDs</a></h2>
1409
1410<p>Table of Content:</p>
1411<ol>
1412  <li><a href="#General5">General overview</a></li>
1413  <li><a href="#definition">The definition</a></li>
1414  <li><a href="#Simple">Simple rules</a>
1415    <ol>
1416      <li><a href="#reference">How to reference a DTD from a document</a></li>
1417      <li><a href="#Declaring">Declaring elements</a></li>
1418      <li><a href="#Declaring1">Declaring attributes</a></li>
1419    </ol>
1420  </li>
1421  <li><a href="#Some">Some examples</a></li>
1422  <li><a href="#validate">How to validate</a></li>
1423  <li><a href="#Other">Other resources</a></li>
1424</ol>
1425
1426<h3><a name="General5">General overview</a></h3>
1427
1428<p>Well what is validation and what is a DTD ?</p>
1429
1430<p>DTD is the acronym for Document Type Definition. This is a description of
1431the content for a familly of XML files. This is part of the XML 1.0
1432specification, and alows to describe and check that a given document instance
1433conforms to a set of rules detailing its structure and content.</p>
1434
1435<p>Validation is the process of checking a document against a DTD (more
1436generally against a set of construction rules).</p>
1437
1438<p>The validation process and building DTDs are the two most difficult parts
1439of the XML life cycle. Briefly a DTD defines all the possibles element to be
1440found within your document, what is the formal shape of your document tree
1441(by defining the allowed content of an element, either text, a regular
1442expression for the allowed list of children, or mixed content i.e. both text
1443and children). The DTD also defines the allowed attributes for all elements
1444and the types of the attributes.</p>
1445
1446<h3><a name="definition1">The definition</a></h3>
1447
1448<p>The <a href="http://www.w3.org/TR/REC-xml">W3C XML Recommendation</a> (<a
1449href="http://www.xml.com/axml/axml.html">Tim Bray's annotated version of
1450Rev1</a>):</p>
1451<ul>
1452  <li><a href="http://www.w3.org/TR/REC-xml#elemdecls">Declaring
1453  elements</a></li>
1454  <li><a href="http://www.w3.org/TR/REC-xml#attdecls">Declaring
1455  attributes</a></li>
1456</ul>
1457
1458<p>(unfortunately) all this is inherited from the SGML world, the syntax is
1459ancient...</p>
1460
1461<h3><a name="Simple1">Simple rules</a></h3>
1462
1463<p>Writing DTD can be done in multiple ways, the rules to build them if you
1464need something fixed or something which can evolve over time can be radically
1465different. Really complex DTD like Docbook ones are flexible but quite harder
1466to design. I will just focuse on DTDs for a formats with a fixed simple
1467structure. It is just a set of basic rules, and definitely not exhaustive nor
1468useable for complex DTD design.</p>
1469
1470<h4><a name="reference1">How to reference a DTD from a document</a>:</h4>
1471
1472<p>Assuming the top element of the document is <code>spec</code> and the dtd
1473is placed in the file <code>mydtd</code> in the subdirectory
1474<code>dtds</code> of the directory from where the document were loaded:</p>
1475
1476<p><code>&lt;!DOCTYPE spec SYSTEM "dtds/mydtd"&gt;</code></p>
1477
1478<p>Notes:</p>
1479<ul>
1480  <li>the system string is actually an URI-Reference (as defined in <a
1481    href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>) so you can use a
1482    full URL string indicating the location of your DTD on the Web, this is a
1483    really good thing to do if you want others to validate your document</li>
1484  <li>it is also possible to associate a <code>PUBLIC</code> identifier (a
1485    magic string) so that the DTd is looked up in catalogs on the client side
1486    without having to locate it on the web</li>
1487  <li>a dtd contains a set of elements and attributes declarations, but they
1488    don't define what the root of the document should be. This is explicitely
1489    told to the parser/validator as the first element of the
1490    <code>DOCTYPE</code> declaration.</li>
1491</ul>
1492
1493<h4><a name="Declaring2">Declaring elements</a>:</h4>
1494
1495<p>The following declares an element <code>spec</code>:</p>
1496
1497<p><code>&lt;!ELEMENT spec (front, body, back?)&gt;</code></p>
1498
1499<p>it also expresses that the spec element contains one <code>front</code>,
1500one <code>body</code> and one optionnal <code>back</code> children elements
1501in this order. The declaration of one element of the structure and its
1502content are done in a single declaration. Similary the following declares
1503<code>div1</code> elements:</p>
1504
1505<p><code>&lt;!ELEMENT div1 (head, (p | list | note)*, div2*)&gt;</code></p>
1506
1507<p>means div1 contains one <code>head</code> then a series of optional
1508<code>p</code>, <code>list</code>s and <code>note</code>s and then an
1509optional <code>div2</code>. And last but not least an element can contain
1510text:</p>
1511
1512<p><code>&lt;!ELEMENT b (#PCDATA)&gt;</code></p>
1513
1514<p><code>b</code> contains text or being of mixed content (text and elements
1515in no particular order):</p>
1516
1517<p><code>&lt;!ELEMENT p (#PCDATA|a|ul|b|i|em)*&gt;</code></p>
1518
1519<p><code>p </code>can contain text or <code>a</code>, <code>ul</code>,
1520<code>b</code>, <code>i </code>or <code>em</code> elements in no particular
1521order.</p>
1522
1523<h4><a name="Declaring1">Declaring attributes</a>:</h4>
1524
1525<p>again the attributes declaration includes their content definition:</p>
1526
1527<p><code>&lt;!ATTLIST termdef name CDATA #IMPLIED&gt;</code></p>
1528
1529<p>means that the element <code>termdef</code> can have a <code>name</code>
1530attribute containing text (<code>CDATA</code>) and which is optionnal
1531(<code>#IMPLIED</code>). The attribute value can also be defined within a
1532set:</p>
1533
1534<p><code>&lt;!ATTLIST list type (bullets|ordered|glossary)
1535"ordered"&gt;</code></p>
1536
1537<p>means <code>list</code> element have a <code>type</code> attribute with 3
1538allowed values "bullets", "ordered" or "glossary" and which default to
1539"ordered" if the attribute is not explicitely specified.</p>
1540
1541<p>The content type of an attribute can be text (<code>CDATA</code>),
1542anchor/reference/references
1543(<code>ID</code>/<code>IDREF</code>/<code>IDREFS</code>), entity(ies)
1544(<code>ENTITY</code>/<code>ENTITIES</code>) or name(s)
1545(<code>NMTOKEN</code>/<code>NMTOKENS</code>). The following defines that a
1546<code>chapter</code> element can have an optional <code>id</code> attribute
1547of type <code>ID</code>, usable for reference from attribute of type
1548IDREF:</p>
1549
1550<p><code>&lt;!ATTLIST chapter id ID #IMPLIED&gt;</code></p>
1551
1552<p>The last value of an attribute definition can be <code>#REQUIRED
1553</code>meaning that the attribute has to be given, <code>#IMPLIED</code>
1554meaning that it is optional, or the default value (possibly prefixed by
1555<code>#FIXED</code> if it is the only allowed).</p>
1556
1557<p>Notes:</p>
1558<ul>
1559  <li>usually the attributes pertaining to a given element are declared in a
1560    single expression, but it is just a convention adopted by a lot of DTD
1561    writers:
1562    <pre>&lt;!ATTLIST termdef
1563          id      ID      #REQUIRED
1564          name    CDATA   #IMPLIED&gt;</pre>
1565    <p>The previous construct defines both <code>id</code> and
1566    <code>name</code> attributes for the element <code>termdef</code></p>
1567  </li>
1568</ul>
1569
1570<h3><a name="Some1">Some examples</a></h3>
1571
1572<p>The directory <code>test/valid/dtds/</code> in the libxml distribution
1573contains some complex DTD examples. The  <code>test/valid/dia.xml</code>
1574example shows an XML file where the simple DTD is directly included within
1575the document.</p>
1576
1577<h3><a name="validate1">How to validate</a></h3>
1578
1579<p>The simplest is to use the xmllint program comming with libxml. The
1580<code>--valid</code> option turn on validation of the files given as input,
1581for example the following validates a copy of the first revision of the XML
15821.0 specification:</p>
1583
1584<p><code>xmllint --valid --noout test/valid/REC-xml-19980210.xml</code></p>
1585
1586<p>the -- noout is used to not output the resulting tree.</p>
1587
1588<p>The <code>--dtdvalid dtd</code> allows to validate the document(s) against
1589a given DTD.</p>
1590
1591<p>Libxml exports an API to handle DTDs and validation, check the <a
1592href="http://xmlsoft.org/html/libxml-valid.html">associated
1593description</a>.</p>
1594
1595<h3><a name="Other1">Other resources</a></h3>
1596
1597<p>DTDs are as old as SGML. So there may be a number of examples on-line, I
1598will just list one for now, others pointers welcome:</p>
1599<ul>
1600  <li><a href="http://www.xml101.com:8081/dtd/">XML-101 DTD</a></li>
1601</ul>
1602
1603<p>I suggest looking at the examples found under test/valid/dtd and any of
1604the large number of books available on XML. The dia example in test/valid
1605should be both simple and complete enough to allow you to build your own.</p>
1606
1607<p></p>
1608
1609<h2><a name="Memory">Memory Management</a></h2>
1610
1611<p>Table of Content:</p>
1612<ol>
1613  <li><a href="#General3">General overview</a></li>
1614  <li><a href="#setting">Setting libxml set of memory routines</a></li>
1615  <li><a href="#cleanup">Cleaning up after parsing</a></li>
1616  <li><a href="#Debugging">Debugging routines</a></li>
1617  <li><a href="#General4">General memory requirements</a></li>
1618</ol>
1619
1620<h3><a name="General3">General overview</a></h3>
1621
1622<p>The module <code><a
1623href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlmemory.h</a></code>
1624provides the interfaces to the libxml memory system:</p>
1625<ul>
1626  <li>libxml does not use the libc memory allocator directly but xmlFree(),
1627    xmlMalloc() and xmlRealloc()</li>
1628  <li>those routines can be reallocated to a specific set of routine, by
1629    default the libc ones i.e. free(), malloc() and realloc()</li>
1630  <li>the xmlmemory.c module includes a set of debugging routine</li>
1631</ul>
1632
1633<h3><a name="setting">Setting libxml set of memory routines</a></h3>
1634
1635<p>It is sometimes useful to not use the default memory allocator, either for
1636debugging, analysis or to implement a specific behaviour on memory management
1637(like on embedded systems). Two function calls are available to do so:</p>
1638<ul>
1639  <li><a href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlMemGet ()</a>
1640     which return the current set of functions in use by the parser</li>
1641  <li><a
1642    href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlMemSetup()</a>
1643     which allow to set up a new set of memory allocation functions</li>
1644</ul>
1645
1646<p>Of course a call to xmlMemSetup() should probably be done before calling
1647any other libxml routines (unless you are sure your allocations routines are
1648compatibles).</p>
1649
1650<h3><a name="cleanup">Cleaning up after parsing</a></h3>
1651
1652<p>Libxml is not stateless, there is a few set of memory structures needing
1653allocation before the parser is fully functionnal (some encoding structures
1654for example). This also mean that once parsing is finished there is a tiny
1655amount of memory (a few hundred bytes) which can be recollected if you don't
1656reuse the parser immediately:</p>
1657<ul>
1658  <li><a href="http://xmlsoft.org/html/libxml-parser.html">xmlCleanupParser
1659    ()</a>
1660     is a centralized routine to free the parsing states. Note that it won't
1661    deallocate any produced tree if any (use the xmlFreeDoc() and related
1662    routines for this).</li>
1663  <li><a href="http://xmlsoft.org/html/libxml-parser.html">xmlInitParser
1664    ()</a>
1665     is the dual routine allowing to preallocate the parsing state which can
1666    be useful for example to avoid initialization reentrancy problems when
1667    using libxml in multithreaded applications</li>
1668</ul>
1669
1670<p>Generally xmlCleanupParser() is safe, if needed the state will be rebuild
1671at the next invocation of parser routines, but be careful of the consequences
1672in multithreaded applications.</p>
1673
1674<h3><a name="Debugging">Debugging routines</a></h3>
1675
1676<p>When configured using --with-mem-debug flag (off by default), libxml uses
1677a set of memory allocation debugging routineskeeping track of all allocated
1678blocks and the location in the code where the routine was called. A couple of
1679other debugging routines allow to dump the memory allocated infos to a file
1680or call a specific routine when a given block number is allocated:</p>
1681<ul>
1682  <li><a
1683    href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlMallocLoc()</a>
1684     <a
1685    href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlReallocLoc()</a>
1686    and <a
1687    href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlMemStrdupLoc()</a>
1688    are the memory debugging replacement allocation routines</li>
1689  <li><a href="http://xmlsoft.org/html/libxml-xmlmemory.html">xmlMemoryDump
1690    ()</a>
1691     dumps all the informations about the allocated memory block lefts in the
1692    <code>.memdump</code> file</li>
1693</ul>
1694
1695<p>When developping libxml memory debug is enabled, the tests programs call
1696xmlMemoryDump () and the "make test" regression tests will check for any
1697memory leak during the full regression test sequence, this helps a lot
1698ensuring that libxml  does not leak memory and bullet proof memory
1699allocations use (some libc implementations are known to be far too permissive
1700resulting in major portability problems!).</p>
1701
1702<p>If the .memdump reports a leak, it displays the allocation function and
1703also tries to give some informations about the content and structure of the
1704allocated blocks left. This is sufficient in most cases to find the culprit,
1705but not always. Assuming the allocation problem is reproductible, it is
1706possible to find more easilly:</p>
1707<ol>
1708  <li>write down the block number xxxx not allocated</li>
1709  <li>export the environement variable XML_MEM_BREAKPOINT=xxxx</li>
1710  <li>run the program under a debugger and set a breakpoint on
1711    xmlMallocBreakpoint() a specific function called when this precise block
1712    is allocated</li>
1713  <li>when the breakpoint is reached you can then do a fine analysis of the
1714    allocation an step  to see the condition resulting in the missing
1715    deallocation.</li>
1716</ol>
1717
1718<p>I used to use a commercial tool to debug libxml memory problems but after
1719noticing that it was not detecting memory leaks that simple mechanism was
1720used and proved extremely efficient until now.</p>
1721
1722<h3><a name="General4">General memory requirements</a></h3>
1723
1724<p>How much libxml memory require ? It's hard to tell in average it depends
1725of a number of things:</p>
1726<ul>
1727  <li>the parser itself should work  in a fixed amout of memory, except for
1728    information maintained about the stacks of names and  entities locations.
1729    The I/O and encoding handlers will probably account for a few KBytes.
1730    This is true for both the XML and HTML parser (though the HTML parser
1731    need more state).</li>
1732  <li>If you are generating the DOM tree then memory requirements will grow
1733    nearly lineary with the size of the data. In general for a balanced
1734    textual document the internal memory requirement is about 4 times the
1735    size of the UTF8 serialization of this document (exmple the XML-1.0
1736    recommendation is a bit more of 150KBytes and takes 650KBytes of main
1737    memory when parsed). Validation will add a amount of memory required for
1738    maintaining the external Dtd state which should be linear with the
1739    complexity of the content model defined by the Dtd</li>
1740  <li>If you don't care about the advanced features of libxml like
1741    validation, DOM, XPath or XPointer, but really need to work fixed memory
1742    requirements, then the SAX interface should be used.</li>
1743</ul>
1744
1745<p></p>
1746
1747<h2><a name="Encodings">Encodings support</a></h2>
1748
1749<p>Table of Content:</p>
1750<ol>
1751  <li><a href="encoding.html#What">What does internationalization support
1752    mean ?</a></li>
1753  <li><a href="encoding.html#internal">The internal encoding, how and
1754  why</a></li>
1755  <li><a href="encoding.html#implemente">How is it implemented ?</a></li>
1756  <li><a href="encoding.html#Default">Default supported encodings</a></li>
1757  <li><a href="encoding.html#extend">How to extend the existing
1758  support</a></li>
1759</ol>
1760
1761<h3><a name="What">What does internationalization support mean ?</a></h3>
1762
1763<p>XML was designed from the start to allow the support of any character set
1764by using Unicode. Any conformant XML parser has to support the UTF-8 and
1765UTF-16 default encodings which can both express the full unicode ranges. UTF8
1766is a variable length encoding whose greatest point are to resuse the same
1767emcoding for ASCII and to save space for Western encodings, but it is a bit
1768more complex to handle in practice. UTF-16 use 2 bytes per characters (and
1769sometimes combines two pairs), it makes implementation easier, but looks a
1770bit overkill for Western languages encoding. Moreover the XML specification
1771allows document to be encoded in other encodings at the condition that they
1772are clearly labelled as such. For example the following is a wellformed XML
1773document encoded in ISO-8859 1 and using accentuated letter that we French
1774likes for both markup and content:</p>
1775<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
1776&lt;tr�s&gt;l�&lt;/tr�s&gt;</pre>
1777
1778<p>Having internationalization support in libxml means the foolowing:</p>
1779<ul>
1780  <li>the document is properly parsed</li>
1781  <li>informations about it's encoding are saved</li>
1782  <li>it can be modified</li>
1783  <li>it can be saved in its original encoding</li>
1784  <li>it can also be saved in another encoding supported by libxml (for
1785    example straight UTF8 or even an ASCII form)</li>
1786</ul>
1787
1788<p>Another very important point is that the whole libxml API, with the
1789exception of a few routines to read with a specific encoding or save to a
1790specific encoding, is completely agnostic about the original encoding of the
1791document.</p>
1792
1793<p>It should be noted too that the HTML parser embedded in libxml now obbey
1794the same rules too, the following document will be (as of 2.2.2) handled  in
1795an internationalized fashion by libxml too:</p>
1796<pre>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
1797                      "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
1798&lt;html lang="fr"&gt;
1799&lt;head&gt;
1800  &lt;META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"&gt;
1801&lt;/head&gt;
1802&lt;body&gt;
1803&lt;p&gt;W3C cr�e des standards pour le Web.&lt;/body&gt;
1804&lt;/html&gt;</pre>
1805
1806<h3><a name="internal">The internal encoding, how and why</a></h3>
1807
1808<p>One of the core decision was to force all documents to be converted to a
1809default internal encoding, and that encoding to be UTF-8, here are the
1810rationale for those choices:</p>
1811<ul>
1812  <li>keeping the native encoding in the internal form would force the libxml
1813    users (or the code associated) to be fully aware of the encoding of the
1814    original document, for examples when adding a text node to a document,
1815    the content would have to be provided in the document encoding, i.e. the
1816    client code would have to check it before hand, make sure it's conformant
1817    to the encoding, etc ... Very hard in practice, though in some specific
1818    cases this may make sense.</li>
1819  <li>the second decision was which encoding. From the XML spec only UTF8 and
1820    UTF16 really makes sense as being the two only encodings for which there
1821    is amndatory support. UCS-4 (32 bits fixed size encoding) could be
1822    considered an intelligent choice too since it's a direct Unicode mapping
1823    support. I selected UTF-8 on the basis of efficiency and compatibility
1824    with surrounding software:
1825    <ul>
1826      <li>UTF-8 while a bit more complex to convert from/to (i.e. slightly
1827        more costly to import and export CPU wise) is also far more compact
1828        than UTF-16 (and UCS-4) for a majority of the documents I see it used
1829        for right now (RPM RDF catalogs, advogato data, various configuration
1830        file formats, etc.) and the key point for today's computer
1831        architecture is efficient uses of caches. If one nearly double the
1832        memory requirement to store the same amount of data, this will trash
1833        caches (main memory/external caches/internal caches) and my take is
1834        that this harms the system far more than the CPU requirements needed
1835        for the conversion to UTF-8</li>
1836      <li>Most of libxml version 1 users were using it with straight ASCII
1837        most of the time, doing the conversion with an internal encoding
1838        requiring all their code to be rewritten was a serious show-stopper
1839        for using UTF-16 or UCS-4.</li>
1840      <li>UTF-8 is being used as the de-facto internal encoding standard for
1841        related code like the <a href="http://www.pango.org/">pango</a>
1842        upcoming Gnome text widget, and a lot of Unix code (yep another place
1843        where Unix programmer base takes a different approach from Microsoft
1844        - they are using UTF-16)</li>
1845    </ul>
1846  </li>
1847</ul>
1848
1849<p>What does this mean in practice for the libxml user:</p>
1850<ul>
1851  <li>xmlChar, the libxml data type is a byte, those bytes must be assembled
1852    as UTF-8 valid strings. The proper way to terminate an xmlChar * string
1853    is simply to append 0 byte, as usual.</li>
1854  <li>One just need to make sure that when using chars outside the ASCII set,
1855    the values has been properly converted to UTF-8</li>
1856</ul>
1857
1858<h3><a name="implemente">How is it implemented ?</a></h3>
1859
1860<p>Let's describe how all this works within libxml, basically the I18N
1861(internationalization) support get triggered only during I/O operation, i.e.
1862when reading a document or saving one. Let's look first at the reading
1863sequence:</p>
1864<ol>
1865  <li>when a document is processed, we usually don't know the encoding, a
1866    simple heuristic allows to detect UTF-18 and UCS-4 from whose where the
1867    ASCII range (0-0x7F) maps with ASCII</li>
1868  <li>the xml declaration if available is parsed, including the encoding
1869    declaration. At that point, if the autodetected encoding is different
1870    from the one declared a call to xmlSwitchEncoding() is issued.</li>
1871  <li>If there is no encoding declaration, then the input has to be in either
1872    UTF-8 or UTF-16, if it is not then at some point when processing the
1873    input, the converter/checker of UTF-8 form will raise an encoding error.
1874    You may end-up with a garbled document, or no document at all ! Example:
1875    <pre>~/XML -&gt; /xmllint err.xml 
1876err.xml:1: error: Input is not proper UTF-8, indicate encoding !
1877&lt;tr�s&gt;l�&lt;/tr�s&gt;
1878   ^
1879err.xml:1: error: Bytes: 0xE8 0x73 0x3E 0x6C
1880&lt;tr�s&gt;l�&lt;/tr�s&gt;
1881   ^</pre>
1882  </li>
1883  <li>xmlSwitchEncoding() does an encoding name lookup, canonalize it, and
1884    then search the default registered encoding converters for that encoding.
1885    If it's not within the default set and iconv() support has been compiled
1886    it, it will ask iconv for such an encoder. If this fails then the parser
1887    will report an error and stops processing:
1888    <pre>~/XML -&gt; /xmllint err2.xml 
1889err2.xml:1: error: Unsupported encoding UnsupportedEnc
1890&lt;?xml version="1.0" encoding="UnsupportedEnc"?&gt;
1891                                             ^</pre>
1892  </li>
1893  <li>From that point the encoder process progressingly the input (it is
1894    plugged as a front-end to the I/O module) for that entity. It captures
1895    and convert on-the-fly the document to be parsed to UTF-8. The parser
1896    itself just does UTF-8 checking of this input and process it
1897    transparently. The only difference is that the encoding information has
1898    been added to the parsing context (more precisely to the input
1899    corresponding to this entity).</li>
1900  <li>The result (when using DOM) is an internal form completely in UTF-8
1901    with just an encoding information on the document node.</li>
1902</ol>
1903
1904<p>Ok then what's happen when saving the document (assuming you
1905colllected/built an xmlDoc DOM like structure) ? It depends on the function
1906called, xmlSaveFile() will just try to save in the original encoding, while
1907xmlSaveFileTo() and xmlSaveFileEnc() can optionally save to a given
1908encoding:</p>
1909<ol>
1910  <li>if no encoding is given, libxml will look for an encoding value
1911    associated to the document and if it exists will try to save to that
1912    encoding,
1913    <p>otherwise everything is written in the internal form, i.e. UTF-8</p>
1914  </li>
1915  <li>so if an encoding was specified, either at the API level or on the
1916    document, libxml will again canonalize the encoding name, lookup for a
1917    converter in the registered set or through iconv. If not found the
1918    function will return an error code</li>
1919  <li>the converter is placed before the I/O buffer layer, as another kind of
1920    buffer, then libxml will simply push the UTF-8 serialization to through
1921    that buffer, which will then progressively be converted and pushed onto
1922    the I/O layer.</li>
1923  <li>It is possible that the converter code fails on some input, for example
1924    trying to push an UTF-8 encoded chinese character through the UTF-8 to
1925    ISO-8859-1 converter won't work. Since the encoders are progressive they
1926    will just report the error and the number of bytes converted, at that
1927    point libxml will decode the offending character, remove it from the
1928    buffer and replace it with the associated charRef encoding &amp;#123; and
1929    resume the convertion. This guarante that any document will be saved
1930    without losses (except for markup names where this is not legal, this is
1931    a problem in the current version, in pactice avoid using non-ascci
1932    characters for tags or attributes names  @@). A special "ascii" encoding
1933    name is used to save documents to a pure ascii form can be used when
1934    portability is really crucial</li>
1935</ol>
1936
1937<p>Here is a few examples based on the same test document:</p>
1938<pre>~/XML -&gt; /xmllint isolat1 
1939&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
1940&lt;tr�s&gt;l�&lt;/tr�s&gt;
1941~/XML -&gt; /xmllint --encode UTF-8 isolat1 
1942&lt;?xml version="1.0" encoding="UTF-8"?&gt;
1943&lt;très&gt;l� �&lt;/très&gt;
1944~/XML -&gt; </pre>
1945
1946<p>The same processing is applied (and reuse most of the code) for HTML I18N
1947processing. Looking up and modifying the content encoding is a bit more
1948difficult since it is located in a &lt;meta&gt; tag under the &lt;head&gt;,
1949so a couple of functions htmlGetMetaEncoding() and htmlSetMetaEncoding() have
1950been provided. The parser also attempts to switch encoding on the fly when
1951detecting such a tag on input. Except for that the processing is the same
1952(and again reuses the same code).</p>
1953
1954<h3><a name="Default">Default supported encodings</a></h3>
1955
1956<p>libxml has a set of default converters for the following encodings
1957(located in encoding.c):</p>
1958<ol>
1959  <li>UTF-8 is supported by default (null handlers)</li>
1960  <li>UTF-16, both little and big endian</li>
1961  <li>ISO-Latin-1 (ISO-8859-1) covering most western languages</li>
1962  <li>ASCII, useful mostly for saving</li>
1963  <li>HTML, a specific handler for the conversion of UTF-8 to ASCII with HTML
1964    predefined entities like &amp;copy; for the Copyright sign.</li>
1965</ol>
1966
1967<p>More over when compiled on an Unix platfor with iconv support the full set
1968of encodings supported by iconv can be instantly be used by libxml. On a
1969linux machine with glibc-2.1 the list of supported encodings and aliases fill
19703 full pages, and include UCS-4, the full set of ISO-Latin encodings, and the
1971various Japanese ones.</p>
1972
1973<h4>Encoding aliases</h4>
1974
1975<p>From 2.2.3, libxml has support to register encoding names aliases. The
1976goal is to be able to parse document whose encoding is supported but where
1977the name differs (for example from the default set of names accepted by
1978iconv). The following functions allow to register and handle new aliases for
1979existing encodings. Once registered libxml will automatically lookup the
1980aliases when handling a document:</p>
1981<ul>
1982  <li>int xmlAddEncodingAlias(const char *name, const char *alias);</li>
1983  <li>int xmlDelEncodingAlias(const char *alias);</li>
1984  <li>const char * xmlGetEncodingAlias(const char *alias);</li>
1985  <li>void xmlCleanupEncodingAliases(void);</li>
1986</ul>
1987
1988<h3><a name="extend">How to extend the existing support</a></h3>
1989
1990<p>Well adding support for new encoding, or overriding one of the encoders
1991(assuming it is buggy) should not be hard, just write an input and output
1992conversion routines to/from UTF-8, and register them using
1993xmlNewCharEncodingHandler(name, xxxToUTF8, UTF8Toxxx),  and they will be
1994called automatically if the parser(s) encounter such an encoding name
1995(register it uppercase, this will help). The description of the encoders,
1996their arguments and expected return values are described in the encoding.h
1997header.</p>
1998
1999<p>A quick note on the topic of subverting the parser to use a different
2000internal encoding than UTF-8, in some case people will absolutely want to
2001keep the internal encoding different, I think it's still possible (but the
2002encoding must be compliant with ASCII on the same subrange) though I didn't
2003tried it. The key is to override the default conversion routines (by
2004registering null encoders/decoders for your charsets), and bypass the UTF-8
2005checking of the parser by setting the parser context charset
2006(ctxt-&gt;charset) to something different than XML_CHAR_ENCODING_UTF8, but
2007there is no guarantee taht this will work. You may also have some troubles
2008saving back.</p>
2009
2010<p>Basically proper I18N support is important, this requires at least
2011libxml-2.0.0, but a lot of features and corrections are really available only
2012starting 2.2.</p>
2013
2014<h2><a name="IO">I/O Interfaces</a></h2>
2015
2016<p>Table of Content:</p>
2017<ol>
2018  <li><a href="#General1">General overview</a></li>
2019  <li><a href="#basic">The basic buffer type</a></li>
2020  <li><a href="#Input">Input I/O handlers</a></li>
2021  <li><a href="#Output">Output I/O handlers</a></li>
2022  <li><a href="#entities">The entities loader</a></li>
2023  <li><a href="#Example2">Example of customized I/O</a></li>
2024</ol>
2025
2026<h3><a name="General1">General overview</a></h3>
2027
2028<p>The module <code><a
2029href="http://xmlsoft.org/html/libxml-xmlio.html">xmlIO.h</a></code> provides
2030the interfaces to the libxml I/O system. This consists of 4 main parts:</p>
2031<ul>
2032  <li>Entities loader, this is a routine which tries to fetch the entities
2033    (files) based on their PUBLIC and SYSTEM identifiers. The default loader
2034    don't look at the public identifier since libxml do not maintain a
2035    catalog. You can redefine you own entity loader by using
2036    <code>xmlGetExternalEntityLoader()</code> and
2037    <code>xmlSetExternalEntityLoader()</code>. <a href="#entities">Check the
2038    example</a>.</li>
2039  <li>Input I/O buffers which are a commodity structure used by the parser(s)
2040    input layer to handle fetching the informations to feed the parser. This
2041    provides buffering and is also a placeholder where the encoding
2042    convertors to UTF8 are piggy-backed.</li>
2043  <li>Output I/O buffers are similar to the Input ones and fulfill similar
2044    task but when generating a serialization from a tree.</li>
2045  <li>A mechanism to register sets of I/O callbacks and associate them with
2046    specific naming schemes like the protocol part of the URIs.
2047    <p>This affect the default I/O operations and allows to use specific I/O
2048    handlers for certain names.</p>
2049  </li>
2050</ul>
2051
2052<p>The general mechanism used when loading http://rpmfind.net/xml.html for
2053example in the HTML parser is the following:</p>
2054<ol>
2055  <li>The default entity loader calls <code>xmlNewInputFromFile()</code> with
2056    the parsing context and the URI string.</li>
2057  <li>the URI string is checked against the existing registered handlers
2058    using their match() callback function, if the HTTP module was compiled
2059    in, it is registered and its match() function will succeeds</li>
2060  <li>the open() function of the handler is called and if successful will
2061    return an I/O Input buffer</li>
2062  <li>the parser will the start reading from this buffer and progressively
2063    fetch information from the resource, calling the read() function of the
2064    handler until the resource is exhausted</li>
2065  <li>if an encoding change is detected it will be installed on the input
2066    buffer, providing buffering and efficient use of the conversion
2067  routines</li>
2068  <li>once the parser has finished, the close() function of the handler is
2069    called once and the Input buffer and associed resources are
2070  deallocated.</li>
2071</ol>
2072
2073<p>The user defined callbacks are checked first to allow overriding of the
2074default libxml I/O routines.</p>
2075
2076<h3><a name="basic">The basic buffer type</a></h3>
2077
2078<p>All the buffer manipulation handling is done using the
2079<code>xmlBuffer</code> type define in <code><a
2080href="http://xmlsoft.org/html/libxml-tree.html">tree.h</a> </code>which is a
2081resizable memory buffer. The buffer allocation strategy can be selected to be
2082either best-fit or use an exponential doubling one (CPU vs. memory use
2083tradeoff). The values are <code>XML_BUFFER_ALLOC_EXACT</code> and
2084<code>XML_BUFFER_ALLOC_DOUBLEIT</code>, and can be set individually or on a
2085system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number
2086of functions allows to manipulate buffers with names starting with the
2087<code>xmlBuffer...</code> prefix.</p>
2088
2089<h3><a name="Input">Input I/O handlers</a></h3>
2090
2091<p>An Input I/O handler is a simple structure
2092<code>xmlParserInputBuffer</code> containing a context associated to the
2093resource (file descriptor, or pointer to a protocol handler), the read() and
2094close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset
2095encoding handler are also present to support charset conversion when
2096needed.</p>
2097
2098<h3><a name="Output">Output I/O handlers</a></h3>
2099
2100<p>An Output handler <code>xmlOutputBuffer</code> is completely similar to an
2101Input one except the callbacks are write() and close().</p>
2102
2103<h3><a name="entities">The entities loader</a></h3>
2104
2105<p>The entity loader resolves requests for new entities and create inputs for
2106the parser. Creating an input from a filename or an URI string is done
2107through the xmlNewInputFromFile() routine.  The default entity loader do not
2108handle the PUBLIC identifier associated with an entity (if any). So it just
2109calls xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in
2110XML).</p>
2111
2112<p>If you want to hook up a catalog mechanism then you simply need to
2113override the default entity loader, here is an example:</p>
2114<pre>#include &lt;libxml/xmlIO.h&gt;
2115
2116xmlExternalEntityLoader defaultLoader = NULL;
2117
2118xmlParserInputPtr
2119xmlMyExternalEntityLoader(const char *URL, const char *ID,
2120                               xmlParserCtxtPtr ctxt) {
2121    xmlParserInputPtr ret;
2122    const char *fileID = NULL;
2123    /* lookup for the fileID depending on ID */
2124
2125    ret = xmlNewInputFromFile(ctxt, fileID);
2126    if (ret != NULL)
2127        return(ret);
2128    if (defaultLoader != NULL)
2129        ret = defaultLoader(URL, ID, ctxt);
2130    return(ret);
2131}
2132
2133int main(..) {
2134    ...
2135
2136    /*
2137     * Install our own entity loader
2138     */
2139    defaultLoader = xmlGetExternalEntityLoader();
2140    xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
2141
2142    ...
2143}</pre>
2144
2145<h3><a name="Example2">Example of customized I/O</a></h3>
2146
2147<p>This example come from <a href="http://xmlsoft.org/messages/0708.html">a
2148real use case</a>,  xmlDocDump() closes the FILE * passed by the application
2149and this was a problem. The <a
2150href="http://xmlsoft.org/messages/0711.html">solution</a> was to redefine a
2151new output handler with the closing call deactivated:</p>
2152<ol>
2153  <li>First define a new I/O ouput allocator where the output don't close the
2154    file:
2155    <pre>xmlOutputBufferPtr
2156xmlOutputBufferCreateOwn(FILE *file, xmlCharEncodingHandlerPtr encoder) {
2157����xmlOutputBufferPtr ret;
2158����
2159����if (xmlOutputCallbackInitialized == 0)
2160��������xmlRegisterDefaultOutputCallbacks();
2161
2162����if (file == NULL) return(NULL);
2163����ret = xmlAllocOutputBuffer(encoder);
2164����if (ret != NULL) {
2165��������ret-&gt;context = file;
2166��������ret-&gt;writecallback = xmlFileWrite;
2167��������ret-&gt;closecallback = NULL;  /* No close callback */
2168����}
2169����return(ret); <br>
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184} </pre>
2185  </li>
2186  <li>And then use it to save the document:
2187    <pre>FILE *f;
2188xmlOutputBufferPtr output;
2189xmlDocPtr doc;
2190int res;
2191
2192f = ...
2193doc = ....
2194
2195output = xmlOutputBufferCreateOwn(f, NULL);
2196res = xmlSaveFileTo(output, doc, NULL);
2197    </pre>
2198  </li>
2199</ol>
2200
2201<h2><a name="Catalog">Catalog support</a></h2>
2202
2203<p>Table of Content:</p>
2204<ol>
2205  <li><a href="General2">General overview</a></li>
2206  <li><a href="#definition">The definition</a></li>
2207  <li><a href="#Simple">Using catalogs</a></li>
2208  <li><a href="#Some">Some examples</a></li>
2209  <li><a href="#reference">How to tune  catalog usage</a></li>
2210  <li><a href="#validate">How to debug catalog processing</a></li>
2211  <li><a href="#Declaring">How to create and maintain catalogs</a></li>
2212  <li><a href="#implemento">The implementor corner quick review of the
2213  API</a></li>
2214  <li><a href="#Other">Other resources</a></li>
2215</ol>
2216
2217<h3><a name="General2">General overview</a></h3>
2218
2219<p>What is a catalog? Basically it's a lookup mechanism used when an entity
2220(a file or a remote resource) references another entity. The catalog lookup
2221is inserted between the moment the reference is recognized by the software
2222(XML parser, stylesheet processing, or even images referenced for inclusion
2223in a rendering) and the time where loading that resource is actually
2224started.</p>
2225
2226<p>It is basically used for 3 things:</p>
2227<ul>
2228  <li>mapping from "logical" names, the public identifiers and a more
2229    concrete name usable for download (and URI). For example it can associate
2230    the logical name
2231    <p>"-//OASIS//DTD DocBook XML V4.1.2//EN"</p>
2232    <p>of the DocBook 4.1.2 XML DTD with the actual URL where it can be
2233    downloaded</p>
2234    <p>http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd</p>
2235  </li>
2236  <li>remapping from a given URL to another one, like an HTTP indirection
2237    saying that
2238    <p>"http://www.oasis-open.org/committes/tr.xsl"</p>
2239    <p>should really be looked at</p>
2240    <p>"http://www.oasis-open.org/committes/entity/stylesheets/base/tr.xsl"</p>
2241  </li>
2242  <li>providing a local cache mechanism allowing to load the entities
2243    associated to public identifiers or remote resources, this is a really
2244    important feature for any significant deployment of XML or SGML since it
2245    allows to avoid the aleas and delays associated to fetching remote
2246    resources.</li>
2247</ul>
2248
2249<h3><a name="definition">The definitions</a></h3>
2250
2251<p>Libxml, as of 2.4.3 implements 2 kind of catalogs:</p>
2252<ul>
2253  <li>the older SGML catalogs, the official spec is  SGML Open Technical
2254    Resolution TR9401:1997, but is better understood by reading <a
2255    href="http://www.jclark.com/sp/catalog.htm">the SP Catalog page</a> from
2256    James Clark. This is relatively old and not the preferred mode of
2257    operation of libxml.</li>
2258  <li><a href="http://www.oasis-open.org/committees/entity/spec.html">XML
2259    Catalogs</a>
2260     is far more flexible, more recent, uses an XML syntax and should scale
2261    quite better. This is the default option of libxml.</li>
2262</ul>
2263
2264<p></p>
2265
2266<h3><a name="Simple">Using catalog</a></h3>
2267
2268<p>In a normal environment libxml will by default check the presence of a
2269catalog in /etc/xml/catalog, and assuming it has been correctly populated,
2270the processing is completely transparent to the document user. To take a
2271concrete example, suppose you are authoring a DocBook document, this one
2272starts with the following DOCTYPE definition:</p>
2273<pre>&lt;?xml version='1.0'?&gt;
2274&lt;!DOCTYPE book PUBLIC "-//Norman Walsh//DTD DocBk XML V3.1.4//EN"
2275          "http://nwalsh.com/docbook/xml/3.1.4/db3xml.dtd"&gt;</pre>
2276
2277<p>When validating the document with libxml, the catalog will be
2278automatically consulted to lookup the public identifier "-//Norman Walsh//DTD
2279DocBk XML V3.1.4//EN" and the system identifier
2280"http://nwalsh.com/docbook/xml/3.1.4/db3xml.dtd", and if these entities have
2281been installed on your system and the catalogs actually point to them, libxml
2282will fetch them from the local disk.</p>
2283
2284<p style="font-size: 10pt"><strong>Note</strong>: Really don't use this
2285DOCTYPE example it's a really old version, but is fine as an example.</p>
2286
2287<p>Libxml will check the catalog each time that it is requested to load an
2288entity, this includes DTD, external parsed entities, stylesheets, etc ... If
2289your system is correctly configured all the authoring phase and processing
2290should use only local files, even if your document stays portable because it
2291uses the canonical public and system ID, referencing the remote document.</p>
2292
2293<h3><a name="Some">Some examples:</a></h3>
2294
2295<p>Here is a couple of fragments from XML Catalogs used in libxml early
2296regression tests in <code>test/catalogs</code> :</p>
2297<pre>&lt;?xml version="1.0"?&gt;
2298&lt;!DOCTYPE catalog PUBLIC 
2299   "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
2300   "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"&gt;
2301&lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"&gt;
2302  &lt;public publicId="-//OASIS//DTD DocBook XML V4.1.2//EN"
2303   uri="http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"/&gt;
2304...</pre>
2305
2306<p>This is the beginning of a catalog for DocBook 4.1.2, XML Catalogs are
2307written in XML,  there is a specific namespace for catalog elements
2308"urn:oasis:names:tc:entity:xmlns:xml:catalog". The first entry in this
2309catalog is a <code>public</code> mapping it allows to associate a Public
2310Identifier with an URI.</p>
2311<pre>...
2312    &lt;rewriteSystem systemIdStartString="http://www.oasis-open.org/docbook/"
2313                   rewritePrefix="file:///usr/share/xml/docbook/"/&gt;
2314...</pre>
2315
2316<p>A <code>rewriteSystem</code> is a very powerful instruction, it says that
2317any URI starting with a given prefix should be looked at another  URI
2318constructed by replacing the prefix with an new one. In effect this acts like
2319a cache system for a full area of the Web. In practice it is extremely useful
2320with a file prefix if you have installed a copy of those resources on your
2321local system.</p>
2322<pre>...
2323&lt;delegatePublic publicIdStartString="-//OASIS//DTD XML Catalog //"
2324                catalog="file:///usr/share/xml/docbook.xml"/&gt;
2325&lt;delegatePublic publicIdStartString="-//OASIS//ENTITIES DocBook XML"
2326                catalog="file:///usr/share/xml/docbook.xml"/&gt;
2327&lt;delegatePublic publicIdStartString="-//OASIS//DTD DocBook XML"
2328                catalog="file:///usr/share/xml/docbook.xml"/&gt;
2329&lt;delegateSystem systemIdStartString="http://www.oasis-open.org/docbook/"
2330                catalog="file:///usr/share/xml/docbook.xml"/&gt;
2331&lt;delegateURI uriStartString="http://www.oasis-open.org/docbook/"
2332                catalog="file:///usr/share/xml/docbook.xml"/&gt;
2333...</pre>
2334
2335<p>Delegation is the core features which allows to build a tree of catalogs,
2336easier to maintain than a single catalog, based on Public Identifier, System
2337Identifier or URI prefixes it instructs the catalog software to look up
2338entries in another resource. This feature allow to build hierarchies of
2339catalogs, the set of entries presented should be sufficient to redirect the
2340resolution of all DocBook references to the specific catalog in
2341<code>/usr/share/xml/docbook.xml</code> this one in turn could delegate all
2342references for DocBook 4.2.1 to a specific catalog installed at the same time
2343as the DocBook resources on the local machine.</p>
2344
2345<h3><a name="reference">How to tune catalog usage:</a></h3>
2346
2347<p>The user can change the default catalog behaviour by redirecting queries
2348to its own set of catalogs, this can be done by setting the
2349<code>XML_CATALOG_FILES</code> environment variable to a list of catalogs, an
2350empty one should deactivate loading the default <code>/etc/xml/catalog</code>
2351default catalog</p>
2352
2353<h3><a name="validate">How to debug catalog processing:</a></h3>
2354
2355<p>Setting up the <code>XML_DEBUG_CATALOG</code> environment variable will
2356make libxml output debugging informations for each catalog operations, for
2357example:</p>
2358<pre>orchis:~/XML -&gt; xmllint --memory --noout test/ent2
2359warning: failed to load external entity "title.xml"
2360orchis:~/XML -&gt; export XML_DEBUG_CATALOG=
2361orchis:~/XML -&gt; xmllint --memory --noout test/ent2
2362Failed to parse catalog /etc/xml/catalog
2363Failed to parse catalog /etc/xml/catalog
2364warning: failed to load external entity "title.xml"
2365Catalogs cleanup
2366orchis:~/XML -&gt; </pre>
2367
2368<p>The test/ent2 references an entity, running the parser from memory makes
2369the base URI unavailable and the the "title.xml" entity cannot be loaded.
2370Setting up the debug environment variable allows to detect that an attempt is
2371made to load the <code>/etc/xml/catalog</code> but since it's not present the
2372resolution fails.</p>
2373
2374<p>But the most advanced way to debug XML catalog processing is to use the
2375<strong>xmlcatalog</strong> command shipped with libxml2, it allows to load
2376catalogs and make resolution queries to see what is going on. This is also
2377used for the regression tests:</p>
2378<pre>orchis:~/XML -&gt; /xmlcatalog test/catalogs/docbook.xml \
2379                   "-//OASIS//DTD DocBook XML V4.1.2//EN"
2380http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd
2381orchis:~/XML -&gt; </pre>
2382
2383<p>For debugging what is going on, adding one -v flags increase the verbosity
2384level to indicate the processing done (adding a second flag also indicate
2385what elements are recognized at parsing):</p>
2386<pre>orchis:~/XML -&gt; /xmlcatalog -v test/catalogs/docbook.xml \
2387                   "-//OASIS//DTD DocBook XML V4.1.2//EN"
2388Parsing catalog test/catalogs/docbook.xml's content
2389Found public match -//OASIS//DTD DocBook XML V4.1.2//EN
2390http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd
2391Catalogs cleanup
2392orchis:~/XML -&gt; </pre>
2393
2394<p>A shell interface is also available to debug and process multiple queries
2395(and for regression tests):</p>
2396<pre>orchis:~/XML -&gt; /xmlcatalog -shell test/catalogs/docbook.xml \
2397                   "-//OASIS//DTD DocBook XML V4.1.2//EN"
2398&gt; help   
2399Commands available:
2400public PublicID: make a PUBLIC identifier lookup
2401system SystemID: make a SYSTEM identifier lookup
2402resolve PublicID SystemID: do a full resolver lookup
2403add 'type' 'orig' 'replace' : add an entry
2404del 'values' : remove values
2405dump: print the current catalog state
2406debug: increase the verbosity level
2407quiet: decrease the verbosity level
2408exit:  quit the shell
2409&gt; public "-//OASIS//DTD DocBook XML V4.1.2//EN"
2410http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd
2411&gt; quit
2412orchis:~/XML -&gt; </pre>
2413
2414<p>This should be sufficient for most debugging purpose, this was actually
2415used heavily to debug the XML Catalog implementation itself.</p>
2416
2417<h3><a name="Declaring">How to create and maintain</a> catalogs:</h3>
2418
2419<p>Basically XML Catalogs are XML files, you can either use XML tools to
2420manage them or use  <strong>xmlcatalog</strong> for this. The basic step is
2421to create a catalog the -create option provide this facility:</p>
2422<pre>orchis:~/XML -&gt; /xmlcatalog --create tst.xml
2423&lt;?xml version="1.0"?&gt;
2424&lt;!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
2425         "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"&gt;
2426&lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"/&gt;
2427orchis:~/XML -&gt; </pre>
2428
2429<p>By default xmlcatalog does not overwrite the original catalog and save the
2430result on the standard output, this can be overridden using the -noout
2431option. The <code>-add</code> command allows to add entries in the
2432catalog:</p>
2433<pre>orchis:~/XML -&gt; /xmlcatalog --noout --create --add "public" \
2434  "-//OASIS//DTD DocBook XML V4.1.2//EN" \
2435  http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd tst.xml
2436orchis:~/XML -&gt; cat tst.xml
2437&lt;?xml version="1.0"?&gt;
2438&lt;!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" \
2439  "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"&gt;
2440&lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"&gt;
2441&lt;public publicId="-//OASIS//DTD DocBook XML V4.1.2//EN"
2442        uri="http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"/&gt;
2443&lt;/catalog&gt;
2444orchis:~/XML -&gt; </pre>
2445
2446<p>The <code>-add</code> option will always take 3 parameters even if some of
2447the XML Catalog constructs (like nextCatalog) will have only a single
2448argument, just pass a third empty string, it will be ignored.</p>
2449
2450<p>Similarly the <code>-del</code> option remove matching entries from the
2451catalog:</p>
2452<pre>orchis:~/XML -&gt; /xmlcatalog --del \
2453  "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" tst.xml
2454&lt;?xml version="1.0"?&gt;
2455&lt;!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
2456    "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"&gt;
2457&lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"/&gt;
2458orchis:~/XML -&gt; </pre>
2459
2460<p>The catalog is now empty. Note that the matching of <code>-del</code> is
2461exact and would have worked in a similar fashion with the Public ID
2462string.</p>
2463
2464<p>This is rudimentary but should be sufficient to manage a not too complex
2465catalog tree of resources.</p>
2466
2467<h3><a name="implemento">The implementor corner quick review of the
2468API:</a></h3>
2469
2470<p>First, and like for every other module of libxml, there is an
2471automatically generated <a href="html/libxml-catalog.html">API page for
2472catalog support</a>.</p>
2473
2474<p>The header for the catalog interfaces should be included as:</p>
2475<pre>#include &lt;libxml/catalog.h&gt;</pre>
2476
2477<p>The API is voluntarily kept very simple. First it is not obvious that
2478applications really need access to it since it is the default behaviour of
2479libxml (Note: it is possible to completely override libxml default catalog by
2480using <a href="html/libxml-parser.html">xmlSetExternalEntityLoader</a> to
2481plug an application specific resolver).</p>
2482
2483<p>Basically libxml support 2 catalog lists:</p>
2484<ul>
2485  <li>the default one, global shared by all the application</li>
2486  <li>a per-document catalog, this one is built if the document uses the
2487    <code>oasis-xml-catalog</code> PIs to specify its own catalog list, it is
2488    associated to the parser context and destroyed when the parsing context
2489    is destroyed.</li>
2490</ul>
2491
2492<p>the document one will be used first if it exists.</p>
2493
2494<h4>Initialization routines:</h4>
2495
2496<p>xmlInitializeCatalog(), xmlLoadCatalog() and xmlLoadCatalogs() should be
2497used at startup to initialize the catalog, if the catalog should be
2498initialized with specific values xmlLoadCatalog()  or xmlLoadCatalogs()
2499should be called before xmlInitializeCatalog() which would otherwise do a
2500default initialization first.</p>
2501
2502<p>The xmlCatalogAddLocal() call is used by the parser to grow the document
2503own catalog list if needed.</p>
2504
2505<h4>Preferences setup:</h4>
2506
2507<p>The XML Catalog spec requires the possibility to select default
2508preferences between  public and system delegation,
2509xmlCatalogSetDefaultPrefer() allows this, xmlCatalogSetDefaults() and
2510xmlCatalogGetDefaults() allow to control  if XML Catalogs resolution should
2511be forbidden, allowed for global catalog, for document catalog or both, the
2512default is to allow both.</p>
2513
2514<p>And of course xmlCatalogSetDebug() allows to generate debug messages
2515(through the xmlGenericError() mechanism).</p>
2516
2517<h4>Querying routines:</h4>
2518
2519<p>xmlCatalogResolve(), xmlCatalogResolveSystem(), xmlCatalogResolvePublic()
2520and xmlCatalogResolveURI() are relatively explicit if you read the XML
2521Catalog specification they correspond to section 7 algorithms, they should
2522also work if you have loaded an SGML catalog with a simplified semantic.</p>
2523
2524<p>xmlCatalogLocalResolve() and xmlCatalogLocalResolveURI() are the same but
2525operate on the document catalog list</p>
2526
2527<h4>Cleanup and Miscellaneous:</h4>
2528
2529<p>xmlCatalogCleanup() free-up the global catalog, xmlCatalogFreeLocal() is
2530the per-document equivalent.</p>
2531
2532<p>xmlCatalogAdd() and xmlCatalogRemove() are used to dynamically modify the
2533first catalog in the global list, and xmlCatalogDump() allows to dump a
2534catalog state, those routines are primarily designed for xmlcatalog, I'm not
2535sure that exposing more complex interfaces (like navigation ones) would be
2536really useful.</p>
2537
2538<p>The xmlParseCatalogFile() is a function used to load XML Catalog files,
2539it's similar as xmlParseFile() except it bypass all catalog lookups, it's
2540provided because this functionality may be useful for client tools.</p>
2541
2542<h4>threaded environments:</h4>
2543
2544<p>Since the catalog tree is built progressively, some care has been taken to
2545try to avoid troubles in multithreaded environments. The code is now thread
2546safe assuming that the libxml library has been compiled with threads
2547support.</p>
2548
2549<p></p>
2550
2551<h3><a name="Other">Other resources</a></h3>
2552
2553<p>The XML Catalog specification is relatively recent so there isn't much
2554literature to point at:</p>
2555<ul>
2556  <li>You can find an good rant from Norm Walsh about <a
2557    href="http://www.arbortext.com/Think_Tank/XML_Resources/Issue_Three/issue_three.html">the
2558    need for catalogs</a>, it provides a lot of context informations even if
2559    I don't agree with everything presented.</li>
2560  <li>An <a href="http://home.ccil.org/~cowan/XML/XCatalog.html">old XML
2561    catalog proposal</a> from John Cowan</li>
2562  <li>The <a href="http://www.rddl.org/">Resource Directory Description
2563    Language</a> (RDDL) another catalog system but more oriented toward
2564    providing metadata for XML namespaces.</li>
2565  <li>the page from the OASIS Technical <a
2566    href="http://www.oasis-open.org/committees/entity/">Committee on Entity
2567    Resolution</a> who maintains XML Catalog, you will find pointers to the
2568    specification update, some background and pointers to others tools
2569    providing XML Catalog support</li>
2570  <li>I have uploaded <a href="ftp://xmlsoft.org/test/dbk412catalog.tar.gz">a
2571    mall tarball</a> containing XML Catalogs for DocBook 4.1.2 which seems to
2572    work fine for me</li>
2573  <li>The <a href="http://www.xmlsoft.org/xmlcatalog_man.html">xmlcatalog
2574    manual page</a></li>
2575</ul>
2576
2577<p>If you have suggestions for corrections or additions, simply contact
2578me:</p>
2579
2580<h2><a name="library">The parser interfaces</a></h2>
2581
2582<p>This section is directly intended to help programmers getting bootstrapped
2583using the XML library from the C language. It is not intended to be
2584extensive. I hope the automatically generated documents will provide the
2585completeness required, but as a separate set of documents. The interfaces of
2586the XML library are by principle low level, there is nearly zero abstraction.
2587Those interested in a higher level API should <a href="#DOM">look at
2588DOM</a>.</p>
2589
2590<p>The <a href="html/libxml-parser.html">parser interfaces for XML</a> are
2591separated from the <a href="html/libxml-htmlparser.html">HTML parser
2592interfaces</a>.  Let's have a look at how the XML parser can be called:</p>
2593
2594<h3><a name="Invoking">Invoking the parser : the pull method</a></h3>
2595
2596<p>Usually, the first thing to do is to read an XML input. The parser accepts
2597documents either from in-memory strings or from files.  The functions are
2598defined in "parser.h":</p>
2599<dl>
2600  <dt><code>xmlDocPtr xmlParseMemory(char *buffer, int size);</code></dt>
2601    <dd><p>Parse a null-terminated string containing the document.</p>
2602    </dd>
2603</dl>
2604<dl>
2605  <dt><code>xmlDocPtr xmlParseFile(const char *filename);</code></dt>
2606    <dd><p>Parse an XML document contained in a (possibly compressed)
2607      file.</p>
2608    </dd>
2609</dl>
2610
2611<p>The parser returns a pointer to the document structure (or NULL in case of
2612failure).</p>
2613
2614<h3 id="Invoking1">Invoking the parser: the push method</h3>
2615
2616<p>In order for the application to keep the control when the document is
2617being fetched (which is common for GUI based programs) libxml provides a push
2618interface, too, as of version 1.8.3. Here are the interface functions:</p>
2619<pre>xmlParserCtxtPtr xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax,
2620                                         void *user_data,
2621                                         const char *chunk,
2622                                         int size,
2623                                         const char *filename);
2624int              xmlParseChunk          (xmlParserCtxtPtr ctxt,
2625                                         const char *chunk,
2626                                         int size,
2627                                         int terminate);</pre>
2628
2629<p>and here is a simple example showing how to use the interface:</p>
2630<pre>            FILE *f;
2631
2632            f = fopen(filename, "r");
2633            if (f != NULL) {
2634                int res, size = 1024;
2635                char chars[1024];
2636                xmlParserCtxtPtr ctxt;
2637
2638                res = fread(chars, 1, 4, f);
2639                if (res &gt; 0) {
2640                    ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2641                                chars, res, filename);
2642                    while ((res = fread(chars, 1, size, f)) &gt; 0) {
2643                        xmlParseChunk(ctxt, chars, res, 0);
2644                    }
2645                    xmlParseChunk(ctxt, chars, 0, 1);
2646                    doc = ctxt-&gt;myDoc;
2647                    xmlFreeParserCtxt(ctxt);
2648                }
2649            }</pre>
2650
2651<p>The HTML parser embedded into libxml also has a push interface; the
2652functions are just prefixed by "html" rather than "xml".</p>
2653
2654<h3 id="Invoking2">Invoking the parser: the SAX interface</h3>
2655
2656<p>The tree-building interface makes the parser memory-hungry, first loading
2657the document in memory and then building the tree itself. Reading a document
2658without building the tree is possible using the SAX interfaces (see SAX.h and
2659<a href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">James
2660Henstridge's documentation</a>). Note also that the push interface can be
2661limited to SAX: just use the two first arguments of
2662<code>xmlCreatePushParserCtxt()</code>.</p>
2663
2664<h3><a name="Building">Building a tree from scratch</a></h3>
2665
2666<p>The other way to get an XML tree in memory is by building it. Basically
2667there is a set of functions dedicated to building new elements. (These are
2668also described in &lt;libxml/tree.h&gt;.) For example, here is a piece of
2669code that produces the XML document used in the previous examples:</p>
2670<pre>    #include &lt;libxml/tree.h&gt;
2671    xmlDocPtr doc;
2672    xmlNodePtr tree, subtree;
2673
2674    doc = xmlNewDoc("1.0");
2675    doc-&gt;children = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
2676    xmlSetProp(doc-&gt;children, "prop1", "gnome is great");
2677    xmlSetProp(doc-&gt;children, "prop2", "&amp; linux too");
2678    tree = xmlNewChild(doc-&gt;children, NULL, "head", NULL);
2679    subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
2680    tree = xmlNewChild(doc-&gt;children, NULL, "chapter", NULL);
2681    subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
2682    subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
2683    subtree = xmlNewChild(tree, NULL, "image", NULL);
2684    xmlSetProp(subtree, "href", "linus.gif");</pre>
2685
2686<p>Not really rocket science ...</p>
2687
2688<h3><a name="Traversing">Traversing the tree</a></h3>
2689
2690<p>Basically by <a href="html/libxml-tree.html">including "tree.h"</a> your
2691code has access to the internal structure of all the elements of the tree.
2692The names should be somewhat simple like <strong>parent</strong>,
2693<strong>children</strong>, <strong>next</strong>, <strong>prev</strong>,
2694<strong>properties</strong>, etc... For example, still with the previous
2695example:</p>
2696<pre><code>doc-&gt;children-&gt;children-&gt;children</code></pre>
2697
2698<p>points to the title element,</p>
2699<pre>doc-&gt;children-&gt;children-&gt;next-&gt;children-&gt;children</pre>
2700
2701<p>points to the text node containing the chapter title "The Linux
2702adventure".</p>
2703
2704<p><strong>NOTE</strong>: XML allows <em>PI</em>s and <em>comments</em> to be
2705present before the document root, so <code>doc-&gt;children</code> may point
2706to an element which is not the document Root Element; a function
2707<code>xmlDocGetRootElement()</code> was added for this purpose.</p>
2708
2709<h3><a name="Modifying">Modifying the tree</a></h3>
2710
2711<p>Functions are provided for reading and writing the document content. Here
2712is an excerpt from the <a href="html/libxml-tree.html">tree API</a>:</p>
2713<dl>
2714  <dt><code>xmlAttrPtr xmlSetProp(xmlNodePtr node, const xmlChar *name, const
2715  xmlChar *value);</code></dt>
2716    <dd><p>This sets (or changes) an attribute carried by an ELEMENT node.
2717      The value can be NULL.</p>
2718    </dd>
2719</dl>
2720<dl>
2721  <dt><code>const xmlChar *xmlGetProp(xmlNodePtr node, const xmlChar
2722  *name);</code></dt>
2723    <dd><p>This function returns a pointer to new copy of the property
2724      content. Note that the user must deallocate the result.</p>
2725    </dd>
2726</dl>
2727
2728<p>Two functions are provided for reading and writing the text associated
2729with elements:</p>
2730<dl>
2731  <dt><code>xmlNodePtr xmlStringGetNodeList(xmlDocPtr doc, const xmlChar
2732  *value);</code></dt>
2733    <dd><p>This function takes an "external" string and converts it to one
2734      text node or possibly to a list of entity and text nodes. All
2735      non-predefined entity references like &amp;Gnome; will be stored
2736      internally as entity nodes, hence the result of the function may not be
2737      a single node.</p>
2738    </dd>
2739</dl>
2740<dl>
2741  <dt><code>xmlChar *xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int
2742  inLine);</code></dt>
2743    <dd><p>This function is the inverse of
2744      <code>xmlStringGetNodeList()</code>. It generates a new string
2745      containing the content of the text and entity nodes. Note the extra
2746      argument inLine. If this argument is set to 1, the function will expand
2747      entity references.  For example, instead of returning the &amp;Gnome;
2748      XML encoding in the string, it will substitute it with its value (say,
2749      "GNU Network Object Model Environment").</p>
2750    </dd>
2751</dl>
2752
2753<h3><a name="Saving">Saving a tree</a></h3>
2754
2755<p>Basically 3 options are possible:</p>
2756<dl>
2757  <dt><code>void xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int
2758  *size);</code></dt>
2759    <dd><p>Returns a buffer into which the document has been saved.</p>
2760    </dd>
2761</dl>
2762<dl>
2763  <dt><code>extern void xmlDocDump(FILE *f, xmlDocPtr doc);</code></dt>
2764    <dd><p>Dumps a document to an open file descriptor.</p>
2765    </dd>
2766</dl>
2767<dl>
2768  <dt><code>int xmlSaveFile(const char *filename, xmlDocPtr cur);</code></dt>
2769    <dd><p>Saves the document to a file. In this case, the compression
2770      interface is triggered if it has been turned on.</p>
2771    </dd>
2772</dl>
2773
2774<h3><a name="Compressio">Compression</a></h3>
2775
2776<p>The library transparently handles compression when doing file-based
2777accesses. The level of compression on saves can be turned on either globally
2778or individually for one file:</p>
2779<dl>
2780  <dt><code>int  xmlGetDocCompressMode (xmlDocPtr doc);</code></dt>
2781    <dd><p>Gets the document compression ratio (0-9).</p>
2782    </dd>
2783</dl>
2784<dl>
2785  <dt><code>void xmlSetDocCompressMode (xmlDocPtr doc, int mode);</code></dt>
2786    <dd><p>Sets the document compression ratio.</p>
2787    </dd>
2788</dl>
2789<dl>
2790  <dt><code>int  xmlGetCompressMode(void);</code></dt>
2791    <dd><p>Gets the default compression ratio.</p>
2792    </dd>
2793</dl>
2794<dl>
2795  <dt><code>void xmlSetCompressMode(int mode);</code></dt>
2796    <dd><p>Sets the default compression ratio.</p>
2797    </dd>
2798</dl>
2799
2800<h2><a name="Entities">Entities or no entities</a></h2>
2801
2802<p>Entities in principle are similar to simple C macros. An entity defines an
2803abbreviation for a given string that you can reuse many times throughout the
2804content of your document. Entities are especially useful when a given string
2805may occur frequently within a document, or to confine the change needed to a
2806document to a restricted area in the internal subset of the document (at the
2807beginning). Example:</p>
2808<pre>1 &lt;?xml version="1.0"?&gt;
28092 &lt;!DOCTYPE EXAMPLE SYSTEM "example.dtd" [
28103 &lt;!ENTITY xml "Extensible Markup Language"&gt;
28114 ]&gt;
28125 &lt;EXAMPLE&gt;
28136    &amp;xml;
28147 &lt;/EXAMPLE&gt;</pre>
2815
2816<p>Line 3 declares the xml entity. Line 6 uses the xml entity, by prefixing
2817its name with '&amp;' and following it by ';' without any spaces added. There
2818are 5 predefined entities in libxml allowing you to escape charaters with
2819predefined meaning in some parts of the xml document content:
2820<strong>&amp;lt;</strong> for the character '&lt;', <strong>&amp;gt;</strong>
2821for the character '&gt;',  <strong>&amp;apos;</strong> for the character ''',
2822<strong>&amp;quot;</strong> for the character '"', and
2823<strong>&amp;amp;</strong> for the character '&amp;'.</p>
2824
2825<p>One of the problems related to entities is that you may want the parser to
2826substitute an entity's content so that you can see the replacement text in
2827your application. Or you may prefer to keep entity references as such in the
2828content to be able to save the document back without losing this usually
2829precious information (if the user went through the pain of explicitly
2830defining entities, he may have a a rather negative attitude if you blindly
2831susbtitute them as saving time). The <a
2832href="html/libxml-parser.html#XMLSUBSTITUTEENTITIESDEFAULT">xmlSubstituteEntitiesDefault()</a>
2833function allows you to check and change the behaviour, which is to not
2834substitute entities by default.</p>
2835
2836<p>Here is the DOM tree built by libxml for the previous document in the
2837default case:</p>
2838<pre>/gnome/src/gnome-xml -&gt; /xmllint --debug test/ent1
2839DOCUMENT
2840version=1.0
2841   ELEMENT EXAMPLE
2842     TEXT
2843     content=
2844     ENTITY_REF
2845       INTERNAL_GENERAL_ENTITY xml
2846       content=Extensible Markup Language
2847     TEXT
2848     content=</pre>
2849
2850<p>And here is the result when substituting entities:</p>
2851<pre>/gnome/src/gnome-xml -&gt; /tester --debug --noent test/ent1
2852DOCUMENT
2853version=1.0
2854   ELEMENT EXAMPLE
2855     TEXT
2856     content=     Extensible Markup Language</pre>
2857
2858<p>So, entities or no entities? Basically, it depends on your use case. I
2859suggest that you keep the non-substituting default behaviour and avoid using
2860entities in your XML document or data if you are not willing to handle the
2861entity references elements in the DOM tree.</p>
2862
2863<p>Note that at save time libxml enforces the conversion of the predefined
2864entities where necessary to prevent well-formedness problems, and will also
2865transparently replace those with chars (i.e. it will not generate entity
2866reference elements in the DOM tree or call the reference() SAX callback when
2867finding them in the input).</p>
2868
2869<p><span style="background-color: #FF0000">WARNING</span>: handling entities
2870on top of the libxml SAX interface is difficult!!! If you plan to use
2871non-predefined entities in your documents, then the learning cuvre to handle
2872then using the SAX API may be long. If you plan to use complex documents, I
2873strongly suggest you consider using the DOM interface instead and let libxml
2874deal with the complexity rather than trying to do it yourself.</p>
2875
2876<h2><a name="Namespaces">Namespaces</a></h2>
2877
2878<p>The libxml library implements <a
2879href="http://www.w3.org/TR/REC-xml-names/">XML namespaces</a> support by
2880recognizing namespace contructs in the input, and does namespace lookup
2881automatically when building the DOM tree. A namespace declaration is
2882associated with an in-memory structure and all elements or attributes within
2883that namespace point to it. Hence testing the namespace is a simple and fast
2884equality operation at the user level.</p>
2885
2886<p>I suggest that people using libxml use a namespace, and declare it in the
2887root element of their document as the default namespace. Then they don't need
2888to use the prefix in the content but we will have a basis for future semantic
2889refinement and  merging of data from different sources. This doesn't increase
2890the size of the XML output significantly, but significantly increases its
2891value in the long-term. Example:</p>
2892<pre>&lt;mydoc xmlns="http://mydoc.example.org/schemas/"&gt;
2893   &lt;elem1&gt;...&lt;/elem1&gt;
2894   &lt;elem2&gt;...&lt;/elem2&gt;
2895&lt;/mydoc&gt;</pre>
2896
2897<p>The namespace value has to be an absolute URL, but the URL doesn't have to
2898point to any existing resource on the Web. It will bind all the element and
2899atributes with that URL. I suggest to use an URL within a domain you control,
2900and that the URL should contain some kind of version information if possible.
2901For example, <code>"http://www.gnome.org/gnumeric/1.0/"</code> is a good
2902namespace scheme.</p>
2903
2904<p>Then when you load a file, make sure that a namespace carrying the
2905version-independent prefix is installed on the root element of your document,
2906and if the version information don't match something you know, warn the user
2907and be liberal in what you accept as the input. Also do *not* try to base
2908namespace checking on the prefix value. &lt;foo:text&gt; may be exactly the
2909same as &lt;bar:text&gt; in another document. What really matters is the URI
2910associated with the element or the attribute, not the prefix string (which is
2911just a shortcut for the full URI). In libxml, element and attributes have an
2912<code>ns</code> field pointing to an xmlNs structure detailing the namespace
2913prefix and its URI.</p>
2914
2915<p>@@Interfaces@@</p>
2916
2917<p>@@Examples@@</p>
2918
2919<p>Usually people object to using namespaces together with validity checking.
2920I will try to make sure that using namespaces won't break validity checking,
2921so even if you plan to use or currently are using validation I strongly
2922suggest adding namespaces to your document. A default namespace scheme
2923<code>xmlns="http://...."</code> should not break validity even on less
2924flexible parsers. Using namespaces to mix and differentiate content coming
2925from multiple DTDs will certainly break current validation schemes. I will
2926try to provide ways to do this, but this may not be portable or
2927standardized.</p>
2928
2929<h2><a name="Upgrading">Upgrading 1.x code</a></h2>
2930
2931<p>Incompatible changes:</p>
2932
2933<p>Version 2 of libxml is the first version introducing serious backward
2934incompatible changes. The main goals were:</p>
2935<ul>
2936  <li>a general cleanup. A number of mistakes inherited from the very early
2937    versions couldn't be changed due to compatibility constraints. Example
2938    the "childs" element in the nodes.</li>
2939  <li>Uniformization of the various nodes, at least for their header and link
2940    parts (doc, parent, children, prev, next), the goal is a simpler
2941    programming model and simplifying the task of the DOM implementors.</li>
2942  <li>better conformances to the XML specification, for example version 1.x
2943    had an heuristic to try to detect ignorable white spaces. As a result the
2944    SAX event generated were ignorableWhitespace() while the spec requires
2945    character() in that case. This also mean that a number of DOM node
2946    containing blank text may populate the DOM tree which were not present
2947    before.</li>
2948</ul>
2949
2950<h3>How to fix libxml-1.x code:</h3>
2951
2952<p>So client code of libxml designed to run with version 1.x may have to be
2953changed to compile against version 2.x of libxml. Here is a list of changes
2954that I have collected, they may not be sufficient, so in case you find other
2955change which are required, <a href="mailto:Daniel.�eillardw3.org">drop me a
2956mail</a>:</p>
2957<ol>
2958  <li>The package name have changed from libxml to libxml2, the library name
2959    is now -lxml2 . There is a new xml2-config script which should be used to
2960    select the right parameters libxml2</li>
2961  <li>Node <strong>childs</strong> field has been renamed
2962    <strong>children</strong> so s/childs/children/g should be  applied
2963    (probablility of having "childs" anywere else is close to 0+</li>
2964  <li>The document don't have anymore a <strong>root</strong> element it has
2965    been replaced by <strong>children</strong> and usually you will get a
2966    list of element here. For example a Dtd element for the internal subset
2967    and it's declaration may be found in that list, as well as processing
2968    instructions or comments found before or after the document root element.
2969    Use <strong>xmlDocGetRootElement(doc)</strong> to get the root element of
2970    a document. Alternatively if you are sure to not reference Dtds nor have
2971    PIs or comments before or after the root element
2972    s/-&gt;root/-&gt;children/g will probably do it.</li>
2973  <li>The white space issue, this one is more complex, unless special case of
2974    validating parsing, the line breaks and spaces usually used for indenting
2975    and formatting the document content becomes significant. So they are
2976    reported by SAX and if your using the DOM tree, corresponding nodes are
2977    generated. Too approach can be taken:
2978    <ol>
2979      <li>lazy one, use the compatibility call
2980        <strong>xmlKeepBlanksDefault(0)</strong> but be aware that you are
2981        relying on a special (and possibly broken) set of heuristics of
2982        libxml to detect ignorable blanks. Don't complain if it breaks or
2983        make your application not 100% clean w.r.t. to it's input.</li>
2984      <li>the Right Way: change you code to accept possibly unsignificant
2985        blanks characters, or have your tree populated with weird blank text
2986        nodes. You can spot them using the comodity function
2987        <strong>xmlIsBlankNode(node)</strong> returning 1 for such blank
2988        nodes.</li>
2989    </ol>
2990    <p>Note also that with the new default the output functions don't add any
2991    extra indentation when saving a tree in order to be able to round trip
2992    (read and save) without inflating the document with extra formatting
2993    chars.</p>
2994  </li>
2995  <li>The include path has changed to $prefix/libxml/ and the includes
2996    themselves uses this new prefix in includes instructions... If you are
2997    using (as expected) the
2998    <pre>xml2-config --cflags</pre>
2999    <p>output to generate you compile commands this will probably work out of
3000    the box</p>
3001  </li>
3002  <li>xmlDetectCharEncoding takes an extra argument indicating the lenght in
3003    byte of the head of the document available for character detection.</li>
3004</ol>
3005
3006<h3>Ensuring both libxml-1.x and libxml-2.x compatibility</h3>
3007
3008<p>Two new version of libxml (1.8.11) and libxml2 (2.3.4) have been released
3009to allow smoth upgrade of existing libxml v1code while retaining
3010compatibility. They offers the following:</p>
3011<ol>
3012  <li>similar include naming, one should use
3013    <strong>#include&lt;libxml/...&gt;</strong> in both cases.</li>
3014  <li>similar identifiers defined via macros for the child and root fields:
3015    respectively <strong>xmlChildrenNode</strong> and
3016    <strong>xmlRootNode</strong></li>
3017  <li>a new macro <strong>LIBXML_TEST_VERSION</strong> which should be
3018    inserted once in the client code</li>
3019</ol>
3020
3021<p>So the roadmap to upgrade your existing libxml applications is the
3022following:</p>
3023<ol>
3024  <li>install the  libxml-1.8.8 (and libxml-devel-1.8.8) packages</li>
3025  <li>find all occurences where the xmlDoc <strong>root</strong> field is
3026    used and change it to <strong>xmlRootNode</strong></li>
3027  <li>similary find all occurences where the xmlNode <strong>childs</strong>
3028    field is used and change it to <strong>xmlChildrenNode</strong></li>
3029  <li>add a <strong>LIBXML_TEST_VERSION</strong> macro somewhere in your
3030    <strong>main()</strong> or in the library init entry point</li>
3031  <li>Recompile, check compatibility, it should still work</li>
3032  <li>Change your configure script to look first for xml2-config and fallback
3033    using xml-config . Use the --cflags and --libs ouptut of the command as
3034    the Include and Linking parameters needed to use libxml.</li>
3035  <li>install libxml2-2.3.x and  libxml2-devel-2.3.x (libxml-1.8.y and
3036    libxml-devel-1.8.y can be kept simultaneously)</li>
3037  <li>remove your config.cache, relaunch your configuration mechanism, and
3038    recompile, if steps 2 and 3 were done right it should compile as-is</li>
3039  <li>Test that your application is still running correctly, if not this may
3040    be due to extra empty nodes due to formating spaces being kept in libxml2
3041    contrary to libxml1, in that case insert xmlKeepBlanksDefault(1) in your
3042    code before calling the parser (next to
3043    <strong>LIBXML_TEST_VERSION</strong> is a fine place).</li>
3044</ol>
3045
3046<p>Following those steps should work. It worked for some of my own code.</p>
3047
3048<p>Let me put some emphasis on the fact that there is far more changes from
3049libxml 1.x to 2.x than the ones you may have to patch for. The overall code
3050has been considerably cleaned up and the conformance to the XML specification
3051has been drastically improved too. Don't take those changes as an excuse to
3052not upgrade, it may cost a lot on the long term ...</p>
3053
3054<h2><a name="Thread">Thread safety</a></h2>
3055
3056<p>Starting with 2.4.7, libxml makes provisions to ensure that concurent
3057threads can safely work in parallel parsing different documents. There is
3058however a couple of things to do to ensure it:</p>
3059<ul>
3060  <li>configure the library accordingly using the --with-threads options</li>
3061  <li>call xmlInitParser() in the "main" thread before using any of the
3062    libxml API (except possibly selecting a different memory allocator)</li>
3063</ul>
3064
3065<p>Note that the thread safety cannot be ensured for multiple threads sharing
3066the same document, the locking must be done at the application level, libxml
3067exports a basic mutex and reentrant mutexes API in &lt;libxml/threads.h&gt;.
3068The parts of the library checked for thread safety are:</p>
3069<ul>
3070  <li>concurrent loading</li>
3071  <li>file access resolution</li>
3072  <li>catalog access</li>
3073  <li>catalog building</li>
3074  <li>entities lookup/accesses</li>
3075  <li>validation</li>
3076  <li>global variables per-thread override</li>
3077  <li>memory handling</li>
3078</ul>
3079
3080<p>XPath is supposed to be thread safe now, but this wasn't tested
3081seriously.</p>
3082
3083<h2><a name="DOM"></a><a name="Principles">DOM Principles</a></h2>
3084
3085<p><a href="http://www.w3.org/DOM/">DOM</a> stands for the <em>Document
3086Object Model</em>; this is an API for accessing XML or HTML structured
3087documents. Native support for DOM in Gnome is on the way (module gnome-dom),
3088and will be based on gnome-xml. This will be a far cleaner interface to
3089manipulate XML files within Gnome since it won't expose the internal
3090structure.</p>
3091
3092<p>The current DOM implementation on top of libxml is the <a
3093href="http://cvs.gnome.org/lxr/source/gdome2/">gdome2 Gnome module</a>, this
3094is a full DOM interface, thanks to Paolo Casarini, check the <a
3095href="http://www.cs.unibo.it/~casarini/gdome2/">Gdome2 homepage</a> for more
3096informations.</p>
3097
3098<h2><a name="Example"></a><a name="real">A real example</a></h2>
3099
3100<p>Here is a real size example, where the actual content of the application
3101data is not kept in the DOM tree but uses internal structures. It is based on
3102a proposal to keep a database of jobs related to Gnome, with an XML based
3103storage structure. Here is an <a href="gjobs.xml">XML encoded jobs
3104base</a>:</p>
3105<pre>&lt;?xml version="1.0"?&gt;
3106&lt;gjob:Helping xmlns:gjob="http://www.gnome.org/some-location"&gt;
3107  &lt;gjob:Jobs&gt;
3108
3109    &lt;gjob:Job&gt;
3110      &lt;gjob:Project ID="3"/&gt;
3111      &lt;gjob:Application&gt;GBackup&lt;/gjob:Application&gt;
3112      &lt;gjob:Category&gt;Development&lt;/gjob:Category&gt;
3113
3114      &lt;gjob:Update&gt;
3115        &lt;gjob:Status&gt;Open&lt;/gjob:Status&gt;
3116        &lt;gjob:Modified&gt;Mon, 07 Jun 1999 20:27:45 -0400 MET DST&lt;/gjob:Modified&gt;
3117        &lt;gjob:Salary&gt;USD 0.00&lt;/gjob:Salary&gt;
3118      &lt;/gjob:Update&gt;
3119
3120      &lt;gjob:Developers&gt;
3121        &lt;gjob:Developer&gt;
3122        &lt;/gjob:Developer&gt;
3123      &lt;/gjob:Developers&gt;
3124
3125      &lt;gjob:Contact&gt;
3126        &lt;gjob:Person&gt;Nathan Clemons&lt;/gjob:Person&gt;
3127        &lt;gjob:Email&gt;nathan@windsofstorm.net&lt;/gjob:Email&gt;
3128        &lt;gjob:Company&gt;
3129        &lt;/gjob:Company&gt;
3130        &lt;gjob:Organisation&gt;
3131        &lt;/gjob:Organisation&gt;
3132        &lt;gjob:Webpage&gt;
3133        &lt;/gjob:Webpage&gt;
3134        &lt;gjob:Snailmail&gt;
3135        &lt;/gjob:Snailmail&gt;
3136        &lt;gjob:Phone&gt;
3137        &lt;/gjob:Phone&gt;
3138      &lt;/gjob:Contact&gt;
3139
3140      &lt;gjob:Requirements&gt;
3141      The program should be released as free software, under the GPL.
3142      &lt;/gjob:Requirements&gt;
3143
3144      &lt;gjob:Skills&gt;
3145      &lt;/gjob:Skills&gt;
3146
3147      &lt;gjob:Details&gt;
3148      A GNOME based system that will allow a superuser to configure 
3149      compressed and uncompressed files and/or file systems to be backed 
3150      up with a supported media in the system.  This should be able to 
3151      perform via find commands generating a list of files that are passed 
3152      to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine 
3153      or via operations performed on the filesystem itself. Email 
3154      notification and GUI status display very important.
3155      &lt;/gjob:Details&gt;
3156
3157    &lt;/gjob:Job&gt;
3158
3159  &lt;/gjob:Jobs&gt;
3160&lt;/gjob:Helping&gt;</pre>
3161
3162<p>While loading the XML file into an internal DOM tree is a matter of
3163calling only a couple of functions, browsing the tree to gather the ata and
3164generate the internal structures is harder, and more error prone.</p>
3165
3166<p>The suggested principle is to be tolerant with respect to the input
3167structure. For example, the ordering of the attributes is not significant,
3168the XML specification is clear about it. It's also usually a good idea not to
3169depend on the order of the children of a given node, unless it really makes
3170things harder. Here is some code to parse the information for a person:</p>
3171<pre>/*
3172 * A person record
3173 */
3174typedef struct person {
3175    char *name;
3176    char *email;
3177    char *company;
3178    char *organisation;
3179    char *smail;
3180    char *webPage;
3181    char *phone;
3182} person, *personPtr;
3183
3184/*
3185 * And the code needed to parse it
3186 */
3187personPtr parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
3188    personPtr ret = NULL;
3189
3190DEBUG("parsePerson\n");
3191    /*
3192     * allocate the struct
3193     */
3194    ret = (personPtr) malloc(sizeof(person));
3195    if (ret == NULL) {
3196        fprintf(stderr,"out of memory\n");
3197        return(NULL);
3198    }
3199    memset(ret, 0, sizeof(person));
3200
3201    /* We don't care what the top level element name is */
3202    cur = cur-&gt;xmlChildrenNode;
3203    while (cur != NULL) {
3204        if ((!strcmp(cur-&gt;name, "Person")) &amp;&amp; (cur-&gt;ns == ns))
3205            ret-&gt;name = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
3206        if ((!strcmp(cur-&gt;name, "Email")) &amp;&amp; (cur-&gt;ns == ns))
3207            ret-&gt;email = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
3208        cur = cur-&gt;next;
3209    }
3210
3211    return(ret);
3212}</pre>
3213
3214<p>Here are a couple of things to notice:</p>
3215<ul>
3216  <li>Usually a recursive parsing style is the more convenient one: XML data
3217    is by nature subject to repetitive constructs and usually exibits highly
3218    stuctured patterns.</li>
3219  <li>The two arguments of type <em>xmlDocPtr</em> and <em>xmlNsPtr</em>,
3220    i.e. the pointer to the global XML document and the namespace reserved to
3221    the application. Document wide information are needed for example to
3222    decode entities and it's a good coding practice to define a namespace for
3223    your application set of data and test that the element and attributes
3224    you're analyzing actually pertains to your application space. This is
3225    done by a simple equality test (cur-&gt;ns == ns).</li>
3226  <li>To retrieve text and attributes value, you can use the function
3227    <em>xmlNodeListGetString</em> to gather all the text and entity reference
3228    nodes generated by the DOM output and produce an single text string.</li>
3229</ul>
3230
3231<p>Here is another piece of code used to parse another level of the
3232structure:</p>
3233<pre>#include &lt;libxml/tree.h&gt;
3234/*
3235 * a Description for a Job
3236 */
3237typedef struct job {
3238    char *projectID;
3239    char *application;
3240    char *category;
3241    personPtr contact;
3242    int nbDevelopers;
3243    personPtr developers[100]; /* using dynamic alloc is left as an exercise */
3244} job, *jobPtr;
3245
3246/*
3247 * And the code needed to parse it
3248 */
3249jobPtr parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
3250    jobPtr ret = NULL;
3251
3252DEBUG("parseJob\n");
3253    /*
3254     * allocate the struct
3255     */
3256    ret = (jobPtr) malloc(sizeof(job));
3257    if (ret == NULL) {
3258        fprintf(stderr,"out of memory\n");
3259        return(NULL);
3260    }
3261    memset(ret, 0, sizeof(job));
3262
3263    /* We don't care what the top level element name is */
3264    cur = cur-&gt;xmlChildrenNode;
3265    while (cur != NULL) {
3266        
3267        if ((!strcmp(cur-&gt;name, "Project")) &amp;&amp; (cur-&gt;ns == ns)) {
3268            ret-&gt;projectID = xmlGetProp(cur, "ID");
3269            if (ret-&gt;projectID == NULL) {
3270                fprintf(stderr, "Project has no ID\n");
3271            }
3272        }
3273        if ((!strcmp(cur-&gt;name, "Application")) &amp;&amp; (cur-&gt;ns == ns))
3274            ret-&gt;application = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
3275        if ((!strcmp(cur-&gt;name, "Category")) &amp;&amp; (cur-&gt;ns == ns))
3276            ret-&gt;category = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
3277        if ((!strcmp(cur-&gt;name, "Contact")) &amp;&amp; (cur-&gt;ns == ns))
3278            ret-&gt;contact = parsePerson(doc, ns, cur);
3279        cur = cur-&gt;next;
3280    }
3281
3282    return(ret);
3283}</pre>
3284
3285<p>Once you are used to it, writing this kind of code is quite simple, but
3286boring. Ultimately, it could be possble to write stubbers taking either C
3287data structure definitions, a set of XML examples or an XML DTD and produce
3288the code needed to import and export the content between C data and XML
3289storage. This is left as an exercise to the reader :-)</p>
3290
3291<p>Feel free to use <a href="example/gjobread.c">the code for the full C
3292parsing example</a> as a template, it is also available with Makefile in the
3293Gnome CVS base under gnome-xml/example</p>
3294
3295<h2><a name="Contributi">Contributions</a></h2>
3296<ul>
3297  <li>Bjorn Reese, William Brack and Thomas Broyer have provided a number of
3298    patches, Gary Pennington worked on the validation API, threading support
3299    and Solaris port.</li>
3300  <li>John Fleck helps maintaining the documentation and man pages.</li>
3301  <li><a href="mailto:ari@lusis.org">Ari Johnson</a>
3302     provides a  C++ wrapper for libxml:<br>
3303    Website: <a
3304    href="http://lusis.org/~ari/xml++/">http://lusis.org/~ari/xml++/</a><br>
3305    Download: <a
3306    href="http://lusis.org/~ari/xml++/libxml++.tar.gz">http://lusis.org/~ari/xml++/libxml++.tar.gz</a></li>
3307  <li><a href="mailto:izlatkovic@daenet.de">Igor  Zlatkovic</a>
3308     is now the maintainer of the Windows port, <a
3309    href="http://www.fh-frankfurt.de/~igor/projects/libxml/index.html">he
3310    provides binaries</a></li>
3311  <li><a href="mailto:Gary.Pennington@sun.com">Gary Pennington</a>
3312     provides <a href="http://garypennington.net/libxml2/">Solaris
3313    binaries</a></li>
3314  <li><a
3315    href="http://mail.gnome.org/archives/xml/2001-March/msg00014.html">Matt
3316    Sergeant</a>
3317     developped <a href="http://axkit.org/download/">XML::LibXSLT</a>, a perl
3318    wrapper for libxml2/libxslt as part of the <a
3319    href="http://axkit.com/">AxKit XML application server</a></li>
3320  <li><a href="mailto:fnatter@gmx.net">Felix Natter</a>
3321     and <a href="mailto:geertk@ai.rug.nl">Geert Kloosterman</a> provide <a
3322    href="libxml-doc.el">an emacs module</a> to lookup libxml(2) functions
3323    documentation</li>
3324  <li><a href="mailto:sherwin@nlm.nih.gov">Ziying Sherwin</a>
3325     provided <a href="http://xmlsoft.org/messages/0488.html">man
3326  pages</a></li>
3327  <li>there is a module for <a
3328    href="http://acs-misc.sourceforge.net/nsxml.html">libxml/libxslt support
3329    in OpenNSD/AOLServer</a></li>
3330  <li><a href="mailto:dkuhlman@cutter.rexx.com">Dave Kuhlman</a>
3331     provides libxml/libxslt <a href="http://www.rexx.com/~dkuhlman">wrappers
3332    for Python</a></li>
3333  <li>Petr Kozelka provides <a
3334    href="http://sourceforge.net/projects/libxml2-pas">Pascal units to glue
3335    libxml2</a> with Kylix and Delphi and other Pascal compilers</li>
3336</ul>
3337
3338<p></p>
3339</body>
3340</html>
3341