diff options
-rw-r--r-- | lib/xmllib.lua | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/xmllib.lua b/lib/xmllib.lua index 4ca86c7..7bfe36a 100644 --- a/lib/xmllib.lua +++ b/lib/xmllib.lua @@ -298,7 +298,7 @@ end function get_xml_section(t, s, norecurs, matches) local found, r - function get_xml_section_r(t, s, norecurs, matches) + local function get_xml_section_r(t, s, norecurs, matches) local k, v, found, r for k, v in pairs(t) do if type(v) == "table" then @@ -341,6 +341,46 @@ function get_xml_section(t, s, norecurs, matches) return r end +-- Very harsh function, but working for some XML cases. +function flatten_xml(t) + local function flatten_xml_r(t) + local r, n = {} + local i + + if type(t.n) ~= "number" then + if type(t.name) == "string" then + return r, t.name + else + error "Wrong XML structure." + end + end + + if t.n == 1 and type(t[1]) ~= "table" then + r = t[1] + else + for i = 1, t.n do + local cr, cn = flatten_xml_r(t[i]) + if r[cn] then + if not r[cn].__complex then + local ref = r[cn] + r[cn] = { __complex = true } + table.insert(r[cn], ref) + end + table.insert(r[cn], cr) + else + r[cn] = cr + end + end + end + + return r, t.name + end + + local r = flatten_xml_r(t) + + return r +end + --[[ example of use: result = [=[ |