xml

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package xml is the "xml" host module. See std/encoding/register.go for why this and its eight siblings each get their own leaf package instead of living in std's flat root, and how this directory's Module reaches the rest of magus without std importing back down to collect it.

Index

Constants

This section is empty.

Variables

View Source
var Module = std.Module{
	Name: "xml",
	WASM: true,
	Path: "encoding/xml",
	Doc:  "Build, serialize, and parse XML/SVG.",
	Methods: []std.Method{
		{
			Name:    "render",
			Doc:     "Serialize an XML node to a string. A node is a string (text) or an element map {\"tag\": name, \"attrs\": [name, value, ...], \"children\": [node, ...]}. Empty-children elements self-close; no whitespace is emitted between tags.",
			Args:    []std.Arg{{Name: "node", Type: std.TypeAny}},
			Returns: []std.Ret{{Type: std.TypeString}},
			Raises:  true,
			Impl:    XMLRender,
		},
		{
			Name: "element",
			Doc:  "Build an element node from a tag, a flat [name, value, ...] attribute list, and a list of child nodes (elements or strings). Sugar for the {\"tag\", \"attrs\", \"children\"} map that render consumes.",
			Args: []std.Arg{
				{Name: "tag", Type: std.TypeString},
				{Name: "attrs", Type: std.TypeStringSlice},
				{Name: "children", Type: std.TypeAny},
			},
			Returns: []std.Ret{{Type: std.TypeAny}},
			Raises:  true,
			Impl:    XMLElement,
		},
		{
			Name:    "parse",
			Doc:     "Parse an XML string into a node tree: each element becomes {\"tag\": name, \"attrs\": [name, value, ...], \"children\": [node, ...]}, character data becomes a string. The inverse shape of render.",
			Args:    []std.Arg{{Name: "s", Type: std.TypeString}},
			Returns: []std.Ret{{Type: std.TypeAny}},
			Raises:  true,
			Impl:    XMLParse,
		},
	},
}

Module is the "xml" host module: build and serialize an XML/SVG tree, and parse one back. It is the markup counterpart to the json module - render is to stringify what parse is to json.parse.

A NODE is either a string (character data) or an ELEMENT: a map with "tag" (the element name), "attrs" (a FLAT list of alternating name, value strings), and "children" (a list of nodes). Attributes are a list, not a map, on purpose: a map crossing the VM boundary loses order, and attribute order is significant when the output must be byte-for-byte stable (e.g. a committed badge SVG). An element with no children self-closes (<rect .../>); otherwise it wraps its children (<g ...>...</g>). render emits no whitespace between tags, so the caller controls the exact bytes.

Functions

func Modules

func Modules() []std.Module

Modules returns this directory's contribution to the encoding module set. See std/encoding/register.go: it aggregates every leaf's Modules() into one slice, which is how a module here reaches std.All()'s callers without std importing this package to collect it.

func XMLElement

func XMLElement(_ context.Context, tag string, attrs []string, children any) (any, error)

XMLElement builds an element node. attrs is a flat [name, value, ...] list; children is a list of nodes (elements or strings). It is a constructor for the map shape render serializes, so an author writes xml.element(...) instead of a bare map literal.

func XMLParse

func XMLParse(_ context.Context, s string) (any, error)

XMLParse parses an XML string into the same node shape render consumes.

func XMLRender

func XMLRender(_ context.Context, node any) (string, error)

XMLRender serializes an XML node tree to a string (see the module doc for the shape).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL