-local parse_xml = (function()
- local ns_prefixes = {
- ["http://www.w3.org/XML/1998/namespace"] = "xml";
- };
- local ns_separator = "\1";
- local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
- return function(xml)
- local handler = {};
- local stanza = st.stanza("root");
- function handler:StartElement(tagname, attr)
- local curr_ns,name = tagname:match(ns_pattern);
- if name == "" then
- curr_ns, name = "", curr_ns;
- end
- if curr_ns ~= "" then
- attr.xmlns = curr_ns;
- end
- for i=1,#attr do
- local k = attr[i];
- attr[i] = nil;
- local ns, nm = k:match(ns_pattern);
- if nm ~= "" then
- ns = ns_prefixes[ns];
- if ns then
- attr[ns..":"..nm] = attr[k];
- attr[k] = nil;
- end
- end
- end
- stanza:tag(name, attr);
- end
- function handler:CharacterData(data)
- data = data:gsub("^%s*", ""):gsub("%s*$", "");
- stanza:text(data);
+local function create_string_string(str)
+ str = ("%q"):format(str);
+ str = str:gsub("{([^}]*)}", function(s)
+ return '"..(data["'..s..'"]or"").."';
+ end);
+ return str;
+end
+local function create_attr_string(attr, xmlns)
+ local str = '{';
+ for name,value in pairs(attr) do
+ if name ~= "xmlns" or value ~= xmlns then
+ str = str..("[%q]=%s;"):format(name, create_string_string(value));