Skip to content

xml

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

Types

TypeDescription
Attr An XML attribute.
Node A parentless XML element.

Functions

decode xml -> Node

Parses an XML document into a parentless element tree.

Predefined entities and numeric character references are expanded. Custom DTD entities are not resolved.

Parameters

NameTypeDescription
xml Str XML document.

Returns

The root element.

Errors

ValueError if the document is invalid, lacks a root, or uses an unsupported entity.

Example

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

encode node -> Str

Validates and serializes an XML tree.

Namespace declaration spelling and placement can differ from the input while preserving element and attribute namespace semantics.

Parameters

NameTypeDescription
node (Node | Str) XML node or text.

Errors

ValueError if the tree is invalid or cyclic.

Example

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 covers names, namespace bindings, attribute uniqueness, child and attribute types, and cycles.

Parameters

NameTypeDescription
node Node Root element.

Errors

ValueError if the tree is invalid or cyclic.

Example

let doc = Node "root"
verify $doc