Handling electronic invoices at scale—whether Peppol UBL 2.1, EN 16931, or local tax authority XML schemas—usually means uploading sensitive financial documents to a third-party backend. Every vendor uses slightly different XML namespaces (cbc:, cac:, ns2:, or unprefixed tags), making brittle XPath queries break on real-world supplier files.
Here is the architecture behind our 100% client-side XML E-Invoice & Peppol UBL parser and auditor, built with Vanilla ES Modules and deployed at the edge on Cloudflare Workers—with zero server uploads.
1. Namespace-Agnostic Tag Extraction
Instead of hardcoding strict XML namespace prefixes, we normalize extraction across arbitrary vendor prefixes using a lightweight namespace-agnostic pattern combined with DOMParser fallback:
const tagPattern = (tagName) => new RegExp(`<(?:[a-zA-Z0-9_]+:)?${tagName}\\b[^>]*>([\\s\\S]*?)</(?:[a-zA-Z0-9_]+:)?${tagName}>`, 'i'); export function extractTag(xml, tagName) { const match = xml.match(tagPattern(tagName)); return match ? match[1].trim() : ''; } This cleanly resolves fields like InvoiceNumber / ID, IssueDate, SupplierTaxID, TaxExclusiveAmount, TaxAmount, and PayableAmount across both Peppol UBL 2.1 documents and regional e-invoice XML formats without shipping megabytes of XML dependencies.
2. Deterministic 5-Rule Tax Math & Duplicate Auditing
Once parsed in memory, every batch runs through a 5-rule verification pipeline before export:
- Line-Item Sum Check: Verifies
sum(lineNetAmounts)matches headerTaxExclusiveAmountwithin rounding tolerance. - VAT Rate Consistency: Checks
TaxAmount / TaxExclusiveAmountagainst valid statutory brackets (0%, 5%, 8%, 10%, or custom EU/VAT rates). - Grand Total Reconciliation: Validates
TaxExclusiveAmount + TaxAmount === PayableAmount. - Duplicate Invoice Fingerprinting: Flags collisions on
(SupplierTaxID + InvoiceSeries + InvoiceNumber)across multi-file drops. - Required Tax Metadata Guard: Catches missing buyer/supplier tax IDs or malformed ISO dates before ERP import.
3. UTF-8 BOM CSV Export for Excel
A common pitfall with browser CSV exports is garbled UTF-8 characters when accountants double-click the file in Microsoft Excel. Prepending the UTF-8 Byte Order Mark (\uFEFF) and escaping RFC 4180 quotes guarantees instant, clean spreadsheet imports across Windows and macOS.
Live Demos & Full Source Bundle
- Live InvoiceXML & Profit Reconciler Demo: https://ketoan-pro.tranhoangtu-it.workers.dev
- Live Agent & Revenue Vault Demo: https://agent-vault.tranhoangtu-it.workers.dev
- Full Source Code & Offline Enterprise Bundle on Gumroad: https://tranhoangtu.gumroad.com/l/kihth
All parsing happens strictly inside your browser tab—sensitive supplier prices, tax IDs, and margins never leave your device.
