groovy-like xml selection

L

Luke Graham

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...
 
R

Robert Klemme

Luke Graham said:
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
 
A

Aleksi

Robert said:
Luke Graham said:
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...p.lang.ruby+simplexml&rnum=1#c3b1b15741232e9a

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/SimpleXML/doc/

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

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
 
L

Luke Graham

Luke Graham said:
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 :(
 
A

Aleksi

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

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/BuilderObjects.rdoc

- Aleksi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top