-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
- t_insert(buf, ">");
- for n, child in ipairs(t) do
- if child.name then
- self(child, buf, self, xml_escape);
- else
- t_insert(buf, (xml_escape(child)));
+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