Documentation / @einvoice-ts/parser / ParseResult
Interface: ParseResult<T> ​
Defined in: parse.ts:48
What a parse produced: the model, everything wrong with the document's structure, whether any of it is fatal, and a way to find an element of the source by its XPath.
document is always present. A document with fatal diagnostics is still mapped as far as it could be, which is what makes a parse plus a validation report every problem at once instead of stopping at the first.
Example ​
import { parseInvoice } from '@einvoice-ts/parser';
const result: ParseResult<UncheckedInvoice> = parseInvoice(xml);
if (!result.ok) console.error(result.diagnostics.map((issue) => issue.id));Type Parameters ​
T ​
T
Properties ​
diagnostics ​
readonlydiagnostics: readonlyIssue[]
Defined in: parse.ts:50
Every structural problem found, in document order.
document ​
readonlydocument:T
Defined in: parse.ts:52
The model, mapped as far as the document allowed.
locate ​
readonlylocate: (xpath) =>IssueLocation|undefined
Defined in: parse.ts:83
Where in the source XML the element an XPath names starts, as the diagnostics' location gives it: the line and column of its start tag.
The XPath is spelled as the parser's diagnostics and the validator's issues spell it: /ubl:Invoice/cac:InvoiceLine[2]/cbc:ID, with the canonical ubl, cac and cbc prefixes whatever prefixes the document uses. A step without a position is the first occurrence, as [1] is (cac:InvoiceLine/cbc:ID is cac:InvoiceLine[1]/cbc:ID), and an XPath that ends in an attribute (cbc:PayableAmount/@currencyID) names the element that carries it. Elements the model dropped can be found too, at the XPath their diagnostic reports (/ubl:Invoice/cbc:DueDate[2]). An element from another namespace written with a UBL prefix (<cbc:Note xmlns:cbc="urn:x"/>) shares its XPath with the UBL element of that name, so it locates to the element the model holds.
The index behind it is built on the first call, so a parse that never calls it does not pay for it.
Parameters ​
xpath ​
string
The XPath of an element or of one of its attributes.
Returns ​
IssueLocation | undefined
The position, or undefined when no element of the document has that XPath.
Example ​
import { parse, validate } from 'einvoice-ts';
const { document, locate } = parse(xml);
for (const issue of validate(document).fatal) {
const where = locate(issue.xpath); // { column: 9, line: 144, offset: 6552 }
console.log(`${issue.id} at line ${String(where?.line)}`);
}ok ​
readonlyok:boolean
Defined in: parse.ts:85
Whether no diagnostic is fatal.