Skip to content
Jennifer Programming Language

xml - XML encode / decode

Enable with use xml;. Parses and serializes XML over an opaque xml.Value, designed like json and toml: decode returns a handle walked with accessors, and a small build surface constructs a tree to encode. Hand-rolled (no reflect-bound encoding/xml), so it runs on both binaries.

XML is a tree of elements, each with a tag name, ordered attributes, and ordered children mixing elements and text. Navigation uses a small XPath-style path dialect instead of JSON Pointer.

jennifer
use io;
use xml;

def root as xml.Value init xml.decode(
    "<library><book id=\"1\"><title>Go</title></book></library>");
io.printf("%s\n", xml.tag($root));                         # library
io.printf("%s\n", xml.attr(xml.get($root, "book"), "id")); # 1
io.printf("%s\n", xml.text(xml.get($root, "book/title"))); # Go

Decode / encode

CallReturnsNotes
xml.decode(s)xml.ValueParse a document into the root element. Errors (with line / column) on malformed XML, including a raw character XML 1.0 does not allow (see Building), anywhere in the document.
xml.decodeWith(s, o)xml.ValueSame, with an xml.DecodeOptions{maxDepth, maxNodes, namespaces}: lower caps (see Notes and limits), and namespaces (a map of string to string, prefix to namespace URI) puts bindings in scope at the root, for a fragment cut out of a larger document (see Namespaces).
xml.encode(v)stringSerialize compactly.
xml.encodePretty(v)stringSerialize with 2-space indentation.

decode handles elements, attributes, text, <![CDATA[...]]> (as text), comments, processing instructions, an XML declaration, and a DOCTYPE (the last four are skipped, not kept; the DOCTYPE's internal subset is skipped unparsed, quoted literals and comments included). The five predefined entities (&lt; &gt; &amp; &apos; &quot;) and numeric character references (&#65;, &#x41;) are decoded; an unknown entity is an error. Names keep their prefix as written (kml:Document), and decode also resolves each one to its namespace URI; a document that is not namespace-well-formed (an undeclared prefix, say) is an error. See Namespaces.

Decoding is strict about well-formedness: an invalid name (<1a>), a character reference with a sign or an upper-case &#X, -- inside a comment, a bare ]]> in text, an XML declaration anywhere but the very start (a byte-order mark may precede it), and input that is not valid UTF-8 are all errors. Line ends in text and CDATA are normalized as XML requires (CR LF and a lone CR become LF); a CR written as &#13; is kept, and encode writes a CR in text back that way. The encoding named in the declaration is not consulted: the text is already a decoded string.

encodePretty indents element-only content, but an element that contains any text child is emitted inline so its character data stays byte-exact.

Reading

Every accessor takes an xml.Value. xml.get / xml.findAll / xml.has take a path as a second argument and an optional prefix map as a third (see below).

CallReturnsNotes
xml.typeOf(node)string"element" or "text".
xml.tag(node)stringThe element's tag name as written (with prefix, if any).
xml.localName(node)stringThe tag name without its prefix (Document for kml:Document).
xml.namespace(node)stringThe element's namespace URI; "" for none.
xml.lookupPrefix(node, prefix)stringThe namespace URI bound to prefix at this element ("" for the default namespace when none is declared); errors when the prefix is unbound. Works on a sub-tree too.
xml.text(node)stringThe concatenated character data of the element's direct text children (or a text node's own string).
xml.attr(node, name)stringAn attribute's value; errors if the attribute is absent.
xml.hasAttr(node, name)boolWhether the attribute is present.
xml.attrNS(node, uri, local)stringThe attribute named (uri, local), whatever its prefix; uri "" reads an unprefixed attribute. Errors if absent.
xml.hasAttrNS(node, uri, local)boolWhether that attribute is present.
xml.attrs(node)list of stringThe attribute names, in document order.
xml.children(node)list of xml.ValueThe element children (text children are excluded).
xml.get(node, path [, ns])xml.ValueThe first element matching the path; errors if none.
xml.findAll(node, path [, ns])list of xml.ValueEvery element matching the path (empty if none).
xml.has(node, path [, ns])boolWhether the path matches at least one element.

Path dialect

A path is /-separated steps, evaluated against the element children of the node (a leading / is optional; "" addresses the node itself):

StepMatches
nameevery element child with that tag.
name[k]the 1-based k-th element child with that tag.
*every element child, whatever its tag.
*:localevery element child with that local name, in any namespace (or none).

Without a prefix map a name step matches the tag as written, prefix included. With one, it matches by namespace instead - see Namespaces.

xml.findAll($root, "book") returns all book children; xml.get($root, "book[2]/title") drills to the second book's title. Attributes and text are read with xml.attr / xml.text after navigating, not addressed inside the path. A malformed path is an error; a well-formed path that matches nothing is an empty findAll (and a false has, and an error from get).

Building

The build surface is non-mutating: each call returns a fresh xml.Value ($v = xml.setAttr($v, ...)), as in json / toml:

CallReturnsNotes
xml.element(name)xml.ValueA new empty element. Namespace-unaware: its name is used as written and encode adds no declaration for it. name must be an XML name with at most one colon.
xml.elementNS(uri, qname)xml.ValueA new empty element in namespace uri ("" for none); qname may carry a prefix (kml:Placemark).
xml.setAttr(node, name, value)xml.ValueThe element with the attribute added or updated. name as for element. On a namespace-aware element (decoded, or from elementNS) a prefixed name must be declared in scope (else use setAttrNS), and an xmlns / xmlns:p declaration is checked like one in a document and comes into scope.
xml.setAttrNS(node, uri, qname, value)xml.ValueThe element with the attribute (uri, local part of qname) added or updated. A namespaced attribute needs a prefix.
xml.setText(node, s)xml.ValueThe element with its children replaced by one text node.
xml.append(parent, child)xml.ValueThe parent with child (an xml.Value element) appended. Amortised O(1) along a chain of appends.

Text, attribute values and namespace URIs must hold only characters XML 1.0 allows: a C0 control other than tab, LF and CR, U+FFFE or U+FFFF is an error. No escape can write them, so this keeps encode's output well-formed.

jennifer
use xml;

def note as xml.Value init xml.element("note");
$note = xml.setAttr($note, "type", "info");
$note = xml.setText($note, "hi & bye");
def page as xml.Value init xml.append(xml.element("page"), $note);
# <page><note type="info">hi &amp; bye</note></page>

Namespaces

Namespaces in XML name an element or attribute by a namespace URI plus a local name; the prefix a document uses is only a local shorthand. <kml xmlns="http://www.opengis.net/kml/2.2"> and <k:kml xmlns:k="http://www.opengis.net/kml/2.2"> hold the same elements, so code that matches on the written tag (xml.get($root, "Document")) reads one and misses the other. Match by namespace instead:

jennifer
use xml;

def const KML as string init "http://www.opengis.net/kml/2.2";
# The same path reads both forms; "g" is the program's prefix, not the document's.
def docs as list of xml.Value init xml.findAll($root, "g:Document/g:Placemark", {"g": KML});
  • Resolution. decode resolves every name against the xmlns / xmlns:p declarations in scope and keeps the result on the node, so a sub-tree from get / findAll / children still answers namespace and lookupPrefix. An unprefixed element takes the default namespace; an unprefixed attribute is in no namespace. xmlns="" undeclares the default. xml: is bound to http://www.w3.org/XML/1998/namespace without a declaration (xml.attrNS($el, "http://www.w3.org/XML/1998/namespace", "lang") reads xml:lang).
  • Strict. A prefix with no declaration in scope, binding the reserved xml / xmlns prefixes or their namespaces otherwise, xmlns:p="", a name with more than one :, and two attributes that resolve to the same (namespace, local name) are decode errors, with line and column.
  • Paths with a prefix map. get / findAll / has take an optional map of string to string from prefix to URI, as XPath and lxml do. With it, p:name matches elements in ns["p"] with local name name (a prefix not in the map is an error), and an unprefixed name matches the namespace under the "" key, or no namespace when "" is absent. Without a map, paths match the written names. *:name matches a local name in any namespace either way.
  • Fragments. A piece cut out of a larger document (<gpx:trkpt>... without the root's xmlns:gpx) has an undeclared prefix. Pass the root's bindings as namespaces: xml.decodeWith($frag, xml.DecodeOptions{namespaces: {"gpx": GPX}}).
  • Encoding. A decoded document keeps its own xmlns attributes, so it re-encodes with the same declarations. A sub-tree encoded on its own, and a tree built with elementNS / setAttrNS, gets an xmlns declaration exactly where an element needs a binding that is not in scope yet, so the output decodes to the same names. Elements built with xml.element are left as written.
  • Equality. Two elements with the same written name in different namespaces are not equal.

Notes and limits

  • convert.typeOf reports "object"; convert.objectType reports "xml.Value". A bare $v at the REPL (or %v) shows the compact XML.
  • Comments, PIs, the prolog, and the DOCTYPE are dropped on decode, so a decode / encode round-trip is faithful for the element tree but not for those. The one root element is required (XML has exactly one).
  • Richer XPath (predicates, @attr / text() path steps) is not supported; navigate to an element, then read attributes and text with the accessors.
  • Element nesting is capped on decode, encode and xml.append: a deeper document (or an append that would build one) raises a catchable error of kind "limit" instead of overflowing the stack (so does the node cap). The limit is shared by every parser in the toolchain and is set per binary: 1000 levels on the default jennifer (Go's growable stack), 64 on jennifer-tiny (its fixed 4 MB stack). No entity is expanded beyond the five predefined names and numeric references, so there is no entity-expansion ("billion laughs") blow-up, and nothing is fetched externally; an unknown or external entity is an error.
  • Node count is capped on decode: a document with more than about two million elements, attributes and text runs (a quarter million on jennifer-tiny) raises a catchable error. xml.append applies the same cap to the expanded tree, a sub-tree appended several times counted at each place.
  • Lower caps per call. xml.decodeWith's maxDepth (element nesting) and maxNodes lower the two decode caps, as json.decodeWith does: 0 keeps the default; a value above it, or a negative one, is a "runtime" error naming the allowed range.
  • Memory: a decoded xml.Value is a compact element tree (name, ordered attributes, ordered children); names, and text and attribute values with no entity to decode, share the input string. A document takes from about its text size (text-heavy markup) up to roughly 15 times it (many small elements) in memory; each element and attribute also holds its namespace URI (a slice of the input as well). Copies of a handle, and the sub-trees get / findAll / children hand out, share the tree instead of copying it. A sub-tree handle therefore keeps the whole document's text in memory, while tag / text / attr / attrs return copies: to keep a few values of a large document, read them out instead of holding a node.

Errors

KindRaised for
"xml"malformed text in decode / decodeWith, with line and column: a syntax error, a mismatched or unclosed tag, a duplicate attribute, an unknown entity, an undeclared prefix, a character XML 1.0 does not allow, invalid UTF-8
"limit"a reached cap (above)
"runtime"misuse: a wrong argument, DecodeOptions out of range, a bad namespaces map, a name or text the program builds that XML does not allow, a malformed path, attr / get / lookupPrefix finding nothing

Deprecated

CallInsteadRemoved
xml.decode(s, ns)xml.decodeWith(s, xml.DecodeOptions{namespaces: ns})0.29.0

Lint reports the deprecated form as L301.

See also

  • json.md / toml.md - the sibling opaque-handle formats. The shape is alike (an opaque value read by path, non-mutating writes), but the paths differ: xml's follow XPath (name[k] is 1-based, a path is required, and append takes a parent, not a pointer), while json, toml and yaml use JSON Pointer (0-based).
  • cheatsheet.md - every builtin at a glance.

Academy talks

TalkWhat it shows
YAML, TOML, JSON and XMLone config in XML, where every value is a string