Skip to content

xml

Parses, edits, validates, and serializes XML element trees.

Types

Type Description
Attr XML attribute
Node XML element node

Functions

decode xml

Parses an XML document into a parentless element tree.

Parameters:

Name Type Description
xml Str XML document

Returns: Node, the root element.

Errors:

  • ValueError if the document is invalid, has no root element, or uses an unsupported entity.

Predefined entities and numeric character references are expanded into text and attribute values. Custom DTD entities are not resolved.

let doc = decode "<root><child>text</child></root>"
assert_eq $doc.tag "root"

encode node

Validates and serializes an XML tree.

Namespace declarations are generated from expanded names and namespace snapshots. Prefix spelling and declaration placement can differ from the input, but element and attribute namespace semantics are preserved.

Parameters:

Name Type Description
node Node|Str XML node or text

Returns: Str, the serialized XML.

Errors:

  • ValueError if the tree is invalid or cyclic.
let n = Node "greeting"
n.push "hello"
assert_eq (encode $n) "<greeting>hello</greeting>"

verify node

Checks an entire XML tree without serializing it.

Validation includes names, namespace bindings, attribute uniqueness by expanded name, child and attribute types, and cycles. A node can temporarily contain invalid data while it is being edited.

Parameters:

Name Type Description
node Node Root element

Returns: nil.

Errors:

  • ValueError if the tree is invalid or cyclic.
let doc = Node "root"
verify $doc