| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | <title>LuaExpat: XML Expat parsing for the Lua programming language</title> |
| 6 | <link rel="stylesheet" href="../doc.css" type="text/css"/> |
| 7 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 8 | </head> |
| 9 | <body> |
| 10 | |
| 11 | <div id="container"> |
| 12 | |
| 13 | <div id="product"> |
| 14 | <div id="product_logo"><a href="http://www.keplerproject.org"> |
| 15 | <img alt="LuaExpat logo" src="luaexpat.png"/> |
| 16 | </a></div> |
| 17 | <div id="product_name"><big><strong>LuaExpat</strong></big></div> |
| 18 | <div id="product_description">XML Expat parsing for the Lua programming language</div> |
| 19 | </div> <!-- id="product" --> |
| 20 | |
| 21 | <div id="main"> |
| 22 | |
| 23 | <div id="navigation"> |
| 24 | <h1>LuaExpat</h1> |
| 25 | <ul> |
| 26 | <li><a href="index.html">Home</a> |
| 27 | <ul> |
| 28 | <li><a href="index.html#overview">Overview</a></li> |
| 29 | <li><a href="index.html#status">Status</a></li> |
| 30 | <li><a href="index.html#download">Download</a></li> |
| 31 | <li><a href="index.html#history">History</a></li> |
| 32 | <li><a href="index.html#references">References</a></li> |
| 33 | <li><a href="index.html#credits">Credits</a></li> |
| 34 | <li><a href="index.html#contact">Contact</a></li> |
| 35 | </ul> |
| 36 | </li> |
| 37 | <li><strong>Manual</strong> |
| 38 | <ul> |
| 39 | <li><a href="manual.html#introduction">Introduction</a></li> |
| 40 | <li><a href="manual.html#installation">Installation</a></li> |
| 41 | <li><a href="manual.html#parser">Parser Objects</a></li> |
| 42 | </ul> |
| 43 | </li> |
| 44 | <li><a href="examples.html">Examples</a></li> |
| 45 | <li><a href="lom.html">Lua Object Model</a></li> |
| 46 | <li><a href="http://luaforge.net/projects/luaexpat/">Project</a> |
| 47 | <ul> |
| 48 | <li><a href="http://luaforge.net/tracker/?group_id=13">Bug Tracker</a></li> |
| 49 | <li><a href="http://luaforge.net/scm/?group_id=13">CVS</a></li> |
| 50 | </ul> |
| 51 | </li> |
| 52 | <li><a href="license.html">License</a></li> |
| 53 | </ul> |
| 54 | </div> <!-- id="navigation" --> |
| 55 | |
| 56 | <div id="content"> |
| 57 | |
| 58 | <h2><a name="introduction"></a>Introduction</h2> |
| 59 | |
| 60 | <p>LuaExpat is a <a href="http://www.saxproject.org/">SAX</a> XML |
| 61 | parser based on the <a href="http://www.libexpat.org/">Expat</a> library. |
| 62 | SAX is the <em>Simple API for XML</em> and allows programs to: |
| 63 | </p> |
| 64 | |
| 65 | <ul> |
| 66 | <li>process a XML document incrementally, thus being able to handle |
| 67 | huge documents without memory penalties;</li> |
| 68 | |
| 69 | <li>register handler functions which are called by the parser during |
| 70 | the processing of the document, handling the document elements or |
| 71 | text.</li> |
| 72 | </ul> |
| 73 | |
| 74 | <p>With an event-based API like SAX the XML document can be fed to |
| 75 | the parser in chunks, and the parsing begins as soon as the parser |
| 76 | receives the first document chunk. LuaExpat reports parsing events |
| 77 | (such as the start and end of elements) directly to the application |
| 78 | through callbacks. The parsing of huge documents can benefit from |
| 79 | this piecemeal operation.</p> |
| 80 | |
| 81 | <p>LuaExpat is distributed as a library and a file <code>lom.lua</code> that |
| 82 | implements the <a href="lom.html">Lua Object Model</a>.</p> |
| 83 | |
| 84 | |
| 85 | <h2><a name="building"></a>Building</h2> |
| 86 | |
| 87 | <p> |
| 88 | LuaExpat could be built to Lua 5.1 or to Lua 5.2. |
| 89 | In both cases, |
| 90 | the language library and headers files for the desired version |
| 91 | must be installed properly. |
| 92 | LuaExpat also depends on Expat 2.0.0+ which should also be installed. |
| 93 | </p> |
| 94 | <p> |
| 95 | LuaExpat offers a Makefile and a separate configuration file, |
| 96 | <code>config</code>, |
| 97 | which should be edited to suit the particularities of the target platform |
| 98 | before running |
| 99 | <code>make</code>. |
| 100 | The file has some definitions like paths to the external libraries, |
| 101 | compiler options and the like. |
| 102 | One important definition is the version of Lua language, |
| 103 | which is not obtained from the installed software. |
| 104 | </p> |
| 105 | |
| 106 | |
| 107 | <h2><a name="installation"></a>Installation</h2> |
| 108 | |
| 109 | <p>The compiled binary file should be copied to a directory in your |
| 110 | <a href="http://www.lua.org/manual/5.1/manual.html#pdf-package.cpath">C path</a>. |
| 111 | Lua 5.0 users should also install |
| 112 | <a href="http://www.keplerproject.org/compat">Compat-5.1</a>.</p> |
| 113 | |
| 114 | <p>Windows users can use the binary version of LuaExpat (<code>lxp.dll</code>, compatible with |
| 115 | <a href="http://luabinaries.luaforge.net">LuaBinaries</a>) available at |
| 116 | <a href="http://luaforge.net/projects/luaexpat/files">LuaForge</a>.</p> |
| 117 | |
| 118 | <p>The file <code>lom.lua</code> should be copied to a directory in your |
| 119 | <a href="http://www.lua.org/manual/5.1/manual.html#pdf-package.path">Lua path</a>.</p> |
| 120 | |
| 121 | <h2><a name="parser"></a>Parser objects</h2> |
| 122 | |
| 123 | <p>Usually SAX implementations base all operations on the |
| 124 | concept of a parser that allows the registration of callback |
| 125 | functions. LuaExpat offers the same functionality but uses a |
| 126 | different registration method, based on a table of callbacks. This |
| 127 | table contains references to the callback functions which are |
| 128 | responsible for the handling of the document parts. The parser will |
| 129 | assume no behaviour for any undeclared callbacks.</p> |
| 130 | |
| 131 | <h4>Constructor</h4> |
| 132 | |
| 133 | <dl class="reference"> |
| 134 | <dt><strong>lxp.new(<em>callbacks [, separator[, merge_character_data]]</em>)</strong></dt> |
| 135 | <dd>The parser is created by a call to the function <strong>lxp.new</strong>, |
| 136 | which returns the created parser or raises a Lua error. It |
| 137 | receives the callbacks table and optionally the parser <a href="#separator"> |
| 138 | separator character</a> used in the namespace expanded element names. |
| 139 | If <em>merge_character_data</em> is false then LuaExpat will not combine multiple |
| 140 | CharacterData calls into one. For more info on this behaviour see CharacterData below.</dd> |
| 141 | </dl> |
| 142 | |
| 143 | <h4>Methods</h4> |
| 144 | |
| 145 | <dl class="reference"> |
| 146 | <dt><strong>parser:close()</strong></dt> |
| 147 | <dd>Closes the parser, freeing all memory used by it. A call to |
| 148 | parser:close() without a previous call to parser:parse() could |
| 149 | result in an error.</dd> |
| 150 | |
| 151 | <dt><strong>parser:getbase()</strong></dt> |
| 152 | <dd>Returns the base for resolving relative URIs.</dd> |
| 153 | |
| 154 | <dt><strong>parser:getcallbacks()</strong></dt> |
| 155 | <dd>Returns the callbacks table.</dd> |
| 156 | |
| 157 | <dt><strong>parser:parse(s)</strong></dt> |
| 158 | <dd>Parse some more of the document. The string <em>s</em> contains |
| 159 | part (or perhaps all) of the document. When called without |
| 160 | arguments the document is closed (but the parser still has to be |
| 161 | closed).<br/> |
| 162 | The function returns a non nil value when the parser has been |
| 163 | succesfull, and when the parser finds an error it returns five |
| 164 | results: nil, <em>msg</em>, <em>line</em>, <em>col</em>, and |
| 165 | <em>pos</em>, which are the error message, the line number, |
| 166 | column number and absolute position of the error in the XML document.</dd> |
| 167 | |
| 168 | <dt><strong>parser:pos()</strong></dt> |
| 169 | <dd>Returns three results: the current parsing line, column, and |
| 170 | absolute position.</dd> |
| 171 | |
| 172 | <dt><strong>parser:getcurrentbytecount()</strong></dt> |
| 173 | <dd>Return the number of bytes of input corresponding to the current |
| 174 | event. This function can only be called inside a handler, in other |
| 175 | contexts it will return 0. Do not use inside a CharacterData handler |
| 176 | unless CharacterData merging has been disabled (see lxp.new).</dd> |
| 177 | |
| 178 | <dt><strong>parser:setbase(base)</strong></dt> |
| 179 | <dd>Sets the <em>base</em> to be used for resolving relative URIs in |
| 180 | system identifiers.</dd> |
| 181 | |
| 182 | <dt><strong>parser:setencoding(encoding)</strong></dt> |
| 183 | <dd>Set the encoding to be used by the parser. There are four |
| 184 | built-in encodings, passed as strings: "US-ASCII", |
| 185 | "UTF-8", "UTF-16", and "ISO-8859-1".</dd> |
| 186 | |
| 187 | <dt><strong>parser:stop()</strong></dt> |
| 188 | <dd>Abort the parser and prevent it from parsing any further |
| 189 | through the data it was last passed. Use to halt parsing the |
| 190 | document when an error is discovered inside a callback, for |
| 191 | example. The parser object cannot accept more data after |
| 192 | this call.</dd> |
| 193 | </dl> |
| 194 | |
| 195 | <h4>Callbacks</h4> |
| 196 | |
| 197 | <p>The Lua callbacks define the handlers of the parser events. The |
| 198 | use of a table in the parser constructor has some advantages over |
| 199 | the registration of callbacks, since there is no need for for the API |
| 200 | to provide a way to manipulate callbacks.</p> |
| 201 | |
| 202 | <p>Another difference lies in the behaviour of the callbacks during |
| 203 | the parsing itself. The callback table contains references to the |
| 204 | functions that can be redefined at will. The only restriction is |
| 205 | that only the callbacks present in the table at creation time |
| 206 | will be called.</p> |
| 207 | |
| 208 | <p>The callbacks table indices are named after the equivalent Expat |
| 209 | callbacks:<br /> |
| 210 | <em>CharacterData</em>, <em>Comment</em>, |
| 211 | <em>Default</em>, <em>DefaultExpand</em>, <em>EndCDataSection</em>, |
| 212 | <em>EndElement</em>, <em>EndNamespaceDecl</em>, |
| 213 | <em>ExternalEntityRef</em>, <em>NotStandalone</em>, |
| 214 | <em>NotationDecl</em>, <em>ProcessingInstruction</em>, |
| 215 | <em>StartCDataSection</em>, <em>StartElement</em>, |
| 216 | <em>StartNamespaceDecl</em>, <em>UnparsedEntityDecl</em>, |
| 217 | <em>XmlDecl</em> and <em>StartDoctypeDecl</em>.</p> |
| 218 | |
| 219 | <p>These indices can be references to functions with |
| 220 | specific signatures, as seen below. The parser constructor also |
| 221 | checks the presence of a field called <em>_nonstrict</em> in the |
| 222 | callbacks table. If <em>_nonstrict</em> is absent, only valid |
| 223 | callback names are accepted as indices in the table |
| 224 | (Defaultexpanded would be considered an error for example). If |
| 225 | <em>_nonstrict</em> is defined, any other fieldnames can be |
| 226 | used (even if not called at all).</p> |
| 227 | |
| 228 | <p>The callbacks can optionally be defined as <code>false</code>, |
| 229 | acting thus as placeholders for future assignment of functions.</p> |
| 230 | |
| 231 | <p>Every callback function receives as the first parameter the |
| 232 | calling parser itself, thus allowing the same functions to be used |
| 233 | for more than one parser for example.</p> |
| 234 | |
| 235 | <dl class="reference"> |
| 236 | <dt><strong>callbacks.CharacterData = function(parser, string)</strong></dt> |
| 237 | <dd>Called when the <em>parser</em> recognizes an XML CDATA <em>string</em>. |
| 238 | Note that LuaExpat automatically combines multiple CharacterData events |
| 239 | from Expat into a single call to this handler, unless <em>merge_character_data</em> |
| 240 | is set to false when calling lxp.new().</dd> |
| 241 | |
| 242 | <dt><strong>callbacks.Comment = function(parser, string)</strong></dt> |
| 243 | <dd>Called when the <em>parser</em> recognizes an XML comment |
| 244 | <em>string</em>.</dd> |
| 245 | |
| 246 | <dt><strong>callbacks.Default = function(parser, string)</strong></dt> |
| 247 | <dd>Called when the <em>parser</em> has a <em>string</em> |
| 248 | corresponding to any characters in the document which wouldn't |
| 249 | otherwise be handled. Using this handler has the side effect of |
| 250 | turning off expansion of references to internally defined general |
| 251 | entities. Instead these references are passed to the default |
| 252 | handler.</dd> |
| 253 | |
| 254 | <dt><strong>callbacks.DefaultExpand = function(parser, string)</strong></dt> |
| 255 | <dd>Called when the <em>parser</em> has a <em>string</em> |
| 256 | corresponding to any characters in the document which wouldn't |
| 257 | otherwise be handled. Using this handler doesn't affect expansion |
| 258 | of internal entity references.</dd> |
| 259 | |
| 260 | <dt><strong>callbacks.EndCdataSection = function(parser)</strong></dt> |
| 261 | <dd>Called when the <em>parser</em> detects the end of a CDATA |
| 262 | section.</dd> |
| 263 | |
| 264 | <dt><strong>callbacks.EndElement = function(parser, elementName)</strong></dt> |
| 265 | <dd>Called when the <em>parser</em> detects the ending of an XML |
| 266 | element with <em>elementName</em>.</dd> |
| 267 | |
| 268 | <dt><strong>callbacks.EndNamespaceDecl = function(parser, namespaceName)</strong></dt> |
| 269 | <dd>Called when the <em>parser</em> detects the ending of an XML |
| 270 | namespace with <em>namespaceName</em>. The handling of the end |
| 271 | namespace is done after the handling of the end tag for the element |
| 272 | the namespace is associated with.</dd> |
| 273 | |
| 274 | <dt><strong>callbacks.ExternalEntityRef = function(parser, subparser, base, systemId, publicId)</strong></dt> |
| 275 | <dd>Called when the <em>parser</em> detects an external entity |
| 276 | reference.<br/><br/> |
| 277 | The <em>subparser</em> is a LuaExpat parser created with the |
| 278 | same callbacks and Expat context as the <em>parser</em> and should |
| 279 | be used to parse the external entity.<br/> |
| 280 | The <em>base</em> parameter is the base to use for relative |
| 281 | system identifiers. It is set by parser:setbase and may be nil.<br/> |
| 282 | The <em>systemId</em> parameter is the system identifier |
| 283 | specified in the entity declaration and is never nil.<br/> |
| 284 | The <em>publicId</em> parameter is the public id given in the |
| 285 | entity declaration and may be nil.</dd> |
| 286 | |
| 287 | <dt><strong>callbacks.NotStandalone = function(parser)</strong></dt> |
| 288 | <dd>Called when the <em>parser</em> detects that the document is not |
| 289 | "standalone". This happens when there is an external subset or a |
| 290 | reference to a parameter entity, but the document does not have standalone set |
| 291 | to "yes" in an XML declaration.</dd> |
| 292 | |
| 293 | <dt><strong>callbacks.NotationDecl = function(parser, notationName, base, systemId, publicId)</strong></dt> |
| 294 | <dd>Called when the <em>parser</em> detects XML notation |
| 295 | declarations with <em>notationName</em><br/> |
| 296 | The <em>base</em> parameter is the base to use for relative |
| 297 | system identifiers. It is set by parser:setbase and may be nil.<br/> |
| 298 | The <em>systemId</em> parameter is the system identifier |
| 299 | specified in the entity declaration and is never nil.<br/> |
| 300 | The <em>publicId</em> parameter is the public id given in the |
| 301 | entity declaration and may be nil.</dd> |
| 302 | |
| 303 | <dt><strong>callbacks.ProcessingInstruction = function(parser, target, data)</strong></dt> |
| 304 | <dd>Called when the <em>parser</em> detects XML processing |
| 305 | instructions. The <em>target</em> is the first word in the |
| 306 | processing instruction. The <em>data</em> is the rest of the |
| 307 | characters in it after skipping all whitespace after the initial |
| 308 | word.</dd> |
| 309 | |
| 310 | <dt><strong>callbacks.StartCdataSection = function(parser)</strong></dt> |
| 311 | <dd>Called when the <em>parser</em> detects the begining of an XML |
| 312 | CDATA section.</dd> |
| 313 | |
| 314 | <dt><strong>callbacks.XmlDecl = function(parser, version, encoding)</strong></dt> |
| 315 | <dd>Called when the <em>parser</em> encounters an XML document declaration (these are |
| 316 | optional, and valid only at the start of the document). The callback receives |
| 317 | the declared XML <em>version</em> and document <em>encoding</em>.</dd> |
| 318 | |
| 319 | <dt><strong>callbacks.StartElement = function(parser, elementName, attributes)</strong></dt> |
| 320 | <dd>Called when the <em>parser</em> detects the begining of an XML |
| 321 | element with <em>elementName</em>.<br/> |
| 322 | The <em>attributes</em> parameter is a Lua table with all the |
| 323 | element attribute names and values. The table contains an entry for |
| 324 | every attribute in the element start tag and entries for the |
| 325 | default attributes for that element.<br/> |
| 326 | The attributes are listed by name (including the inherited ones) |
| 327 | and by position (inherited attributes are not considered in the |
| 328 | position list).<br/> |
| 329 | As an example if the <em>book</em> element has attributes |
| 330 | <em>author</em>, <em>title</em> and an optional <em>format</em> |
| 331 | attribute (with "printed" as default value), |
| 332 | <pre class="example"> |
| 333 | <book author="Ierusalimschy, Roberto" title="Programming in Lua"> |
| 334 | </pre> |
| 335 | would be represented as<br/> |
| 336 | <pre class="example"> |
| 337 | {[1] = "Ierusalimschy, Roberto", |
| 338 | [2] = "Programming in Lua", |
| 339 | author = "Ierusalimschy, Roberto", |
| 340 | format = "printed", |
| 341 | title = "Programming in Lua"} |
| 342 | </pre></dd> |
| 343 | |
| 344 | <dt><strong>callbacks.StartNamespaceDecl = function(parser, namespaceName)</strong></dt> |
| 345 | <dd>Called when the <em>parser</em> detects an XML namespace |
| 346 | declaration with <em>namespaceName</em>. Namespace declarations |
| 347 | occur inside start tags, but the StartNamespaceDecl handler is |
| 348 | called before the StartElement handler for each namespace declared |
| 349 | in that start tag.</dd> |
| 350 | |
| 351 | <dt><strong>callbacks.UnparsedEntityDecl = function(parser, entityName, base, systemId, publicId, notationName)</strong></dt> |
| 352 | <dd>Called when the <em>parser</em> receives declarations of |
| 353 | unparsed entities. These are entity declarations that have a |
| 354 | notation (NDATA) field.<br/> |
| 355 | As an example, in the chunk |
| 356 | <pre class="example"> |
| 357 | <!ENTITY logo SYSTEM "images/logo.gif" NDATA gif> |
| 358 | </pre> |
| 359 | <em>entityName</em> would be "logo", <em>systemId</em> would be |
| 360 | "images/logo.gif" and <em>notationName</em> would be "gif". |
| 361 | For this example the <em>publicId</em> parameter would be nil. |
| 362 | The <em>base</em> parameter would be whatever has been set with |
| 363 | <code>parser:setbase</code>. If not set, it would be nil.</dd> |
| 364 | |
| 365 | <dt><strong>callbacks.StartDoctypeDecl = function(parser, name, sysid, pubid, has_internal_subset)</strong></dt> |
| 366 | <dd>Called when the <em>parser</em> detects the beginning of an XML |
| 367 | DTD (DOCTYPE) section. These precede the XML root element and take |
| 368 | the form: |
| 369 | <pre class="example"> |
| 370 | <!DOCTYPE root_elem PUBLIC "example"> |
| 371 | </pre> |
| 372 | </dd> |
| 373 | </dl> |
| 374 | |
| 375 | <h4><a name="separator"></a>The separator character</h4> |
| 376 | |
| 377 | <p>The optional separator character in the parser constructor |
| 378 | defines the character used in the namespace expanded element names. |
| 379 | The separator character is optional (if not defined the parser will |
| 380 | not handle namespaces) but if defined it must be different from |
| 381 | the character '\0'.</p> |
| 382 | |
| 383 | </div> <!-- id="content" --> |
| 384 | |
| 385 | </div> <!-- id="main" --> |
| 386 | |
| 387 | </div> <!-- id="container" --> |
| 388 | |
| 389 | </body> |
| 390 | </html> |