Beautiful Elixir DSL for building XML documents.
XM turns Elixir syntax into Saxy simple-form XML nodes, then delegates escaping and encoding to Saxy. It is useful for feeds, sitemaps, service integrations, and any code that needs XML without string concatenation.
Supported syntax
- local calls become XML elements:
url do ... end,loc "..." - keyword arguments become attributes:
link href: "/feed.xml", rel: "self" tag/2builds dynamic or namespaced names that are awkward as atomsqname/2builds qualified names such as"media:thumbnail"xmlns/1andxmlns/2build namespace declaration attributesschema do ... enddeclares root namespaces and XSD locations- dotted namespace calls such as
media.thumbnailwork for declared prefixes XM.validate!/2validates XML against declared or explicit XSD filesconfig :xm, validate: truevalidates everydocument do ... endfor,if,unless, andcasework inside XML blockstext/1,comment/1, andcdata/1create explicit XML node kinds- remote calls, variables, operators, and normal expressions remain Elixir
Examples
import XM
document do
urlset xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do
url do
loc "https://example.com/"
lastmod Date.utc_today()
end
end
end
tree do
tag qname(:media, :thumbnail), [xmlns(:media, "https://example.com/media"), url: image_url]
enddocument/2 requires exactly one root element and returns a binary. Use
tree/1 with render_iodata/2 when you want iodata, or when building
fragments to embed in a larger document.
Summary
Functions
Build a CDATA node.
Build a comment node.
Build and encode an XML document as a binary.
Build an XML element node.
Normalize nested XML nodes and scalar content.
Build a qualified XML name, such as "media:thumbnail".
Encode XML nodes through Saxy and return a binary.
Encode XML nodes through Saxy and return iodata.
Build a text node.
Build XML nodes without encoding them.
Validate XML against XSD schema locations.
Build a default namespace declaration attribute.
Build a prefixed namespace declaration attribute.
Types
@type prolog() :: Saxy.Prolog.t() | keyword() | nil
@type xml_node() :: Saxy.XML.element() | Saxy.XML.cdata() | Saxy.XML.comment() | Saxy.XML.characters() | Saxy.XML.ref() | Saxy.XML.processing_instruction()
Functions
@spec cdata(term()) :: Saxy.XML.cdata()
Build a CDATA node.
@spec comment(term()) :: Saxy.XML.comment()
Build a comment node.
Build and encode an XML document as a binary.
The document macro reads config :xm, validate: true when the macro expands.
If enabled, the rendered document is passed through validate!/2. Validation
is intentionally global; there is no per-document validation option.
Build an XML element node.
Normalize nested XML nodes and scalar content.
Build a qualified XML name, such as "media:thumbnail".
Encode XML nodes through Saxy and return a binary.
Encode XML nodes through Saxy and return iodata.
Use this with tree/1 for iodata-first pipelines:
tree do
feed do
title "Hello"
end
end
|> XM.render_iodata()
@spec text(term()) :: Saxy.XML.characters()
Build a text node.
Build XML nodes without encoding them.
Validate XML against XSD schema locations.
By default, schema locations are read from parsed root attributes
(xsi:schemaLocation or xsi:noNamespaceSchemaLocation). Pass :schema or
:schemas to validate against explicit local XSD paths instead. Returns the
original XML binary on success and raises XM.Error on failure.
Build a default namespace declaration attribute.
Build a prefixed namespace declaration attribute.