xml writes XML for the files an app serves to others: a sitemap, an Atom or RSS feed, an OPML list. You build a tree of elements, attributes and texts, and Xml.to_s writes it. It is written in Agentica, with no C.

It writes well-formed XML 1.0, or fails. Every text and attribute value is escaped, so what a user typed cannot become markup, and a name or a character that XML 1.0 does not allow is an error, so no app writes its own checker. Namespaces are not checked: a prefix with no xmlns attribute is written as it is, and a namespace-aware reader will refuse it. This is the writer; a reader is planned.

Install #

Add it in a project with an agentica.toml, with its prefixed tag and its folder as --subdir:

agentica add xml agentica-lang/packages --tag xml-0.2.0 --subdir xml

That writes this line of agentica.toml, and the entry of agentica.lock:

[dependencies]
xml = { git = "https://github.com/agentica-lang/packages", tag = "xml-0.2.0", subdir = "xml" }

It needs Agentica 0.40.0 or later.

A sitemap #

import Xml from "pkg/xml/xml"

def page(url: String, modified: String) -> Xml.Node
  Xml.element("url", [], [
    Xml.element("loc", [], [Xml.text(url)]),
    Xml.element("lastmod", [], [Xml.text(modified)]),
  ])
end

def main() -> Void
  sitemap = Xml.Element(
    name: "urlset",
    attributes: [Xml.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")],
    children: [
      page("https://example.com/", "2026-11-30"),
      page("https://example.com/search?q=a&b=c", "2026-11-29"),
    ],
  )
  case Xml.to_s(sitemap)
    ok(text) -> puts(text)
    error(e) -> IO.warn(e.message)
  end
end
Output
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/</loc><lastmod>2026-11-30</lastmod></url><url><loc>https://example.com/search?q=a&amp;b=c</loc><lastmod>2026-11-29</lastmod></url></urlset>

An Atom feed #

A prefix and its xmlns attribute are an attribute name and a value like any other: write both, and nothing is resolved.

import Xml from "pkg/xml/xml"

def entry(title: String, link: String) -> Xml.Node
  Xml.element("entry", [], [
    Xml.element("title", [Xml.attr("type", "html")], [Xml.text(title)]),
    Xml.element("link", [Xml.attr("href", link), Xml.attr("rel", "alternate")]),
    Xml.element("updated", [], [Xml.text("2026-11-30T12:00:00Z")]),
  ])
end

def main() -> Void
  feed = Xml.Element(
    name: "feed",
    attributes: [Xml.attr("xmlns", "http://www.w3.org/2005/Atom"), Xml.attr("xml:lang", "en")],
    children: [
      Xml.element("title", [], [Xml.text("Fish & chips")]),
      entry("Cod <b>today</b>", "https://example.com/posts/1?a=1&b=2"),
    ],
  )
  case Xml.to_s(feed, declaration: false)
    ok(text) -> puts(text)
    error(e) -> IO.warn(e.message)
  end
end
Output
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title>Fish &amp; chips</title><entry><title type="html">Cod &lt;b&gt;today&lt;/b&gt;</title><link href="https://example.com/posts/1?a=1&amp;b=2" rel="alternate"/><updated>2026-11-30T12:00:00Z</updated></entry></feed>

What is written #

  • The text is written as it is built: no space or line break is added, so a text node is never changed by a pretty printer. Add a Xml.text("\n") where you want one.
  • &, <, >, " and ' are escaped in every text and attribute value, so a text that looks like markup, ]]> or a comment stays text. A carriage return in a text, and a tab, a line feed or a carriage return in an attribute value, are written as character references, because a reader would turn them into other characters.
  • An element with no children is <name/>; one with a text node, even an empty one, has its end tag.
  • The text is UTF-8, and the declaration says so. The declaration is <?xml version="1.0" encoding="UTF-8"?>, followed by a line feed.
  • The tree is written with a stack, not with calls, so a tree of any depth is written.

What is refused #

Xml.to_s returns an error, and writes nothing, for:

  • a name that is not an XML name, for an element or an attribute: the first character a letter or _, then letters, digits, ., - and the other characters XML 1.0 allows in a name, with at most one : that has a part on each side (kind xml.invalid_name);
  • a character that XML 1.0 does not allow in a text or in an attribute value: the control characters from U+0000 to U+001F other than tab, line feed and carriage return, and U+FFFE and U+FFFF (kind xml.invalid_char; the message names the character and the element). A lone surrogate cannot be in a String, so it never gets here;
  • an attribute written twice on one element (kind xml.duplicate_attribute).

The names xml and xmlns and their prefixes are not checked: the namespace rules are the caller's.

API #

Xml.to_s(root, declaration = true) #

Xml.to_s(root: Xml.Element, declaration: Bool = true) -> Result[String]

The XML of root, after the XML declaration unless declaration is false. It fails with xml.invalid_name, xml.invalid_char or xml.duplicate_attribute, at the first problem in document order.

import Xml from "pkg/xml/xml"

def main() -> Void
  case Xml.to_s(Xml.Element(name: "a", children: [Xml.text("x#{String.from_code(1)}")]))
    ok(text) -> puts(text)
    error(e) -> puts(e.kind)
  end
end
Output
xml.invalid_char

Xml.Element #

struct Xml.Element
  name: String
  attributes: List[Xml.Attr] = []
  children: List[Xml.Node] = []
end

An element: its name, its attributes in the order they are written, and its children in order. The root of Xml.to_s is an Element; a child is a Node.

Xml.Node #

enum Xml.Node
  Element(Xml.Element)
  Text(String)
end

A child of an element: an element or a text. Xml.element and Xml.text build one.

Xml.Attr #

struct Xml.Attr
  name: String
  value: String
end

An attribute. Xml.attr builds one.

Xml.element(name, attributes = [], children = []) #

Xml.element(name: String, attributes: List[Xml.Attr] = [], children: List[Xml.Node] = []) -> Xml.Node

An element as a child of another: Xml.Node.Element(Xml.Element(...)). It returns a Node; the root of Xml.to_s is built with Xml.Element(...).

Xml.text(value) #

Xml.text(value: String) -> Xml.Node

A text node. It is escaped when written.

Xml.attr(name, value) #

Xml.attr(name: String, value: String) -> Xml.Attr

An attribute, with the value as text: it is escaped when written.