blob: 35a714f6daa0dbf94ba0576d5b24e039ca3f4c3b [file] [log] [blame]
Daniel Veillard43d3f612001-11-10 11:57:23 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
Daniel Veillardbe40c8b2000-07-14 12:10:59 +00002<html>
3<head>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +00004<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
Daniel Veillardc332dab2002-03-29 14:08:27 +00005<link rel="SHORTCUT ICON" href="/favicon.ico">
Daniel Veillardb8cfbd12001-10-25 10:53:28 +00006<style type="text/css"><!--
Daniel Veillard373a4752002-02-21 14:46:29 +00007TD {font-family: Verdana,Arial,Helvetica}
8BODY {font-family: Verdana,Arial,Helvetica; margin-top: 2em; margin-left: 0em; margin-right: 0em}
9H1 {font-family: Verdana,Arial,Helvetica}
10H2 {font-family: Verdana,Arial,Helvetica}
11H3 {font-family: Verdana,Arial,Helvetica}
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000012A:link, A:visited, A:active { text-decoration: underline }
13--></style>
14<title>Encodings support</title>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +000015</head>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000016<body bgcolor="#8b7765" text="#000000" link="#000000" vlink="#000000">
17<table border="0" width="100%" cellpadding="5" cellspacing="0" align="center"><tr>
18<td width="180">
19<a href="http://www.gnome.org/"><img src="smallfootonly.gif" alt="Gnome Logo"></a><a href="http://www.w3.org/Status"><img src="w3c.png" alt="W3C Logo"></a><a href="http://www.redhat.com/"><img src="redhat.gif" alt="Red Hat Logo"></a>
20</td>
21<td><table border="0" width="90%" cellpadding="2" cellspacing="0" align="center" bgcolor="#000000"><tr><td><table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="#fffacd"><tr><td align="center">
22<h1>The XML C library for Gnome</h1>
23<h2>Encodings support</h2>
24</td></tr></table></td></tr></table></td>
25</tr></table>
26<table border="0" cellpadding="4" cellspacing="0" width="100%" align="center"><tr><td bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr>
27<td valign="top" width="200" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td>
28<table width="100%" border="0" cellspacing="1" cellpadding="3">
29<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Main Menu</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000030<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000031<li><a href="index.html">Home</a></li>
32<li><a href="intro.html">Introduction</a></li>
33<li><a href="FAQ.html">FAQ</a></li>
34<li><a href="docs.html">Documentation</a></li>
35<li><a href="bugs.html">Reporting bugs and getting help</a></li>
36<li><a href="help.html">How to help</a></li>
37<li><a href="downloads.html">Downloads</a></li>
38<li><a href="news.html">News</a></li>
Daniel Veillard7b602b42002-01-08 13:26:00 +000039<li><a href="XMLinfo.html">XML</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000040<li><a href="XSLT.html">XSLT</a></li>
Daniel Veillard6dbcaf82002-02-20 14:37:47 +000041<li><a href="python.html">Python and bindings</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000042<li><a href="architecture.html">libxml architecture</a></li>
43<li><a href="tree.html">The tree output</a></li>
44<li><a href="interface.html">The SAX interface</a></li>
45<li><a href="xmldtd.html">Validation &amp; DTDs</a></li>
46<li><a href="xmlmem.html">Memory Management</a></li>
47<li><a href="encoding.html">Encodings support</a></li>
48<li><a href="xmlio.html">I/O Interfaces</a></li>
49<li><a href="catalog.html">Catalog support</a></li>
50<li><a href="library.html">The parser interfaces</a></li>
51<li><a href="entities.html">Entities or no entities</a></li>
52<li><a href="namespaces.html">Namespaces</a></li>
53<li><a href="upgrade.html">Upgrading 1.x code</a></li>
Daniel Veillard52dcab32001-10-30 12:51:17 +000054<li><a href="threads.html">Thread safety</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000055<li><a href="DOM.html">DOM Principles</a></li>
56<li><a href="example.html">A real example</a></li>
57<li><a href="contribs.html">Contributions</a></li>
58<li>
59<a href="xml.html">flat page</a>, <a href="site.xsl">stylesheet</a>
60</li>
61</ul></td></tr>
62</table>
63<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillard3bf65be2002-01-23 12:36:34 +000064<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>API Indexes</b></center></td></tr>
65<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardf8592562002-01-23 17:58:17 +000066<li><a href="APIchunk0.html">Alphabetic</a></li>
Daniel Veillard3bf65be2002-01-23 12:36:34 +000067<li><a href="APIconstructors.html">Constructors</a></li>
68<li><a href="APIfunctions.html">Functions/Types</a></li>
69<li><a href="APIfiles.html">Modules</a></li>
70<li><a href="APIsymbols.html">Symbols</a></li>
71</ul></td></tr>
72</table>
73<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000074<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000075<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000076<li><a href="http://mail.gnome.org/archives/xml/">Mail archive</a></li>
77<li><a href="http://xmlsoft.org/XSLT/">XSLT libxslt</a></li>
Daniel Veillard4a859202002-01-08 11:49:22 +000078<li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
Daniel Veillard2d347fa2002-03-17 10:34:11 +000079<li><a href="http://www.aleksey.com/xmlsec/">XML-DSig xmlsec</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000080<li><a href="ftp://xmlsoft.org/">FTP</a></li>
81<li><a href="http://www.fh-frankfurt.de/~igor/projects/libxml/">Windows binaries</a></li>
Daniel Veillarddb9dfd92001-11-26 17:25:02 +000082<li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
Daniel Veillarde6d8e202002-05-02 06:11:10 +000083<li><a href="http://sourceforge.net/projects/libxml2-pas/">Pascal bindings</a></li>
Daniel Veillard2d347fa2002-03-17 10:34:11 +000084<li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxml&amp;product=libxml2">Bug Tracker</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000085</ul></td></tr>
86</table>
87</td></tr></table></td>
88<td valign="top" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%"><tr><td><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td><table border="0" cellpadding="3" cellspacing="1" width="100%"><tr><td bgcolor="#fffacd">
Daniel Veillardbe40c8b2000-07-14 12:10:59 +000089<p>Table of Content:</p>
90<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000091<li><a href="encoding.html#What">What does internationalization support
92 mean ?</a></li>
93<li><a href="encoding.html#internal">The internal encoding, how and
94 why</a></li>
95<li><a href="encoding.html#implemente">How is it implemented ?</a></li>
96<li><a href="encoding.html#Default">Default supported encodings</a></li>
97<li><a href="encoding.html#extend">How to extend the existing
98 support</a></li>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +000099</ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000100<h3><a name="What">What does internationalization support mean ?</a></h3>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000101<p>XML was designed from the start to allow the support of any character set
102by using Unicode. Any conformant XML parser has to support the UTF-8 and
103UTF-16 default encodings which can both express the full unicode ranges. UTF8
Daniel Veillard63d83142002-05-20 06:51:05 +0000104is a variable length encoding whose greatest points are to reuse the same
105encoding for ASCII and to save space for Western encodings, but it is a bit
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000106more complex to handle in practice. UTF-16 use 2 bytes per characters (and
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000107sometimes combines two pairs), it makes implementation easier, but looks a
108bit overkill for Western languages encoding. Moreover the XML specification
109allows document to be encoded in other encodings at the condition that they
Daniel Veillard63d83142002-05-20 06:51:05 +0000110are clearly labeled as such. For example the following is a wellformed XML
Daniel Veillard0d6b1702000-08-22 23:52:16 +0000111document encoded in ISO-8859 1 and using accentuated letter that we French
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000112likes for both markup and content:</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000113<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000114&lt;très&gt;là&lt;/très&gt;</pre>
Daniel Veillard63d83142002-05-20 06:51:05 +0000115<p>Having internationalization support in libxml means the following:</p>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000116<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000117<li>the document is properly parsed</li>
118<li>informations about it's encoding are saved</li>
119<li>it can be modified</li>
120<li>it can be saved in its original encoding</li>
121<li>it can also be saved in another encoding supported by libxml (for
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000122 example straight UTF8 or even an ASCII form)</li>
123</ul>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000124<p>Another very important point is that the whole libxml API, with the
125exception of a few routines to read with a specific encoding or save to a
126specific encoding, is completely agnostic about the original encoding of the
127document.</p>
Daniel Veillard63d83142002-05-20 06:51:05 +0000128<p>It should be noted too that the HTML parser embedded in libxml now obey
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000129the same rules too, the following document will be (as of 2.2.2) handled in
130an internationalized fashion by libxml too:</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000131<pre>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;
132 &quot;http://www.w3.org/TR/REC-html40/loose.dtd&quot;&gt;
133&lt;html lang=&quot;fr&quot;&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000134&lt;head&gt;
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000135 &lt;META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=ISO-8859-1&quot;&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000136&lt;/head&gt;
137&lt;body&gt;
138&lt;p&gt;W3C crée des standards pour le Web.&lt;/body&gt;
139&lt;/html&gt;</pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000140<h3><a name="internal">The internal encoding, how and why</a></h3>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000141<p>One of the core decision was to force all documents to be converted to a
142default internal encoding, and that encoding to be UTF-8, here are the
143rationale for those choices:</p>
144<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000145<li>keeping the native encoding in the internal form would force the libxml
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000146 users (or the code associated) to be fully aware of the encoding of the
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000147 original document, for examples when adding a text node to a document,
148 the content would have to be provided in the document encoding, i.e. the
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000149 client code would have to check it before hand, make sure it's conformant
150 to the encoding, etc ... Very hard in practice, though in some specific
151 cases this may make sense.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000152<li>the second decision was which encoding. From the XML spec only UTF8 and
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000153 UTF16 really makes sense as being the two only encodings for which there
Daniel Veillard63d83142002-05-20 06:51:05 +0000154 is mandatory support. UCS-4 (32 bits fixed size encoding) could be
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000155 considered an intelligent choice too since it's a direct Unicode mapping
156 support. I selected UTF-8 on the basis of efficiency and compatibility
157 with surrounding software:
158 <ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000159<li>UTF-8 while a bit more complex to convert from/to (i.e. slightly
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000160 more costly to import and export CPU wise) is also far more compact
161 than UTF-16 (and UCS-4) for a majority of the documents I see it used
162 for right now (RPM RDF catalogs, advogato data, various configuration
163 file formats, etc.) and the key point for today's computer
164 architecture is efficient uses of caches. If one nearly double the
165 memory requirement to store the same amount of data, this will trash
166 caches (main memory/external caches/internal caches) and my take is
167 that this harms the system far more than the CPU requirements needed
168 for the conversion to UTF-8</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000169<li>Most of libxml version 1 users were using it with straight ASCII
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000170 most of the time, doing the conversion with an internal encoding
171 requiring all their code to be rewritten was a serious show-stopper
172 for using UTF-16 or UCS-4.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000173<li>UTF-8 is being used as the de-facto internal encoding standard for
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000174 related code like the <a href="http://www.pango.org/">pango</a>
175 upcoming Gnome text widget, and a lot of Unix code (yep another place
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000176 where Unix programmer base takes a different approach from Microsoft
177 - they are using UTF-16)</li>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000178</ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000179</li>
180</ul>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000181<p>What does this mean in practice for the libxml user:</p>
182<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000183<li>xmlChar, the libxml data type is a byte, those bytes must be assembled
184 as UTF-8 valid strings. The proper way to terminate an xmlChar * string
185 is simply to append 0 byte, as usual.</li>
186<li>One just need to make sure that when using chars outside the ASCII set,
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000187 the values has been properly converted to UTF-8</li>
188</ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000189<h3><a name="implemente">How is it implemented ?</a></h3>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000190<p>Let's describe how all this works within libxml, basically the I18N
191(internationalization) support get triggered only during I/O operation, i.e.
192when reading a document or saving one. Let's look first at the reading
193sequence:</p>
194<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000195<li>when a document is processed, we usually don't know the encoding, a
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000196 simple heuristic allows to detect UTF-18 and UCS-4 from whose where the
197 ASCII range (0-0x7F) maps with ASCII</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000198<li>the xml declaration if available is parsed, including the encoding
199 declaration. At that point, if the autodetected encoding is different
200 from the one declared a call to xmlSwitchEncoding() is issued.</li>
201<li>If there is no encoding declaration, then the input has to be in either
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000202 UTF-8 or UTF-16, if it is not then at some point when processing the
203 input, the converter/checker of UTF-8 form will raise an encoding error.
204 You may end-up with a garbled document, or no document at all ! Example:
205 <pre>~/XML -&gt; ./xmllint err.xml
206err.xml:1: error: Input is not proper UTF-8, indicate encoding !
207&lt;très&gt;là&lt;/très&gt;
208 ^
209err.xml:1: error: Bytes: 0xE8 0x73 0x3E 0x6C
210&lt;très&gt;là&lt;/très&gt;
211 ^</pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000212</li>
Daniel Veillard63d83142002-05-20 06:51:05 +0000213<li>xmlSwitchEncoding() does an encoding name lookup, canonicalize it, and
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000214 then search the default registered encoding converters for that encoding.
215 If it's not within the default set and iconv() support has been compiled
216 it, it will ask iconv for such an encoder. If this fails then the parser
217 will report an error and stops processing:
218 <pre>~/XML -&gt; ./xmllint err2.xml
219err2.xml:1: error: Unsupported encoding UnsupportedEnc
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000220&lt;?xml version=&quot;1.0&quot; encoding=&quot;UnsupportedEnc&quot;?&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000221 ^</pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000222</li>
Daniel Veillard63d83142002-05-20 06:51:05 +0000223<li>From that point the encoder processes progressingly the input (it is
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000224 plugged as a front-end to the I/O module) for that entity. It captures
225 and convert on-the-fly the document to be parsed to UTF-8. The parser
226 itself just does UTF-8 checking of this input and process it
227 transparently. The only difference is that the encoding information has
228 been added to the parsing context (more precisely to the input
229 corresponding to this entity).</li>
230<li>The result (when using DOM) is an internal form completely in UTF-8
231 with just an encoding information on the document node.</li>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000232</ol>
Daniel Veillard63d83142002-05-20 06:51:05 +0000233<p>Ok then what happens when saving the document (assuming you
234collected/built an xmlDoc DOM like structure) ? It depends on the function
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000235called, xmlSaveFile() will just try to save in the original encoding, while
236xmlSaveFileTo() and xmlSaveFileEnc() can optionally save to a given
237encoding:</p>
238<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000239<li>if no encoding is given, libxml will look for an encoding value
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000240 associated to the document and if it exists will try to save to that
241 encoding,
242 <p>otherwise everything is written in the internal form, i.e. UTF-8</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000243</li>
244<li>so if an encoding was specified, either at the API level or on the
Daniel Veillard63d83142002-05-20 06:51:05 +0000245 document, libxml will again canonicalize the encoding name, lookup for a
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000246 converter in the registered set or through iconv. If not found the
247 function will return an error code</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000248<li>the converter is placed before the I/O buffer layer, as another kind of
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000249 buffer, then libxml will simply push the UTF-8 serialization to through
250 that buffer, which will then progressively be converted and pushed onto
251 the I/O layer.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000252<li>It is possible that the converter code fails on some input, for example
Daniel Veillard63d83142002-05-20 06:51:05 +0000253 trying to push an UTF-8 encoded Chinese character through the UTF-8 to
Daniel Veillard0d6b1702000-08-22 23:52:16 +0000254 ISO-8859-1 converter won't work. Since the encoders are progressive they
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000255 will just report the error and the number of bytes converted, at that
256 point libxml will decode the offending character, remove it from the
257 buffer and replace it with the associated charRef encoding &amp;#123; and
Daniel Veillard63d83142002-05-20 06:51:05 +0000258 resume the conversion. This guarantees that any document will be saved
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000259 without losses (except for markup names where this is not legal, this is
Daniel Veillard63d83142002-05-20 06:51:05 +0000260 a problem in the current version, in practice avoid using non-ascii
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000261 characters for tags or attributes names @@). A special &quot;ascii&quot; encoding
Daniel Veillard0d6b1702000-08-22 23:52:16 +0000262 name is used to save documents to a pure ascii form can be used when
263 portability is really crucial</li>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000264</ol>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000265<p>Here is a few examples based on the same test document:</p>
266<pre>~/XML -&gt; ./xmllint isolat1
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000267&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000268&lt;très&gt;là&lt;/très&gt;
269~/XML -&gt; ./xmllint --encode UTF-8 isolat1
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000270&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000271&lt;très&gt;là  &lt;/très&gt;
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000272~/XML -&gt; </pre>
Daniel Veillard0d6b1702000-08-22 23:52:16 +0000273<p>The same processing is applied (and reuse most of the code) for HTML I18N
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000274processing. Looking up and modifying the content encoding is a bit more
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000275difficult since it is located in a &lt;meta&gt; tag under the &lt;head&gt;,
276so a couple of functions htmlGetMetaEncoding() and htmlSetMetaEncoding() have
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000277been provided. The parser also attempts to switch encoding on the fly when
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000278detecting such a tag on input. Except for that the processing is the same
279(and again reuses the same code).</p>
280<h3><a name="Default">Default supported encodings</a></h3>
281<p>libxml has a set of default converters for the following encodings
282(located in encoding.c):</p>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000283<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000284<li>UTF-8 is supported by default (null handlers)</li>
285<li>UTF-16, both little and big endian</li>
286<li>ISO-Latin-1 (ISO-8859-1) covering most western languages</li>
287<li>ASCII, useful mostly for saving</li>
288<li>HTML, a specific handler for the conversion of UTF-8 to ASCII with HTML
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000289 predefined entities like &amp;copy; for the Copyright sign.</li>
290</ol>
Daniel Veillard63d83142002-05-20 06:51:05 +0000291<p>More over when compiled on an Unix platform with iconv support the full set
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000292of encodings supported by iconv can be instantly be used by libxml. On a
293linux machine with glibc-2.1 the list of supported encodings and aliases fill
2943 full pages, and include UCS-4, the full set of ISO-Latin encodings, and the
295various Japanese ones.</p>
296<h4>Encoding aliases</h4>
297<p>From 2.2.3, libxml has support to register encoding names aliases. The
298goal is to be able to parse document whose encoding is supported but where
299the name differs (for example from the default set of names accepted by
300iconv). The following functions allow to register and handle new aliases for
301existing encodings. Once registered libxml will automatically lookup the
302aliases when handling a document:</p>
Daniel Veillard088f4282000-08-25 23:46:50 +0000303<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000304<li>int xmlAddEncodingAlias(const char *name, const char *alias);</li>
305<li>int xmlDelEncodingAlias(const char *alias);</li>
306<li>const char * xmlGetEncodingAlias(const char *alias);</li>
307<li>void xmlCleanupEncodingAliases(void);</li>
Daniel Veillard088f4282000-08-25 23:46:50 +0000308</ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000309<h3><a name="extend">How to extend the existing support</a></h3>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000310<p>Well adding support for new encoding, or overriding one of the encoders
311(assuming it is buggy) should not be hard, just write an input and output
312conversion routines to/from UTF-8, and register them using
313xmlNewCharEncodingHandler(name, xxxToUTF8, UTF8Toxxx), and they will be
314called automatically if the parser(s) encounter such an encoding name
315(register it uppercase, this will help). The description of the encoders,
316their arguments and expected return values are described in the encoding.h
317header.</p>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000318<p>A quick note on the topic of subverting the parser to use a different
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000319internal encoding than UTF-8, in some case people will absolutely want to
320keep the internal encoding different, I think it's still possible (but the
321encoding must be compliant with ASCII on the same subrange) though I didn't
322tried it. The key is to override the default conversion routines (by
323registering null encoders/decoders for your charsets), and bypass the UTF-8
324checking of the parser by setting the parser context charset
325(ctxt-&gt;charset) to something different than XML_CHAR_ENCODING_UTF8, but
Daniel Veillard63d83142002-05-20 06:51:05 +0000326there is no guarantee that this will work. You may also have some troubles
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000327saving back.</p>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000328<p>Basically proper I18N support is important, this requires at least
329libxml-2.0.0, but a lot of features and corrections are really available only
330starting 2.2.</p>
Daniel Veillard3f4c40f2002-02-13 09:19:28 +0000331<p><a href="bugs.html">Daniel Veillard</a></p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000332</td></tr></table></td></tr></table></td></tr></table></td>
333</tr></table></td></tr></table>
Daniel Veillardbe40c8b2000-07-14 12:10:59 +0000334</body>
335</html>