Node
Represents an XML element node.
Constructor
Calling Node as a function creates a new element node:
Parameters:
| Name | Type | Description |
|---|---|---|
tag |
str |
The tag name |
Returns: Node -- A new empty node
Fields
| Field | Type | Description |
|---|---|---|
tag |
str |
The element's tag name |
The tag field can be read and written:
Indexing
Nodes support attribute access via indexing:
Methods
attrs()
Returns an iterator over the node's attributes, yielding [key, val] pairs.
Returns: An iterator suitable for use in for loops and dict
comprehensions.
let el = from_str r#"<foo x="1" y="2"/>"#
for k v = el.attrs()
echo "$k = $v"
# Or use with dict comprehension
let attrs = {...el.attrs()}
children()
Returns an input iterator over the node's children.
Returns: An input iterator yielding child nodes (which may be Node or
str for text).
traverse()
Returns a depth-first, parent-first iterator over the node and all its descendants.
Returns: An input iterator yielding each node in the tree in document
order. Each yielded value is either an Node (element) or a str (text
content). The root node itself is the first value yielded.
let doc = from_str "<a><b><c/></b><d/></a>"
for n = doc.traverse()
if (type n Node)
echo $n.tag
# prints: a, b, c, d
To collect all element nodes into an array:
push child
Appends a child to the node.
Parameters:
| Name | Type | Description |
|---|---|---|
child |
Node or str |
The child to add |