lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2016-08-21 15:45 GMT+01:00 Marc Balmer <marc@msys.ch>:
> I am working on code to easily create XML trees from Lua tables.  What would,
> in your opinion, be a comfortable Lua table notation of XML trees, expressed
> as Lua tables?

A misclick got me to read this thread, and it just so happens I wrote
something like that today. I got a syntax looking like this:

local function e(label) -- e for element
    return function(node)
        xml.setlabel(node, label)
        return node
    end
end

local doc = e 'html' { xmlns = "http://www.w3.org/1999/xhtml";,
['xml:lang'] = "en", lang = "en",
    e 'head' {
        e 'title' { "Test page" },
        e 'meta' { charset = "utf-8" },
    },
    e 'body' { style = "font-family: sans-serif",
        e 'p' { [[Hello World!]] },
    },
}

xml.write(file, { version = '1.0', root = doc })

Basically each element is a table, string keys are attributes,
numerical keys are children (strings or subtables), and the element
tag/label is stored using a special key private to the xml module
(hence the xml.setlabel and the 'e' sugar).