import('data-xml') provides XML parsing and serialization with support for elements, attributes, text, CDATA sections, comments, and self-closing tags.
xml := import('data-xml')
// Parse XML
nodes := xml.parse('<root attr="val"><item>Hello</item><item>World</item></root>')
// nodes.0.tag => 'root'
// nodes.0.attrs.attr => 'val'
// Build and serialize XML
doc := xml.element('root', {version: '1.0'}, [
xml.element('item', {}, [xml.text('Hello')])
xml.element('item', {}, [xml.text('World')])
xml.comment('end of list')
])
text := xml.serialize(doc)
// Query elements
first := xml.querySelector(nodes, 'item')
all := xml.querySelectorAll(nodes, 'item')
content := xml.textContent(nodes.0)
Creates an element node: {type: :element, tag, attrs, children}.
Creates a text node: {type: :text, content}.
Creates a CDATA section node: {type: :cdata, content}.
Creates a comment node: {type: :comment, content}.
Converts an XML AST node (or list of nodes) to an XML string. Handles attribute escaping and entity encoding (&, <, >, ", ').
Parses XML text into a list of AST nodes. Supports:
- Elements with attributes
- Self-closing tags (
<tag/>) - CDATA sections (
<![CDATA[...]]>) - Comments (
<!--...-->) - Processing instructions (
<?...?>) - Text nodes with entity unescaping
Finds the first element matching a tag name (depth-first search).
Finds all elements matching a tag name.
Extracts all text content from a node and its children recursively.
- Entity references
&,<,>,",'are handled automatically. - Attribute values support both
"and'delimiters. - No namespace or DTD support.