Xpath like syntax

L

Luke Galea

Hi all,

curious if there is a nice ruby way to express an xpath like navigation of an
object graph..

ie:

XPATH way:
Countries/Provinces/Cities[ @name = "London" ]

versus

The only Ruby way I can think of:
countries.collect{ |c| c.provinces.collect {|p| p.cities.select { |c2| c2#name
== 'London' } } }

Thanks in advance
 
T

trans.

Something like this certainly would be nice. Can you think of a nice
way to represent it in Ruby?

~trans.
 
N

Nicholas Van Weerdenburg

Hi all,

curious if there is a nice ruby way to express an xpath like navigation of an
object graph..

ie:

XPATH way:
Countries/Provinces/Cities[ @name = "London" ]

versus

The only Ruby way I can think of:
countries.collect{ |c| c.provinces.collect {|p| p.cities.select { |c2| c2#name
== 'London' } } }

Thanks in advance

I asked a similar question a couple of weeks ago, and there was
nothing existing in the ruby world from the looks of it.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125206

Some external projects to look at mentioned:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125284

A code idea:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125291

JXpath in the Java world seems like it's for exactly this purpose.
http://jakarta.apache.org/commons/jxpath/

I haven't worked on the query aspect of my project lately, so I
haven't made up my mind how I'm going to approach it yet.

Regards,
Nick
 
A

Aredridel

Hi all,

curious if there is a nice ruby way to express an xpath like navigation of an
object graph..

ie:

XPATH way:
Countries/Provinces/Cities[ @name = "London" ]

I was planning on implementing something like this this week!

Ari
 
T

trans.

itsme213 your Q class is promising.

Sometime ago I put out a small challenge to do R like querying and per
element processing. See ruby-talk:116415

http://groups-beta.google.com/group...deb04738c0e/788e64d6ca0dc746#788e64d6ca0dc746

You'll notice Niklas Frykholm gave a nice solution which reminds me of
this. I haven't looked at the possibility in depth, but I wonder if
these two can be integrated? If you wanted to develop further I would
linke to include in upcoming Ruby Carats release.
 
T

trans.

Does Ruby Carats do the same thing as Gabriele's extensions?

Gabriele's? Do you mean Gavins? Or is this another set of extensions I
am not aware?

Ruby Facets is the extensions library, which is unique for its
atomicity.

Ruby Carats OTOH, is an extensive collection of Classes, Modules and
Mixins (but has not yet been released.)
See http://calibre.rubyforge.org/
 
T

trans.

I was planning on implementing something like this this week!

Common wavelengths are amazing common around here :)
Perhaps then you'd be interested in what I have suggested about buiding
a general purpose class for these use cases?
 
A

Aredridel

Common wavelengths are amazing common around here :)
Perhaps then you'd be interested in what I have suggested about buiding
a general purpose class for these use cases?

I was thinking of doing that as well, so I certainly would. I was
thinking that a traverse_path operation is pretty common, and any set of
objects implementing that protocol could be used .. be they a filesystem
tree, an XML document, or an object graph.

I still haven't figured out what the basic API should look like, but I
can imagine it being similar in complexity to Enumerable -- a sort of
Traversable object.

Ari
 
T

trans.

Traversable mixin! That would be great!

As for API, probably best just would work out what your current needs
are (I assume you have a current need ;-), and implement that first.
Most of the rest wil probbly fall out from there, and then others can
offer additional suggestion for functionaity. That's how I'd probbly go
about it, anyway.

Keep me informed and I'd be glad to help in any way I can.
HTH,
trans.
 
A

Aredridel

Traversable mixin! That would be great!

Would a single method, traverse_component be sufficient to build an
implementation on, do you think? Pass it a path component, and have the
object return whatever makes sense? (for a hash or array, map it to [];
for Objects in general, map it to send?)

Perhaps have a second method, traverse_filter, that receives an array of
filters, for XPath-style qualifications like [@attr = 'foo'], or
[/childobject]?
As for API, probably best just would work out what your current needs
are (I assume you have a current need ;-), and implement that first.
Most of the rest wil probbly fall out from there, and then others can
offer additional suggestion for functionaity. That's how I'd probbly go
about it, anyway.

My uses are, right now, for passing an object graph as model data to an
XTemplate view (something that Amrita does, and XTemplate does not,
yet), and for mapping a rails-style URL to an object graph of handlers
-- so that /foo/bar/baz maps to (roughly) startingpoint.foo.bar.baz
Keep me informed and I'd be glad to help in any way I can.

Alright.

Ari
 
N

Nicholas Van Weerdenburg

I'd like a simple addition to Enumerable. Also, maybe combine the
concepts of XPath and XQuery.

nodes= aobjectgraph.traverse("graph path here")
# or
nodes= aobjectgraph.query( "query spec here")

As well as the block result processing versions.

I think following the semantics of xpath and xquery, while maintaining
the semantics of Enumerable, would be ideal. In that light, filters
and such would ideally be done via normal Enumerable primitives.

To follow Ruby conventions, it may make more sense to extend
find/select or add a graph_select/graph_find method (maintaining
similarity to select/find naming).

Regards,
Nick

Traversable mixin! That would be great!

Would a single method, traverse_component be sufficient to build an
implementation on, do you think? Pass it a path component, and have the
object return whatever makes sense? (for a hash or array, map it to [];
for Objects in general, map it to send?)

Perhaps have a second method, traverse_filter, that receives an array of
filters, for XPath-style qualifications like [@attr = 'foo'], or
[/childobject]?
As for API, probably best just would work out what your current needs
are (I assume you have a current need ;-), and implement that first.
Most of the rest wil probbly fall out from there, and then others can
offer additional suggestion for functionaity. That's how I'd probbly go
about it, anyway.

My uses are, right now, for passing an object graph as model data to an
XTemplate view (something that Amrita does, and XTemplate does not,
yet), and for mapping a rails-style URL to an object graph of handlers
-- so that /foo/bar/baz maps to (roughly) startingpoint.foo.bar.baz
Keep me informed and I'd be glad to help in any way I can.

Alright.

Ari
``>
 
T

trans.

Aredridel said:
Traversable mixin! That would be great!

Would a single method, traverse_component be sufficient to build an
implementation on, do you think? Pass it a path component, and have the
object return whatever makes sense? (for a hash or array, map it to [];
for Objects in general, map it to send?)

I'd think so. And I think it can be kept reasonably simple -- just
#traverse or #travel or something like that should suffice.

The return value may be better as the special object (what itisme213 is
calling Q) so that it could be further queried/filtered, and also
operated on "element-wise" (i.e. all the elements could be modified in
parallel). Or maybe have two methods, one returns another "Q" and the
other "whatever makes sense" (with the Q object another method, say
#value, could return "wahtever made sence). Basically thinking out loud
here. Playing with some code will probably reveal much more.
Perhaps have a second method, traverse_filter, that receives an array of
filters, for XPath-style qualifications like [@attr = 'foo'], or
[/childobject]?

If a path is a string (?) and filters are always in an array, the
differnce in class may allow for use of the same method, instead have
having two separate methods. But having two merthods is okay if need be
too.
My uses are, right now, for passing an object graph as model data to an
XTemplate view (something that Amrita does, and XTemplate does not,
yet), and for mapping a rails-style URL to an object graph of handlers
-- so that /foo/bar/baz maps to (roughly) startingpoint.foo.bar.baz
I see. Looks like some very good use cases.
 
L

Luke Galea

Actually when I posted the original question I had something like JXPath in
mind.. or OGNL.. (ognl.org)

It's awesome how many alternate implementations have been proposed in answer!
Open source at it's best!!

Hi all,

curious if there is a nice ruby way to express an xpath like navigation
of an object graph..

ie:

XPATH way:
Countries/Provinces/Cities[ @name = "London" ]

versus

The only Ruby way I can think of:
countries.collect{ |c| c.provinces.collect {|p| p.cities.select { |c2|
c2#name == 'London' } } }

Thanks in advance

I asked a similar question a couple of weeks ago, and there was
nothing existing in the ruby world from the looks of it.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125206

Some external projects to look at mentioned:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125284

A code idea:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/125291

JXpath in the Java world seems like it's for exactly this purpose.
http://jakarta.apache.org/commons/jxpath/

I haven't worked on the query aspect of my project lately, so I
haven't made up my mind how I'm going to approach it yet.

Regards,
Nick
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top