- local xml_entities = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
- function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end
-end
-
-local xml_escape = xml_escape;
-
-local function dostring(t, buf, self, xml_escape)
- local nsid, ns, attrk = 0;
- t_insert(buf, "<");
- t_insert(buf, t.name);
- for k, v in pairs(t.attr) do if type(k) == "string" then
- t_insert(buf, " ");
- ns, attrk = s_match(k, "^([^|]+)|(.+)$");
- if ns then
- nsid = (nsid or -1) + 1;
- t_insert(buf, "xmlns:ns"..nsid);
- t_insert(buf, "='");
- t_insert(buf, (xml_escape(tostring(ns))));
- t_insert(buf, "' ");
- t_insert(buf, "ns"..nsid..":"..attrk);
- else
- t_insert(buf, k);
+ 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).."'");