Expand description
The XML grammar plugin for the tabnas parsing engine.
Parses XML text into a tree of elements: attributes, mixed content,
namespaces, entities (the five predefined plus numeric character
references, custom entities, and DOCTYPE <!ENTITY> declarations),
CDATA sections, comments, processing instructions and DOCTYPE
declarations (including <!ATTLIST> attribute defaults and
xml:space / xml:lang inheritance).
let value = tabnas_xml::parse(r#"<greeting lang="en">Hi <b>world</b></greeting>"#)?;
assert_eq!(
value.to_string(),
r#"{"name":"greeting","localName":"greeting","attributes":{"lang":"en"},"children":["Hi ",{"name":"b","localName":"b","attributes":{},"children":["world"]}]}"#
);The plugin is layered on the relaxed-JSON grammar of
tabnas_jsonic, as the canonical plugin is layered on
@tabnas/jsonic: use(jsonic) first, then use(Xml). In the default
pure-XML mode the XML rules replace the JSON value rules; in embed
mode (embed: true) they sit beside them so an XML element can appear
wherever a jsonic value can.
TypeScript is canonical: ts/src/xml.ts defines behaviour, option
names and defaults, and xml-grammar.jsonic at the repository root
defines the rule chain, embedded below as JSON. The shared fixtures in
test/spec/*.tsv are the parity contract across TypeScript, Go and
Rust.
Structs§
- Translation
Part - One optional alchemy translation source and its explicit entry point.
- Translation
Parts - The package-local structural translation interface.
- XmlError
- The error a failed parse produces, re-exported so callers need not depend on the engine crate directly.
- XmlOptions
- Plugin options.
Defaultis the canonical default set: namespaces resolved, entities decoded, no custom entities, undeclared entities an error, unbound prefixes accepted, pure-XML mode.
Constants§
- GRAMMAR_
TEXT - The grammar, as the JSON that
xml-grammar.jsonicat the repository root parses to. The TypeScript plugin parses that file’s text with jsonic at load time; this crate carries the parsed form so it needs no jsonic parse of its own to start, andtests/xml_test.rsholds the two to the same value. - VERSION
- This crate’s version. It MUST equal
ts/package.json“version”: the release orchestrator rewrites both, andtests/version_test.rsfails the build if they drift. MirrorsVERSIONints/src/xml.tsandconst VERSIONingo/xml.go.
Functions§
- decode_
bom - Decode a byte sequence into text, transcoding from whichever of UTF-8, UTF-16 LE/BE or UTF-32 LE/BE its byte-order mark indicates. UTF-8 is the default when there is no mark, so BOM-less UTF-8 files with non-ASCII tag names round-trip. The mark itself is dropped.
- make
- Build an XML parser with the default options.
- make_
with - Build an XML parser with caller options: the jsonic grammar, then this
plugin, the
new Tabnas().use(jsonic).use(Xml, options)of the canonical plugin and thejsonic.Make()plusUseDefaultsof Go. - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject is what a host that translates reads: the shape XML is read as and written from (tree), the file that holds the render, and the sentences that say what the render does not keep and which trees it writes. The crate embeds its own copy,translate/manifest.json, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds the copy to the file. - parse
- Parse an XML document with the shared default parser.
- plugin
- The plugin form of
xml, forTabnas::use_plugin. Options are read from the plugin option bag withXmlOptions::from_value; installed this way the grammar is re-applied to derived instances, as every native plugin is. - render_
text - XML’s render,
alchemy/render.alc, the file the manifest’stranslate.rendernames: a library of alchemy definitions, with noexport, whose entry pointxml-renderwrites an element tree’s events, the shape this reader builds, as one XML document, and refuses any other tree. A host links it with its own program. The crate embeds its own copy,translate/render.alc, held to the file as the manifest’s is. - strip_
bom - Strip a leading U+FEFF from text that is already decoded: the string
arm of the canonical
decodeBOM. - translate
- Return XML’s immutable translation parts.
- xml
- Install the XML grammar on
parser, which should already carry the jsonic grammar (asmakearranges): the port of theXmlplugin function.