Closure XML Parser

An XML parser written in Common Lisp.

Closure XML was written by Gilbert Baumann (unk6 at rz.uni-karlsruhe.de) as part of the Closure web browser.
Contributions to the parser by

Mailing list cxml-devel is hosted on common-lisp.net.

Download

Get a tarball.

David's tla archive is at http://www.common-lisp.net/project/cxml/david@knowledgetools.de--cxml/. (Brief tla usage instructions: Unpack the cxml tarball.  Enter tla register-archive URL to turn it into a working copy.  tla update is similar to cvs up.)

Contents

Recent changes

patch-306 (2004-09-03)

patch-279 (2004-05-11)

patch-204 (buggy release)

patch-191 (2004-03-18)

CXML Modules

CXML provides three packages:

Installation

CXML is written in Common Lisp and should be portable to all Common Lisp implementations.  Currently assumed to work are ACL, SBCL, CMUCL, and CLISP, though development is done on ACL.  (CLISP needs some -E option teaching it to accept non-ASCII source files.)

ASDF is used for compilation. The following instructions assume that ASDF has already been loaded.

Configuration (optional). CXML has full Unicode code support -- even on Lisps without Unicode strings. On non-unicode aware Lisps, DOMString is implemented as an array of character codes. If your Lisp supports 16 bit characters natively, you can enable feature RUNE-IS-CHARACTER to select an alternative DOMString implementatation, which uses real characters instead of characters codes.

* (pushnew :rune-is-character *features*)

Compiling and loading CXML. Register the .asd file, e.g. by symlinking it:

$ ln -sf `pwd`/cxms.asd /path/to/your/registry

Then compile CXML using:

* (asdf:operate 'asdf:load-op :cxml)

Tests

Check out the XML and DOM testsuites:

$ export CVSROOT=:pserver:anonymous@dev.w3.org:/sources/public
$ cvs login    # password is "anonymous"
$ cvs co 2001/XML-Test-Suite/xmlconf
$ cvs co 2001/DOM-Test-Suite

Usage and expected output:

* (xmlconf:run-all-tests "/path/to/2001/XML-Test-Suite/xmlconf/")
0/556 tests failed; 1606 tests were skipped
* (domtest:run-all-tests "/path/to/2001/DOM-Test-Suite/")
0/450 tests failed; 71 tests were skipped

fixme: Add an explanation of xml/sax-tests here.

fixme My parser does not understand the current testsuite anymore.  To fix this problem, revert the affected files manually after check-out:

$ cd 2001/XML-Test-Suite/xmlconf/
xmltest$ patch -p0 -R </path/to/cxml/test/xmlconf-base.diff

The log message for the changes reads "Removed unnecessary xml:base attribute".  If I understand correctly, only DOM 3 parsers provide the baseURI attribute necessary for understanding xmlconf.xml now.  We don't have that yet.

To do

(Compare also with Gilbert Baumann's older TODO list in xml-parse.lisp.)

Using CXML

Parsing and validating

Function CXML:PARSE-FILE (pathname handler &key validate root)
Function CXML:PARSE-STREAM (stream handler &key validate root)
Function CXML:PARSE-OCTETS (octets handler &key validate root)
Parse a CXML document.  Return values from this function depend on the SAX handler used.
Arguments:

Common keyword arguments:

Arguments to validate:

When validating, argument root can be used to override the name given in the DOCTYPE declaration.  This is useful together with a caller-specified DTD instance.

Function CXML:PARSE-DTD-FILE (pathname)
Function CXML:PARSE-DTD-STREAM (stream)
Parse
declarations from a stand-alone file and return an object representing the DTD, suitable as an argument to validate.

Function DOM:MAKE-DOM-BUILDER ()
Create a SAX handler which builds a DOM document.  Example:

(cxml:parse-file "test.xml" (dom:make-dom-builder))

Serialization

Function CXML:UNPARSE-DOCUMENT (document stream &rest keys)
Function CXML:UNPARSE-DOCUMENT-TO-OCTETS (document &rest keys) => vector
Serialize a DOM document object.

Keyword arguments:

The following canonical values are allowed:

With an indentation level, pretty-print the XML by inserting additional whitespace.  Note that indentation changes the document model and should only be used if whitespace does not matter to the application.

unparse-document-to-octets returns an (unsigned-byte 8) array, whereas unparse-document writes characters.  unparse-document is useful together with with-output-to-string.  However, note that the resulting document in both cases is UTF-8 encoded, so the characters written by unparse-document are really UTF-8 bytes encoded as characters.

Function CXML:MAKE-CHARACTER-STREAM-SINK (stream &rest keys) => sink
Function CXML:MAKE-OCTET-VECTOR-SINK (&rest keys) => sink
Return a handle suitable for event-based XML serialization.

These function provide the low-level mechanism used by the DOM serialization functions. To serialize a document without building its DOM tree first, create a sink handle and call SAX functions on that handle. sax:end-document returns the serialized form of the document described by the SAX events.

Macro CXML:WITH-XML-OUTPUT (sink &body body) => vector
Macro CXML:WITH-ELEMENT (qname &body body) => result
Function CXML:ATTRIBUTE (name value) => value
Function CXML:TEXT (data) => data
Function CXML:CDATA (data) => data
Convenience syntax for event-based serialization.

Example:

(with-xml-output (make-octet-stream-sink stream :indentation 2 :canonical nil)
  (with-element "foo"
    (attribute "xyz" "abc")
    (with-element "bar"
      (attribute "blub" "bla"))
    (text "Hi there.")))

Prints this to stream, which must be an (unsigned-byte 8) stream:

<foo xyz="abc">
  <bar blub="bla"></bar>
  Hi there.
</foo>

(Note that these functions accept both strings and rods, so we could write "foo" instead of #"foo" above.)

Miscellaneous utility functions

Function CXML:MAKE-VALIDATOR (dtd root)
Create a SAX handler which validates against a DTD instance.  The document's root element must be named root.  Used with dom:map-document, this validates a document object as if by re-reading it with a validating parser, except that declarations recorded in the document instance are completely ignored.
Example:

(let ((d (parse-file "~/test.xml" (dom:make-dom-builder)))
      (x (parse-dtd-file "~/test.dtd")))
  (dom:map-document (cxml:make-validator x #"foo") d))

Function DOM:MAP-DOCUMENT (handler document &key include-xmlns-attributes include-default-values)
Traverse a DOM document and call SAX functions as if an XML representation of the document were processed by a SAX parser.

XMLS compatibility

Like other XML parsers written in Lisp, CXML can work with documents represented as list structures. The specific model implemented by cxml is compatible with the xmls parser. Xmls list structures are a simpler and faster alternative to full DOM document trees. They also serve as an example showing how to implement user-defined document models as an independent layer over the the base parser (c.f. xmls/xmls-compat.lisp in the cxml distribution). However, note that the list structures do not include all information available in DOM documents and are sometimes more difficult to work wth since many DOM functions cannot be implemented on them.

Function CXML-XMLS:MAKE-XMLS-BUILDER ()
Create a SAX handler which builds XMLS list structures.  Example:

(cxml:parse-file "test.xml" (cxml-xmls:make-xmls-builder))

Function CXML-XMLS:MAP-NODE (handler node &key include-xmlns-attributes)
Traverse an XMLS document/node and call SAX functions as if an XML representation of the document were processed by a SAX parser.

Use this function to serialize XMLS data. For example, we could define a replacement for xmls:write-xml like this:

(defun write-xml (stream node &key indent)
  (let ((sink (cxml:make-character-stream-sink
               stream :canonical nil :indentation indent)))
    (cxml-xmls:map-node sink node)))

Function CXML-XMLS:MAKE-NODE (&key name attrs children) => xmls node
Build a list node of the form (name ((name value)*child*).

Accessor CXML-XMLS:NODE-NAME (node)
Accessor CXML-XMLS:NODE-ATTRS (node)
Accessor CXML-XMLS:NODE-CHILDREN (node)
Accessors for xmls node data.

fixme: namespace support.

Dealing with rods

As explained above, the XML parser handles character encoding and uses 16bit strings internally. Instead of using characters and strings it uses runes and rods. This is seen as a feature, but can be inconvenient.

Function CXML:MAKE-RECODER (chained-handler &optional recoder-fn)
Return a SAX handler which passes all events on to chained-handler after converting all strings and rods using recoder-fn, a function of one argument which defaults to runes:rod-string.

Example. In a Lisp which ordinarily would use octet vector rods:

CL-USER(14): (cxml:parse-string "<test/>" (cxml-xmls:make-xmls-builder))
(#(116 101 115 116) NIL)

Use a SAX recoder to get strings instead::

CL-USER(17): (parse-string "<test/>" (cxml:make-recoder (cxml-xmls:make-xmls-builder)))
("test" NIL)

SAX interface

A SAX handler is an arbitrary objects that implements some of the generic functions in the SAX package.  Note that no default handler class is necessary, because all generic functions have default methods which do nothing.  SAX functions are:

Function SAX:START-DOCUMENT (handler)
Function SAX:END-DOCUMENT (handler)

Function SAX:START-ELEMENT (handler namespace-uri local-name qname attributes)
Function SAX:END-ELEMENT (handler namespace-uri local-name qname)
Function SAX:START-PREFIX-MAPPING (handler prefix uri)
Function SAX:END-PREFIX-MAPPING (handler prefix)
Function SAX:PROCESSING-INSTRUCTION (handler target data)
Function SAX:COMMENT (handler data)
Function SAX:START-CDATA (handler)
Function SAX:END-CDATA (handler)
Function SAX:CHARACTERS (handler data)

Function SAX:START-DTD (handler name public-id system-id)
Function SAX:END-DTD (handler)
Function SAX:UNPARSED-ENTITY-DECLARATION (handler name public-id system-id notation-name)
Function SAX:EXTERNAL-ENTITY-DECLARATION (handler kind name public-id system-id)
Function SAX:INTERNAL-ENTITY-DECLARATION (handler kind name value)
Function SAX:NOTATION-DECLARATION (handler name public-id system-id)
Function SAX:ELEMENT-DECLARATION (handler name model)
Function SAX:ATTRIBUTE-DECLARATION (handler ename aname type default)

Accessor SAX:ATTRIBUTE-PREFIX (attribute)
Accessor SAX:ATTRIBUTE-NAMESPACE-URI (attribute)
Accessor SAX:ATTRIBUTE-LOCAL-NAME (attribute)
Accessor SAX:ATTRIBUTE-VALUE (attribute)
Accessor SAX:ATTRIBUTE-QNAME (attribute)
Accessor SAX:ATTRIBUTE-SPECIFIED-P (attribute)

The entity declaration methods are similar to Java SAX definitions, but parameter entities are distinguished from general entities not by a % prefix to the name, but by the kind argument, either :parameter or :general.

The arguments to sax:element-declaration and sax:attribute-declaration differ significantly from their Java counterparts.

fixme: For more information on these functions refer to the docstrings.

DOM Notes

CXML implements the DOM Level 1 Core interfaces.  Explaining DOM is better left to the specification, so please refer to the official W3C documents for DOM.

However, there is no "standard" DOM mapping for Lisp.  DOM is specified in CORBA IDL, but it refrains from using object-oriented IDL features, allowing for a much more natural Lisp implemenation than the the ordinary IDL/Lisp mapping would.

Differences between CXML's DOM and the direct IDL/Lisp mapping:

Example:

XML(97): (dom:node-type
          (dom:document-element
           (cxml:parse-file "~/test.xml" (dom:make-dom-builder))))
:ELEMENT