lua-users home
lua-l archive

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

cool. looking forward to it to use it for xml processing in lua

On Jan 7, 2008 10:29 AM, Javier Guerra <> wrote:
I'm sure not to be the first to want E4X's clean syntax in Lua

but, here's my first successful attempt at that.  so far, it only
manages the read part, no nice way to modify the XML yet.

what does work:

- get subtags as fields
- get attributes as fields with leading '_'
- convert to XML text
- get content as string
- insert lua code to filter results based on attributes
- a L4X object represents a list of XML nodes, everything above works
with a set as well as a single node
- get number of results with '#' operator, and specific node with '[n]'

- iterator for lists
- modify the XML
- handle namespaces (currently you can use ["ns:tag"] )
- move the backing structs from LOM to something more optimised.
- add cacheing or memoizing, but currently you can use local vars to
hold any repeating part of a query


------------- lua code -------------
local l4x = require "l4x"
local x = l4x ([[
   <title>this is the title</title>
   <entry id="1" visible="true">
       <title>first entry</title>
       <description>something more</description>
       <link type="text/html" href="" href="" target="_blank">" />
       <link type="image/jpeg" href="" href="" target="_blank">" />
   <entry id="2" visible="false">
       <title>second entry</title>
       <description>blah blah</description>
       <link type="text/html" href="" href="" target="_blank">" />
       <link type="image/jpeg" href="" href="" target="_blank">" />
   <entry id="3" visible="true">
       <title>third entry</title>
       <description>worthless scrap</description>
       <link type="text/html" href=""" />
       <link type="image/jpeg" href="" href="" target="_blank"> " />

print (x.title())  --  "<title>this is the title</title>"
print (x.title(true)  --  "this is the title"
print (#x.entry)     --  3

local ents = x.entry('visible == "true"')  -- list of visible entries
print (#ents)    -- 2

print (ents.title(true))    -- concatenation of titles
local images = ('type=="image/jpeg"')._ref  -- array of image urls
print (ents[1].title(true))     -- title of first entry
print (ents.title[1](true))     -- same thing (slower?)

------------------ end of lua code -----------------------

at first i thought of creating a new LuaForge project, but now i think
it might be better to add it to LuaExpat's collection.  it's just 200
lines of lua. (no C code, but uses newproxy() )

any opinions?