Skip to content

Node

A parentless XML element.

Nodes can be shared or inserted into different trees. Serialization derives namespace declarations from each node rather than from parent pointers.

Implements: std.Index[{...Str: Str, ...Int: Node | Str, ...Range[Int]: Array[Node | Str]}], std.Assign[Str, Str]

Constructor

Node tag *items …

Creates an element, optionally with attributes and children.

Children are not checked here, matching children.push; verify and serialization reject children other than Node and Str. attrs: is applied before positional Attr values.

Parameters

NameTypeDescription
tag Str Local element name.
:namespace? Str Namespace URI.
:prefix? Str Preferred namespace prefix.
:attrs? Dict[Sym | Str, Str] Unnamespaced attributes with Sym or Str keys and Str values.
*items (Attr | Value) Attributes or children.

Errors

TypeError if attrs: is not a dict or one of its values is not a Str.

Example

let item = Node "item" namespace: "urn:inventory" prefix: "inv"
let disk = Node disk
  attrs:
    type: file
    device: disk
  - $ Node driver attrs: {name: "qemu", type: "qcow2"}

Fields

attrs @ std.MutSeq[Attr]

Mutable array-like view of Attr objects in document order.

The view supports indexed assignment and array mutation methods. Duplicate expanded names can exist temporarily, but verification rejects them.

children @ std.MutSeq[Node | Str]

Mutable array-like view of child Node and Str values in document order.

Iterating a node iterates this view directly.

namespace

Mutable namespace URI, or nil for no namespace.

namespaces

Mutable dict containing the complete effective namespace snapshot.

Keys are prefix strings; "" is the default namespace. Parsed descendants retain inherited bindings so they can be detached and serialized.

prefix

Mutable preferred prefix, or nil.

qname

Read-only qualified name formed from prefix and tag.

tag

Mutable local element name.

Methods

(assign) key value

Sets an unnamespaced attribute, adding it if it is missing.

Parameters

NameTypeDescription
key Str
value Str

(index) key

Reads an unnamespaced attribute by string or a child by integer index.

Negative integer indexes count from the end.

(iter)()

Iterates the children.

attr[D = nil] name … -> (Str | D)

Gets the first attribute matching an expanded name.

Parameters

NameTypeDescription
name Str Local attribute name.
:namespace? Str Namespace URI.
:default? D Value returned when absent.
:else? (() -> D) Function evaluated when absent.

Example

let id = node.attr "id" namespace: "urn:inventory"

delete_attr name … -> Bool

Deletes all attributes matching an expanded name.

Parameters

NameTypeDescription
name Str Local attribute name.
:namespace? Str Namespace URI.

Returns

Whether an attribute was deleted.

push child

Appends a child.

Parameters

NameTypeDescription
child (Node | Str) Child value.

set_attr name value …

Updates the first matching attribute or appends one.

An omitted prefix: mutates the existing value. An explicit prefix, including nil, replaces the attribute because identity fields are immutable.

Parameters

NameTypeDescription
name Str Local attribute name.
value Str Attribute value.
:namespace? Str Namespace URI.
:prefix? Str Preferred namespace prefix.

traverse() -> Iter[Node | Str]

Returns a depth-first, parent-first iterator over this node and descendants.