2 local st
= require
"util.stanza";
3 local lxp
= require
"lxp";
4 local t_insert
= table.insert
;
5 local t_remove
= table.remove;
10 local parse_xml
= (function()
12 ["http://www.w3.org/XML/1998/namespace"] = "xml";
14 local ns_separator
= "\1";
15 local ns_pattern
= "^([^"..ns_separator
.."]*)"..ns_separator
.."?(.*)$";
17 --luacheck: ignore 212/self
19 local stanza
= st
.stanza("root");
20 local namespaces
= {};
22 function handler
:StartNamespaceDecl(prefix
, url
)
24 t_insert(namespaces
, url
);
25 t_insert(prefixes
, prefix
);
28 function handler
:EndNamespaceDecl(prefix
)
30 -- we depend on each StartNamespaceDecl having a paired EndNamespaceDecl
35 function handler
:StartElement(tagname
, attr
)
36 local curr_ns
,name
= tagname
:match(ns_pattern
);
38 curr_ns
, name
= "", curr_ns
;
46 local ns
, nm
= k
:match(ns_pattern
);
50 attr
[ns
..":"..nm
] = attr
[k
];
56 for i
=1,#namespaces
do
57 n
[prefixes
[i]]
= namespaces
[i
];
59 stanza
:tag(name
, attr
, n
);
61 function handler
:CharacterData(data
)
64 function handler
:EndElement()
67 local parser
= lxp
.new(handler
, "\1");
68 local ok
, err
, line
, col
= parser
:parse(xml
);
69 if ok
then ok
, err
, line
, col
= parser
:parse(); end
72 return stanza
.tags
[1];
74 return ok
, err
.." (line "..line
..", col "..col
..")";