# XMLS

## Summary

Xmls is a small, simple, non-validating xml parser for Common Lisp. It's designed to be a self-contained, easily embedded parser that recognizes a useful subset of the XML spec. It provides a simple mapping from xml to lisp s-expressions and back.

## Features

- Free (BSD license).
- Understands enough of the xml spec to parse many common documents, including those occurring in common internet protocols like xml-rpc, webdav, and BEEP. Parses 85 out of the 98 valid documents in the oasis parser compliance suite.
- Small and easily embedded. The entire parser is contained in one file and it's currently less than 600 lines of code. Xmls is written in pure lisp and requires no external parsing tools or foreign libraries.
- Supports xml namespaces.
- Threadsafe.
- Serializes s-expr list structures back to xml as well as parsing xml.

## Limitations

- Parses entire document into memory and consequently can't handle large documents.
- No detailed error reporting.

## Xml Representation

Parsed xml is represented as a lisp list. A node is represented as follows:

    (name (attributes) children*)

A name is either a simple string, if the element does not belong to a namespace, or a list of (name namespace-url) if the element does belong to a namespace.

Attributes are stored as (name value) lists.

Children are stored as a list of either element nodes or text nodes.

For example, the following xml document:

    <?xml version="1.0"?>
    <!-- test document -->
    <book title='The Cyberiad'>
      <!-- comment in here -->
      <author xmlns='http://authors'>Stanislaw Lem</author>
      <info:subject xmlns:info='http://bookinfo' rank='1'>&quot;Cybernetic Fables&quot;</info:subject>
    </book>

Would parse as:

    ("book" (("title" "The Cyberiad"))
     (("author" . "http://authors") NIL "Stanislaw Lem")
     (("subject" . "http://bookinfo") (("rank" "1")) "\"Cybernetic Fables\""))

Xmls also includes a helper function, make-node for creating xml nodes of this form:

    (make-node &key name ns attrs children)

Xmls provides the corresponding accessor functions node-name, node-ns node-attrs, and node-children.

## Usage

The interface is straightforward. The two main functions are parse and toxml.

    (parse source &key (compress-whitespace t))

Parse accepts either a string or an input stream and attempts to parse the xml document contained therein. It will return the s-expr parse tree if it's successful or nil if parsing fails. If compress-whitespace is t, content nodes will be trimmed of whitespace and empty whitespace strings between nodes will be discarded.

    (write-xml xml stream &key (indent nil))

write-xml accepts a lisp list in the format described above and writes the equivalent xml string to stream. Currently, if nodes use namespaces xmls will not assign namespaces prefixes but will explicitly assign the namespace to each node. This will be changed in a later release. Xmls will indent the generated xml output if indent is non-nil.

    (toxml node &key (indent nil))

Toxml is a convenience wrapper around write-xml that returns the in a newly allocated string.

### Helper functions

These are intended to allow programmers to avoid direct manipulation of the s-expression representation. If you use these, your code should be easier to read and you will avoid problems if there is a change in internal representation (such changes would be hard to even find, much less correct, if using the lists directly).

`make-xmlrep (tag &key attribs children)`
  Constructor function.
`xmlrep-add-child! (xmlrep child)`
  Add a new child node to the XMLREP node.
`xmlrep-tag (xmlrep)`
  Extract the tag from XMLREP.
`xmlrep-tagmatch (tag treenode)`
  Returns true if TAG is the tag of TREENODE. Match is case insensitive (quite possibly this is the Wrong Thing).
`xmlrep-attribs (xmlrep)`
  Extract the attributes from an XMLREP node.
`xmlrep-children (xmlrep)`
  Extract the children from an XMLREP node.
`xmlrep-find-child-tags (tag treenode)`
  Return all of the (direct) children of TREENODE whose tags are TAG. Matching done by `xmlrep-tagmatch`.
`xmlrep-find-child-tag (tag treenode &optional (if-unfound :error))`
  Find a single child of TREENODE with TAG. Returns an error if there is more or less than one such child.
`xmlrep-attrib-value (attrib treenode &optional (if-undefined :error))`
  Find the value of ATTRIB, a string, in TREENODE. if there is no ATTRIB, will return the value of IF-UNDEFINED, which defaults to :ERROR.
`xmlrep-boolean-attrib-value (attrib treenode &optional (if-undefined :error))`
  Find the value of ATTRIB, a string, in TREENODE. The value should be either "true" or "false". The function will return T or NIL, accordingly. If there is no ATTRIB, will return the value of IF-UNDEFINED, which defaults to :ERROR.

## Installation

xmls can be installed as an asdf system. An asdf system definition is provided with the distribution.

Previous versions of XMLS were single files, and could be installed simply by loading the file xmls.lisp. This option is no longer supported.

## Contact Information

Please contact Robert Goldman, rpgoldman AT sift.info with any questions or bug reports.