-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
75 lines (62 loc) · 1.84 KB
/
parser.go
File metadata and controls
75 lines (62 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package einvoice
import (
"fmt"
"io"
"os"
"github.com/shopspring/decimal"
"github.com/speedata/cxpath"
)
// getDecimal parses a decimal value from an XPath evaluation result.
// Shared by both CII and UBL parsers.
func getDecimal(ctx *cxpath.Context, eval string) (decimal.Decimal, error) {
a := ctx.Eval(eval).String()
if a == "" {
return decimal.Zero, nil
}
str, err := decimal.NewFromString(a)
if err != nil {
return decimal.Zero, fmt.Errorf("invalid decimal value '%s' at %s: %w", a, eval, err)
}
return str, nil
}
// ParseReader reads the XML from the reader and auto-detects the format (CII or UBL).
// It detects the format by examining the root element namespace and routes to the
// appropriate parser. Each parser handles its own namespace setup.
func ParseReader(r io.Reader) (*Invoice, error) {
ctx, err := cxpath.NewFromReader(r)
if err != nil {
return nil, fmt.Errorf("cannot read from reader: %w", err)
}
// Detect format by checking root element namespace
root := ctx.Root()
rootns := root.Eval("namespace-uri()").String()
var inv *Invoice
switch rootns {
case "":
return nil, fmt.Errorf("empty root element namespace")
// CII format (ZUGFeRD/Factur-X)
case nsCIIRootInvoice:
inv, err = parseCII(ctx)
if err != nil {
return nil, fmt.Errorf("parse CII: %w", err)
}
// UBL format (Invoice or CreditNote)
case nsUBLInvoice, nsUBLCreditNote:
inv, err = parseUBL(ctx)
if err != nil {
return nil, fmt.Errorf("parse UBL: %w", err)
}
default:
return nil, fmt.Errorf("unknown root element namespace: %s", rootns)
}
return inv, nil
}
// ParseXMLFile reads the XML file at filename.
func ParseXMLFile(filename string) (*Invoice, error) {
r, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("einvoice: cannot open file (%w)", err)
}
defer func() { _ = r.Close() }()
return ParseReader(r)
}