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