+local xml_escape
+do
+ local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
+ function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
+ _M.xml_escape = xml_escape;
+end
+
+local function _dostring(t, buf, self, xml_escape, parentns)
+ local nsid = 0;
+ local name = t.name
+ t_insert(buf, "<"..name);
+ for k, v in pairs(t.attr) do
+ if s_find(k, "\1", 1, true) then
+ local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
+ nsid = nsid + 1;
+ t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
+ elseif not(k == "xmlns" and v == parentns) then
+ t_insert(buf, " "..k.."='"..xml_escape(v).."'");
+ end
+ end
+ local len = #t;
+ if len == 0 then
+ t_insert(buf, "/>");
+ else
+ t_insert(buf, ">");
+ for n=1,len do
+ local child = t[n];
+ if child.name then
+ self(child, buf, self, xml_escape, t.attr.xmlns);
+ else
+ t_insert(buf, xml_escape(child));
+ end
+ end
+ t_insert(buf, "</"..name..">");
+ end
+end