2 local st = require "util.stanza";
3 local lxp = require "lxp";
4 local t_insert = table.insert;
5 local t_remove = table.remove;
9 local parse_xml = (function()
11 ["http://www.w3.org/XML/1998/namespace"] = "xml";
13 local ns_separator = "\1";
14 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
16 --luacheck: ignore 212/self
18 local stanza = st.stanza("root");
19 local namespaces = {};
21 function handler:StartNamespaceDecl(prefix, url)
23 t_insert(namespaces, url);
24 t_insert(prefixes, prefix);
27 function handler:EndNamespaceDecl(prefix)
29 -- we depend on each StartNamespaceDecl having a paired EndNamespaceDecl
34 function handler:StartElement(tagname, attr)
35 local curr_ns,name = tagname:match(ns_pattern);
37 curr_ns, name = "", curr_ns;
45 local ns, nm = k:match(ns_pattern);
49 attr[ns..":"..nm] = attr[k];
55 for i=1,#namespaces do
56 n[prefixes[i]] = namespaces[i];
58 stanza:tag(name, attr, n);
60 function handler:CharacterData(data)
63 function handler:EndElement()
66 local parser = lxp.new(handler, "\1");
67 local ok, err, line, col = parser:parse(xml);
68 if ok then ok, err, line, col = parser:parse(); end
71 return stanza.tags[1];
73 return ok, err.." (line "..line..", col "..col..")";