@einvoice-ts/parser
Namespace-aware, schema-driven UBL XML parser for Peppol BIS Billing 3.0 documents: XML in,
Unchecked* model plus structural diagnostics out. Depends on @einvoice-ts/core and
@rgrove/parse-xml (the only third-party runtime dependency in the monorepo).
Part of einvoice-ts. To parse and validate in one call, use validateXml() from
einvoice-ts. Guide: parsing, validating and
serializing.
Install
pnpm add @einvoice-ts/parser
Parsing
import { parse, parseCreditNote, parseInvoice } from '@einvoice-ts/parser';
const { diagnostics, document, ok } = parse(xml);
document.kind; // 'Invoice' | 'CreditNote'
ok; // no diagnostic is fatal
parse() reads the document element to decide which kind it is; parseInvoice() and
parseCreditNote() narrow the result and throw a DocumentKindError when the document is the
other one. The model is always returned, however wrong the document is, so that one parse plus one
validation report every problem at once.
Three things are thrown rather than reported, because they are about the call and not about the
document's contents:
| Error |
When |
XmlSyntaxError |
the text is not well-formed XML, or nests deeper than 1,000 levels |
UnsupportedDocumentError |
the document element is neither a UBL Invoice nor a UBL CreditNote |
DocumentKindError |
parseInvoice() was given a credit note, or the other way round |
Documents are matched on namespace URIs, never on prefixes: a document that writes n1: for
the aggregate namespace and makes Invoice-2 the default parses into exactly the same model as one
that writes cac:. A byte order mark is accepted and does not shift the reported positions. No
document type declaration is ever expanded: @rgrove/parse-xml discards it and a reference to an
entity it defines is a syntax error, so an XXE payload never reaches the file system.
Elements may nest at most 1,000 levels deep, the document element being the first and an empty
element counting at its own level. A UBL invoice nests fewer than 20. A deeper document is an
XmlSyntaxError pointing at the start tag that goes past the limit: @rgrove/parse-xml, which
recurses once per element, stops there before it could overflow the call stack. Hostile input
therefore never escapes parse() as a RangeError.
Structural diagnostics
Every diagnostic is an Issue of @einvoice-ts/core with source: 'structure' and carries the
model path, the pathString, the xpath and the location (offset, 1-based line and
column) of what it is about.
The EN 16931 UBL syntax rules that only the XML can show are reported under their own ids, with
the specification's wording and severity, from the tables generated out of the Schematron:
| Family |
What it is about |
UBL-CR-001..682 |
an element or an attribute the Peppol subset does not use |
UBL-SR-01..55 |
an element that appears more often than it may |
UBL-DT-08..28 |
an attribute forbidden wherever it appears |
What no rule covers is reported under this library's own codes:
| Code |
Severity |
When |
EINV-XML-UNKNOWN-ELEMENT |
fatal |
an element the syntax does not know and no rule names |
EINV-XML-UNKNOWN-ATTRIBUTE |
warning |
likewise for an attribute; its value is in data |
EINV-XML-CARDINALITY |
fatal |
too many of an element no UBL-SR rule caps |
EINV-XML-ORDER |
fatal |
a sibling out of the XSD sequence |
EINV-XML-MIXED-CONTENT |
fatal |
text inside an aggregate |
EINV-XML-EMPTY |
warning |
an element with no children and no text (R008) |
import { parseInvoice } from '@einvoice-ts/parser';
const { diagnostics } = parseInvoice(xml);
for (const issue of diagnostics) {
console.error(`${issue.id} at ${issue.xpath} (line ${issue.location?.line}): ${issue.message}`);
}
Leaf text is kept verbatim - no trimming, no collapsing - because the rules apply
normalize-space() exactly where the Schematron does. An element that carries nothing becomes the
empty string and an EINV-XML-EMPTY warning, which the validator unions with its own model walk
for PEPPOL-EN16931-R008.
Also exported
The structural tables generated from the Schematron - UBL_CR_TABLE, UBL_CR_ATTRIBUTE_TABLE,
UBL_SR_TABLE, UBL_DT_ATTRIBUTE_TABLE, UBL_RULE_TABLE, UBL_SR_BESPOKE_IDS and
UBL_DT_BESPOKE_IDS - so that a rule-coverage report can tell which UBL-* rule is answered
where. Everything else (the namespace stack, the locator, the table matchers) is an implementation
detail of the walk and is not part of the public surface.
License
MIT. Some generated files reproduce specification text from OpenPeppol, CEN (EUPL-1.2) and OASIS; see
NOTICE.