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§
- 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. - 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. - strip_
bom - Strip a leading U+FEFF from text that is already decoded: the string
arm of the canonical
decodeBOM. - xml
- Install the XML grammar on
parser, which should already carry the jsonic grammar (asmakearranges): the port of theXmlplugin function.