attributefunctionfunction attribute(n: XmlNode | undefined, name: string): string | undefinedAn attribute's value, by its name with or without a prefix.
XML as a typed tree of elements and text, written with every value escaped (no document is a template of values) and read with no outside package (commons 0001-formats FR-004). The reader takes elements, attributes in either quote, the five named entities and character references, CDATA, comments, processing instructions and the XML declaration, checks that every element is closed by its own name, and refuses a document type with an internal subset, so no entity is ever expanded (no "billion laughs", no external entity). Names are kept as written (dc:title); the finders match a name with or without its prefix. Taken from erga's lib/ts/xml, whose reader was @std/xml.
import { el, readXml, textOf, writeXml } from "./xml.ts";
const doc = writeXml(el("note", [el("to", ["Ada & Charles"])], [["lang", "en"]]));
// '<?xml version="1.0" encoding="UTF-8"?>\n<note lang="en"><to>Ada & Charles</to></note>'
const back = readXml(doc);
if (back.kind === "ok") textOf(back.value); // "Ada & Charles"attributefunctionfunction attribute(n: XmlNode | undefined, name: string): string | undefinedAn attribute's value, by its name with or without a prefix.
childfunctionfunction child(n: XmlNode, name: string): XmlElement | undefinedThe first child element named name.
childrenfunctionfunction children(n: XmlNode, name: string): readonly XmlElement[]The child elements of a node named name (with or without a prefix).
descendantsfunctionfunction descendants(n: XmlNode, name: string): readonly XmlElement[]Every element below a node named name, in document order.
elfunctionfunction el(name: string, children?: readonly XmlChild[], attributes?: readonly (readonly [string, string])[]): XmlElementAn element.
escapeXmlfunctionfunction escapeXml(s: string): stringA value as XML text or an attribute's value: &, <, >, " and ' escaped, and the characters XML 1.0 forbids removed.
formatXmlErrorfunctionfunction formatXmlError(e: XmlError): stringAn XML error as one line: 3:14: expected a name.
leaffunctionfunction leaf(name: string, value: string | number | boolean | undefined): XmlElement | undefinedAn element holding one value, or nothing where the value is absent.
localNamefunctionfunction localName(name: string): stringA name without its prefix: title for dc:title.
readXmlfunctionfunction readXml(text: string, options?: ReadXmlOptions): Result<XmlElement, XmlError>A document read: its root element; or where and why it is not XML this reader takes.
textOffunctionfunction textOf(n: XmlNode | undefined): stringAll the text a node holds.
writeNodefunctionfunction writeNode(n: XmlNode): stringA node as XML text, with no declaration.
writeXmlfunctionfunction writeXml(root: XmlElement): stringA whole document: the XML declaration, then its root.
ReadXmlOptionstypeno test imports ittype ReadXmlOptions = { readonly keepWhitespace?: boolean };How to read: whether text that is only white space between elements is kept (it is dropped by default).
XmlChildtypeno test imports ittype XmlChild = XmlNode | string | number | boolean | undefined;What an element may be given as a child: a node, or a value written as text; undefined gives nothing.
XmlElementtypetype XmlElement = {
readonly kind: "element";
readonly name: string;
readonly attributes: readonly (readonly [string, string])[];
readonly children: readonly XmlNode[];
};An element: its name as written, its attributes in order, and its children.
XmlErrortypeno test imports ittype XmlError = { readonly kind: "xml"; readonly line: number; readonly column: number; readonly message: string };Where a text stops being XML this reader takes, and why. Lines and columns count from 1.
XmlNodetypetype XmlNode = XmlElement | XmlText;A node of an XML document.
XmlTexttypeno test imports ittype XmlText = { readonly kind: "text"; readonly text: string };Text, its entities and references already replaced.