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
| Name | Type | Description |
|---|---|---|
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
Attrobjects 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
NodeandStrvalues in document order.Iterating a node iterates this view directly.
namespaceMutable namespace URI, or
nilfor no namespace.namespacesMutable 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.prefixMutable preferred prefix, or
nil.qnameRead-only qualified name formed from
prefixandtag.tagMutable local element name.
Methods
(assign) key value
Sets an unnamespaced attribute, adding it if it is missing.
Parameters
| Name | Type | Description |
|---|---|---|
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
| Name | Type | Description |
|---|---|---|
name |
Str |
Local attribute name. |
:namespace? |
Str |
Namespace URI. |
:default? |
D |
Value returned when absent. |
:else? |
(() -> D) |
Function evaluated when absent. |
Example
delete_attr name … -> Bool
Deletes all attributes matching an expanded name.
Parameters
| Name | Type | Description |
|---|---|---|
name |
Str |
Local attribute name. |
:namespace? |
Str |
Namespace URI. |
Returns
Whether an attribute was deleted.
push child
Appends a child.
Parameters
| Name | Type | Description |
|---|---|---|
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
| Name | Type | Description |
|---|---|---|
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.