Module:SLAXML/doc

SLAXML is a pure-Lua SAX-like streaming XML parser. It is more robust than many (simpler) pattern-based parsers that exist, properly supporting code like, CDATA nodes, comments, namespaces, and processing instructions.

It is currently not a truly valid XML parser, however, as it allows certain XML that is syntactically-invalid (not well-formed) to be parsed without reporting an error.

Features

 * Pure Lua in a single file (two files if you use the DOM parser).
 * Streaming parser does a single pass through the input and reports what it sees along the way.
 * Supports processing instructions.
 * Supports comments.
 * Supports CDATA sections.
 * Supports namespaces, resolving prefixes to the proper namespace URI ( and  ).
 * Supports unescaped greater-than symbols in attribute content (a common failing for simpler pattern-based parsers).
 * Unescapes named XML entities and numeric entities (e.g.  ) in attributes and text nodes (but—properly—not in comments or CDATA). Properly handles edge cases like.
 * Optionally ignore whitespace-only text nodes (as appear when indenting XML markup).
 * Includes a DOM parser that is a both a convenient way to pull in XML to use as well as a nice example of using the streaming parser.
 * Does not add any keys to the global namespace.

Usage
If you just want to see if it will parse your document correctly, you can simply do:

DOM Builder
If you simply want to build tables from your XML, you can alternatively:

The returned table is a 'document' comprised of tables for elements, attributes, text nodes, comments, and processing instructions. See the following documentation for what each supports.

DOM Table Features

 * Document - the root table returned from the  method.
 * : the string
 * : the string
 * : an array table of child processing instructions, the root element, and comment nodes.
 * : the root element for the document
 * Element
 * : the string
 * : the string name of the element (without any namespace prefix)
 * : the namespace URI for this element;  if no namespace is applied
 * : a table of attributes, indexed by name and index
 * : any namespace prefix of the attribute is not part of the name
 * : an single attribute table (see below); useful for iterating all attributes of an element, or for disambiguating attributes with the same name in different namespaces
 * : an array table of child elements, text nodes, comment nodes, and processing instructions
 * : an array table of child elements only
 * : reference to the the parent element or document table
 * Attribute
 * : the string
 * : the name of the attribute (without any namespace prefix)
 * : the string value of the attribute (with XML and numeric entities unescaped)
 * : the namespace URI for the attribute;  if no namespace is applied
 * : reference to the the parent element table
 * Text - for both CDATA and normal text nodes
 * : the string
 * : the string
 * : the string content of the text node (with XML and numeric entities unescaped for non-CDATA elements)
 * : reference to the the parent element table
 * Comment
 * : the string
 * : the string
 * : the string content of the attribute
 * : reference to the the parent element or document table
 * Processing Instruction
 * : the string
 * : the string name of the PI, e.g.  has a name of
 * : the string content of the PI, i.e. everything but the name
 * : reference to the the parent element or document table

Finding Text for a DOM Element
The following function can be used to calculate the "inner text" for an element:

A Simpler DOM
If you want the DOM tables to be simpler-to-serialize you can supply the simple option via:

In this case no table will have a parent attribute, elements will not have the el collection, and the attr collection will be a simple array (without values accessible directly via attribute name). In short, the output will be a strict hierarchy with no internal references to other tables, and all data represented in exactly one spot.

Known Limitations / TODO

 * Does not require or enforce well-formed XML. Certain syntax errors are silently ignored and consumed. For example:
 * is seen as a valid attribute
 * invokes two  calls but no   calls
 * invokes  followed by
 * No support for custom entity expansion other than the standard XML entities and numeric ASCII entities (e.g.  )
 * XML Declarations are incorrectly reported as Processing Instructions
 * No support for DTDs
 * No support for extended (Unicode) characters in element/attribute names
 * No support for charset
 * No support for XInclude

History

 * v0.5.1 2013-Feb-18
 * now directly generates  with no post callback for   required.


 * v0.5 2013-Feb-18
 * Use the  pattern to prevent any pollution of the global namespace.


 * v0.4.3 2013-Feb-17
 * Bugfix to allow empty attributes, i.e.
 * no longer includes namespace prefix in the name, includes the nsURI


 * v0.4 2013-Feb-16
 * DOM adds  references
 * is now
 * "simple" mode for DOM parsing


 * v0.3 2013-Feb-15
 * Support namespaces for elements and attributes
 * will call  followed by   (and then  ); you must apply the namespace to your element after creation.
 * Child elements without a namespace prefix that inherit a namespace will receive
 * will call
 * will call
 * Runtime errors are generated for any namespace prefix that cannot be resolved
 * Add (optional) DOM parser that validates hierarchy and supports namespaces


 * v0.2 2013-Feb-15
 * Supports expanding numeric entities e.g.  ->
 * Utility functions are local to parsing (not spamming the global namespace)


 * v0.1 2013-Feb-7
 * Option to ignore whitespace-only text nodes
 * Supports unescaped > in attributes
 * Supports CDATA
 * Supports Comments
 * Supports Processing Instructions

License
Copyright (c) 2013 Gavin Kistner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.