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 <javier@guerrag.com> 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]'

todo's:
- 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

example:

------------- lua code -------------
local l4x = require "l4x"
local x = l4x ([[
<feed>
   <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="http://www.xxx.com/one.html" target="_blank">http://www.xxx.com/one.html" />
       <link type="image/jpeg" href="" href="http://www.xxx.com/one.jpg" target="_blank">http://www.xxx.com/one.jpg" />
   <entry>
   <entry id="2" visible="false">
       <title>second entry</title>
       <description>blah blah</description>
       <link type="text/html" href="" href="http://www.xxx.com/two.html" target="_blank"> http://www.xxx.com/two.html" />
       <link type="image/jpeg" href="" href="http://www.xxx.com/two.jpg" target="_blank">http://www.xxx.com/two.jpg" />
   <entry>
   <entry id="3" visible="true">
       <title>third entry</title>
       <description>worthless scrap</description>
       <link type="text/html" href="" http://www.xxx.com/three.html" />
       <link type="image/jpeg" href="" href="http://www.xxx.com/three.jpg" target="_blank">http://www.xxx.com/three.jpg " />
   <entry>
</feed>
]])

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 = ents.link ('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?

--
Javier