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