Functions
at core
fn (xml: Xml, segments: Vec): Xml?
Navigate through nested XML using tag names as path segments. Returns null if any segment in the path is not found.
Example
xml from-xml("<root><user><name>Alice</name></user></root>")
name at(xml, ["user", "name"])
text(name) // "Alice"
// Or chain text() directly
text(at(xml, ["user", "name"])) // "Alice"
attr core
fn (xml: Xml, name: Str): Str?
Get an attribute value by name. Returns null if not found.
Example
xml from-xml("<person id='123' status='active'/>")
attr(xml, "id") // "123"
attr(xml, "status") // "active"
attr(xml, "missing") // null
child core
fn (xml: Xml, tag: Str): Xml?
Get the first child element with the matching tag name. Returns null if no matching child is found.
Example
xml from-xml("<root><item>A</item><item>B</item></root>")
item child(xml, "item")
item.tag // "item"
text(item) // "A"
children core
fn (xml: Xml, tag: Str): Vec
Get all child elements with the matching tag name.
Example
xml from-xml("<root><item>A</item><item>B</item><other>C</other></root>")
items children(xml, "item")
length(items) // 2
from-xml core
fn (value: Str): Xml
Parse an XML string into an Xml structure.
Example
xml from-xml("<person id='123'><name>Alice</name></person>")
xml.tag // "person"
xml.attrs // {"id": "123"}
text core
fn (xml: Xml): Str
Get the concatenated text content of an element.
Example
xml from-xml("<name>Alice <b>Smith</b></name>")
text(xml) // "Alice Smith"
to-xml core
fn (xml: Xml): Str
Serialize an Xml structure to an XML string.
Example
xml Xml({
"tag": "person",
"attrs": {"id": "123"},
"content": ["Alice"]
})
to-xml(xml) // "<person id=\"123\">Alice</person>"
Types
Xml
Xml type {
tag: Str,
attrs: Map,
content: Vec
}
Represents an XML element with tag, attributes, and content.
Fields
tag: The element tag name (Str)attrs: Attribute map (Map<Str, Str>)content: Child elements and text nodes (Vec)
Example
// Create an XML element manually
elem Xml({
"tag": "person",
"attrs": {"id": "123"},
"content": [
Xml({"tag": "name", "attrs": {}, "content": ["Alice"]}),
Xml({"tag": "age", "attrs": {}, "content": ["30"]})
]
})