[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

groovy-like xml selection

Luke Graham

4/11/2005 3:50:00 AM

Hi list,

Ive taken a quick look at groovy for the first time, and saw an
interesting idea. Using xml to build an object tree, its possible to
write code like this...

<foo>
<bar>
<jim age="1"/>
<joe age="2"/>
</bar>
<bar/>
</foo>

foo.bar.star.select { |it| it[@age] == "1" && it.parent.name == "bar"
&& it.star.count == 0 }

Note that foo.bar and foo.bar.star are not arrays, rather
transparently hide multiple elements in one object. Requesting a value
would be handled by the first element.

The interesting part is that theres no quotes around the path
expression, its just evaluated in the context of bar. Complex
expressions could be broken into multiple selects and combined with
normal array operations.

Im sure someones done the xml->object (method in this case) thing, but
has the native path trick been tried in ruby yet?

PS. Extra kudos if someone implements not only the above, but foo.bar
{ ... }, where foo and bar are methods...

--
spooq


4 Answers

Robert Klemme

4/11/2005 7:45:00 AM

0


"Luke Graham" <spoooq@gmail.com> schrieb im Newsbeitrag
news:c6afaed005041020493e803be4@mail.gmail.com...
> Hi list,
>
> Ive taken a quick look at groovy for the first time, and saw an
> interesting idea. Using xml to build an object tree, its possible to
> write code like this...
>
> <foo>
> <bar>
> <jim age="1"/>
> <joe age="2"/>
> </bar>
> <bar/>
> </foo>
>
> foo.bar.star.select { |it| it[@age] == "1" && it.parent.name == "bar"
> && it.star.count == 0 }
>
> Note that foo.bar and foo.bar.star are not arrays, rather
> transparently hide multiple elements in one object. Requesting a value
> would be handled by the first element.
>
> The interesting part is that theres no quotes around the path
> expression, its just evaluated in the context of bar. Complex
> expressions could be broken into multiple selects and combined with
> normal array operations.
>
> Im sure someones done the xml->object (method in this case) thing, but
> has the native path trick been tried in ruby yet?

REXML comes pretty close. I'm sure with a small addition like this one
you get pretty far:

class REXML::Element
def method_missing(sym,*a,&b)
elements[sym.to_s] or super
end
end

Maybe you should add this to module REXML::Node.

> PS. Extra kudos if someone implements not only the above, but foo.bar
> { ... }, where foo and bar are methods...

Dunno what you mean here. foo and bar have to be methods.

Regards

robert

Aleksi

4/11/2005 12:48:00 PM

0

Robert Klemme wrote:
> "Luke Graham" <spoooq@gmail.com> schrieb im Newsbeitrag
>
>>Hi list,
>>
>>Ive taken a quick look at groovy for the first time, and saw an
>>
>><foo>
>> <bar>
>> <jim age="1"/>
>> <joe age="2"/>
>> </bar>
>> <bar/>
>></foo>
>>
>>foo.bar.star.select { |it| it[@age] == "1" && it.parent.name == "bar"
>>&& it.star.count == 0 }
>>
>
>
> REXML comes pretty close. I'm sure with a small addition like this one
> you get pretty far:
>
> class REXML::Element
> def method_missing(sym,*a,&b)
> elements[sym.to_s] or super
> end
> end
>
> Maybe you should add this to module REXML::Node.

I said a week ago about this simple REXML wrapup:

http://groups-beta.google.com/group/comp.lang.ruby/browse_frm/thread/f4e64a6983199c70/c3b1b15741232e9a?q=comp.lang.ruby+simplexml&rnum=1#c3b1b1...

I'm not sure this is what you meant, but thought to point to it anyway.

Effectively you can write code like given in example
at http://www.cs.helsinki.fi/u/kaniemel/Simp...

xml = SimpleXML.load(xml_string) # or File
puts xml.movie[0].plot # => "\n Whether you..."

>>Im sure someones done the xml->object (method in this case) thing, but
>>has the native path trick been tried in ruby yet?

I'm not sure what you mean with native path trick. But if that's being
able to write xml.foo which finds a correct branch in DOM tree and
allows one to continue with that branch, yes this module already
implements it over REXML.

I have currently no plans to go forward with it, but I've been pondering
if there should be generic API for accessing and modifying and
underlying implementations over whatevery DOM traversal API is present
be it REXML or any of the 15 libraries out there :).

- Aleksi

Luke Graham

4/12/2005 12:47:00 AM

0

On Apr 11, 2005 5:49 PM, Robert Klemme <bob.news@gmx.net> wrote:
>
> "Luke Graham" <spoooq@gmail.com> schrieb im Newsbeitrag
> news:c6afaed005041020493e803be4@mail.gmail.com...
> > Hi list,
> >
> > Ive taken a quick look at groovy for the first time, and saw an
> > interesting idea. Using xml to build an object tree, its possible to
> > write code like this...
> >
> > <foo>
> > <bar>
> > <jim age="1"/>
> > <joe age="2"/>
> > </bar>
> > <bar/>
> > </foo>
> >
> > foo.bar.star.select { |it| it[@age] == "1" && it.parent.name == "bar"
> > && it.star.count == 0 }
> >
> > Note that foo.bar and foo.bar.star are not arrays, rather
> > transparently hide multiple elements in one object. Requesting a value
> > would be handled by the first element.
> >
> > The interesting part is that theres no quotes around the path
> > expression, its just evaluated in the context of bar. Complex
> > expressions could be broken into multiple selects and combined with
> > normal array operations.
> >
> > Im sure someones done the xml->object (method in this case) thing, but
> > has the native path trick been tried in ruby yet?
>
> REXML comes pretty close. I'm sure with a small addition like this one
> you get pretty far:
>
> class REXML::Element
> def method_missing(sym,*a,&b)
> elements[sym.to_s] or super
> end
> end
>
> Maybe you should add this to module REXML::Node.
>
> > PS. Extra kudos if someone implements not only the above, but foo.bar
> > { ... }, where foo and bar are methods...
>
> Dunno what you mean here. foo and bar have to be methods.

I have no idea what I meant either, it was late :(

--
spooq


Aleksi

4/12/2005 10:17:00 AM

0

Aleksi wrote:
> Robert Klemme wrote:
>
>> "Luke Graham" <spoooq@gmail.com> schrieb im Newsbeitrag
>>
>>> Hi list,
>>>
>>> Ive taken a quick look at groovy for the first time, and saw an
> Effectively you can write code like given in example
> at http://www.cs.helsinki.fi/u/kaniemel/Simp...
>
> xml = SimpleXML.load(xml_string) # or File
> puts xml.movie[0].plot # => "\n Whether you..."
>
> I have currently no plans to go forward with it, but I've been pondering
> if there should be generic API for accessing and modifying and
> underlying implementations over whatevery DOM traversal API is present
> be it REXML or any of the 15 libraries out there :).

I just wanted to note there has already been Jim Weirich's Builder
around for half a year. With it one can build XML easily, in Groovy
style I reckon.

http://onestepback.org/index.cgi/Tech/Ruby/BuilderOb...

- Aleksi