[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

construct AoA with REXML XPath?

Chris McMahon

3/15/2006 6:59:00 PM

Hi...

say I have a document

<plan xsi:type="ns0:plan">
<id>123</id>
<status>ACTIVE</status>
<jellyBean>RED</jellyBean>
<foo>BAR</foo>
</plan>
<plan xsi:type="ns0:plan">
<id>456</id>
<status>BOGUS</status>
<jellyBean>BLUE</jellyBean>
<foo>BAZ</foo>
</plan>

I know I can do
ids = doc.elements.to_a( "//plan/id")
jellybeans = doc.elements.to_a( "//plan/jellyBean")

but is there some way to do something like

idsAndJellyBeans = doc.elements.to_a( "//plan/id" "//plan/jellyBean")

and get

[[123, "RED"],[456,"BLUE"]]?

Thanks...

26 Answers

gordon

3/15/2006 9:42:00 PM

0

You could use Array#zip

ids = doc.elements.to_a( "//plan/id/").map{|i| i.text}
=> ["123", "456"]

jellybeans = doc.elements.to_a( "//plan/jellyBean").map {|i| i.text}
=> ["RED", "BLUE"]

idsAndJellyBeans = ids.zip(jellybeans)
=> [["123", "RED"], ["456", "BLUE"]]

Chris McMahon

3/15/2006 10:22:00 PM

0

Yes, but I wonder if there is a rock-solid guarantee that each array
that is an element of idsAndJellyBeans is in fact from the same "plan"
record.

Empirical research suggests that they are, but I'm nervous. That's why
I wonder if there is some sort of pure-REXML trick that would extract
multiple elements from within a single record, so that I could always
be sure that the id and the jellyBean in each individual array had FER
SHURE come from the same "plan" record.

William James

3/16/2006 4:51:00 AM

0

Chris McMahon wrote:
> Hi...
>
> say I have a document
>
> <plan xsi:type="ns0:plan">
> <id>123</id>
> <status>ACTIVE</status>
> <jellyBean>RED</jellyBean>
> <foo>BAR</foo>
> </plan>
> <plan xsi:type="ns0:plan">
> <id>456</id>
> <status>BOGUS</status>
> <jellyBean>BLUE</jellyBean>
> <foo>BAZ</foo>
> </plan>
>
> I know I can do
> ids = doc.elements.to_a( "//plan/id")
> jellybeans = doc.elements.to_a( "//plan/jellyBean")
>
> but is there some way to do something like
>
> idsAndJellyBeans = doc.elements.to_a( "//plan/id" "//plan/jellyBean")
>
> and get
>
> [[123, "RED"],[456,"BLUE"]]?
>
> Thanks...

Using plain Ruby:

class Array; alias atr first; alias txt last end
class String
def xtag(str)
result = []
re = %r{ < #{str} (?: \s+ ( (?> [^>"/]* (?> "[^"]*" )? )* ) )? }xi
scan( %r{ #{re} / > | #{re} > ( .*? ) </ #{str} >
}mix ) { |unpaired, attr, data| h = { }
( unpaired || attr || "" ).
scan( %r{ ( \w+ ) \s* = \s* (?: " ( [^"]* ) " | ( \S+ ) )
}mx ) { |k,v,v2|
h[k.downcase] = (v || v2) }
block_given? ? ( yield [ h, data ] ) : result << [ h, data ]
}
result
end
end

p DATA.read.xtag('plan').map{|attr,text|
[ text.xtag('id').first.txt,
text.xtag('jellyBean').first.txt
]
}

__END__
<plan xsi:type="ns0:plan">
<id>123</id>
<status>ACTIVE</status>
<jellyBean>RED</jellyBean>
<foo>BAR</foo>
</plan>
<plan xsi:type="ns0:plan">
<id>456</id>
<status>BOGUS</status>
<jellyBean>BLUE</jellyBean>
<foo>BAZ</foo>
</plan>

William Clark

12/17/2010 2:15:00 PM

0

In article <e0olg6p28phjh9tucnnqaaqqf7u9jjtuaj@4ax.com>,
Fred J. McCall <fjmccall@gmail.com> wrote:

> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>
> >In article <4gdkg6tssufe1p4u1ieba1o1mrte6bg5h4@4ax.com>,
> > Fred J. McCall <fjmccall@gmail.com> wrote:
> >
> >> William Clark <wclark2@colnospamumbus.rr.com> wrote:
> >>
> >> >In article <zNidnWmZIftMt5fQnZ2dnUVZ_t-dnZ2d@giganews.com>,
> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >
> >> >> "William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
> >> >> news:wclark2-75FE1F.16041615122010@charm.magnus.acs.ohio-state.edu...
> >> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >> >> "Westprog" <westprog@hottmail.com> wrote in message
> >> >> >> >
> >> >> >> > It's not that the British or Irish tests are that good - it's that
> >> >> >> > most
> >> >> >> > US
> >> >> >> > tests appear to be barely driving tests at all. They test the
> >> >> >> > ability
> >> >> >> > to
> >> >> >> > start the car and manouevre it in a circle.
> >> >> >>
> >> >> >> A bit more to the driving and written tests than that.
> >> >> >> Most sober Americans do very well when it comes to safety.
> >> >> >> Those who have taken to the drink cause most of our problems.
> >> >> >
> >> >> > I am scared of drunk drivers - we all should be. What is more
> >> >> > worrying,
> >> >> > though, is that I am almost equally scared of sober ones in the US.
> >> >> > Too
> >> >> > many of them are just that incompetent, and ignorant of their own
> >> >> > short
> >> >> > comings..
> >> >>
> >> >> Come now, Mister Clark, methinks you embellish a bit.
> >> >> But I've a solution to your problem.
> >> >> If American drivers scare the bejayus out of you, stay in Ireland.
> >> >> Now there's a happy thought.....................
> >> >
> >> >Why would I "stay in Ireland"? I have the driving skills and caution
> >> >necessary to avoid the idiots. You almost certainly do not.
> >> >
> >>
> >> And yet you're the one out there afraid of things. Don't you find
> >> that interesting? I know I do...
> >
> >No, not really "afraid", just sufficiently aware of their lack of
> >driving skills to want to give them a very wide berth.
> >
>
> Gee, that must make things like driving down the street really tough
> for you...

I suppose you go down the freeway at 70, sitting five feet of the trunk
of the car in front of you?

Welcome to the US drivers' club.

William Clark

12/17/2010 2:16:00 PM

0

In article <lrnlg6prnuc4k3logsk0brj0vj95djtqe6@4ax.com>,
Fred J. McCall <fjmccall@gmail.com> wrote:

> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>
> >In article <KKKdneASH4ks25fQnZ2dnUVZ_vmdnZ2d@giganews.com>,
> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >
> >> "William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
> >> news:wclark2-42BA4D.10332316122010@charm.magnus.acs.ohio-state.edu...
> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >
> >>
> >> >> Come now, Mister Clark, methinks you embellish a bit.
> >> >> But I've a solution to your problem.
> >> >> If American drivers scare the bejayus out of you, stay in Ireland.
> >> >> Now there's a happy thought.....................
> >> >
> >> > Why would I "stay in Ireland"? I have the driving skills and caution
> >> > necessary to avoid the idiots. You almost certainly do not.
> >>
> >> So far I've successfully avoided the idiots.
> >> Except for a bit of a problem backing up, I am an exemplary driver.
> >> (They are very quick around here to lift the license of us ancient ones if
> >> we prove to be somewhat remiss in our driving habits.)
> >
> >"Backing up". Then I assume you never had to learn to reverse round a
> >corner in order to pass your "driving test"? Tut, tut.
> >
>
> Why would anyone want to test for that? I mean, that sounds like a
> stupid thing to do, so why practice to pass a 'test' over it?

Because, by Conway's own admission, he can't do it? I would pay the
price of admission to see you try to get your car into a small parking
space. It would be a good laugh.
>
> Besides which, how hard can it be? I mean, getting in and out of
> parallel parking is SO hard for you Brits.

Sounds like it's too hard for your date.

William Clark

12/17/2010 2:17:00 PM

0

In article <83olg69l6c5dg2cueg7k88r8n2o4v0ai0l@4ax.com>,
Fred J. McCall <fjmccall@gmail.com> wrote:

> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>
> >In article <6ddkg6lukb999vrotts35a7gh0l7ma94i5@4ax.com>,
> > Fred J. McCall <fjmccall@gmail.com> wrote:
> >
> >> William Clark <wclark2@colnospamumbus.rr.com> wrote:
> >>
> >> >In article <638kg6plfgsdmm63b1ekicctskds43forh@4ax.com>,
> >> > Fred J. McCall <fjmccall@gmail.com> wrote:
> >> >
> >> >> William Clark <wclark2@colnospamumbus.rr.com> wrote:
> >> >>
> >> >> >In article <ut1jg61it5b3lftddal2sh97t0tr79cavi@4ax.com>,
> >> >> > Fred J. McCall <fjmccall@gmail.com> wrote:
> >> >> >
> >> >> >> William Clark <wclark2@colnospamumbus.rr.com> wrote:
> >> >> >>
> >> >> >> >In article <3ISdndkCWedjQZXQnZ2dnUVZ_tKdnZ2d@giganews.com>,
> >> >> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >> >> >
> >> >> >> >> "Westprog" <westprog@hottmail.com> wrote in message
> >> >> >> >> news:ieadcj$fh$1@news.datemas.de...
> >> >> >> >> > William Clark wrote:
> >> >> >> >> >> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >> >> >> >
> >> >> >> >> > It's not that the British or Irish tests are that good - it's
> >> >> >> >> > that
> >> >> >> >> > most
> >> >> >> >> > US
> >> >> >> >> > tests appear to be barely driving tests at all. They test the
> >> >> >> >> > ability
> >> >> >> >> > to
> >> >> >> >> > start the car and manouevre it in a circle.
> >> >> >> >>
> >> >> >> >> A bit more to the driving and written tests than that.
> >> >> >> >> Most sober Americans do very well when it comes to safety.
> >> >> >> >> Those who have taken to the drink cause most of our problems.
> >> >> >> >
> >> >> >> >I am scared of drunk drivers - we all should be. What is more
> >> >> >> >worrying,
> >> >> >> >though, is that I am almost equally scared of sober ones in the US.
> >> >> >> >Too
> >> >> >> >many of them are just that incompetent, and ignorant of their own
> >> >> >> >short
> >> >> >> >comings..
> >> >> >> >
> >> >> >>
> >> >> >> Feel free to go back wherever it is you're more comfortable and stay
> >> >> >> there...
> >> >> >
> >> >> >That would be anywhere that a cocky little git like you isn't.
> >> >> >
> >> >>
> >> >> So why are you still here, again?
> >> >
> >> >Because you are nothing more than hot air on the Usenet.
> >> >
> >>
> >> And you have an affinity for hot air, do you?
> >>
> >> I can see why, since it seems to be all there is to you. However, the
> >> fact that you think we have something in common (we don't) doesn't
> >> make you my friend...
> >
> >No, it is you and Conway that "have something in common", But don't
> >worry, the secret you two share is safe with me.
> >
>
> Jealousy is *such* an ugly emotion, Willie...

No, thank you, I don't play on that side of the fence. But I am broad
minded, so each to his own.

Fred J. McCall

12/17/2010 3:49:00 PM

0

William Clark <wclark2@colnospamumbus.rr.com> wrote:

>In article <e0olg6p28phjh9tucnnqaaqqf7u9jjtuaj@4ax.com>,
> Fred J. McCall <fjmccall@gmail.com> wrote:
>
>> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>>
>> >In article <4gdkg6tssufe1p4u1ieba1o1mrte6bg5h4@4ax.com>,
>> > Fred J. McCall <fjmccall@gmail.com> wrote:
>> >
>> >> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>> >>
>> >> >In article <zNidnWmZIftMt5fQnZ2dnUVZ_t-dnZ2d@giganews.com>,
>> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>> >> >
>> >> >> "William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
>> >> >> news:wclark2-75FE1F.16041615122010@charm.magnus.acs.ohio-state.edu...
>> >> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>> >> >> >> "Westprog" <westprog@hottmail.com> wrote in message
>> >> >> >> >
>> >> >> >> > It's not that the British or Irish tests are that good - it's that
>> >> >> >> > most
>> >> >> >> > US
>> >> >> >> > tests appear to be barely driving tests at all. They test the
>> >> >> >> > ability
>> >> >> >> > to
>> >> >> >> > start the car and manouevre it in a circle.
>> >> >> >>
>> >> >> >> A bit more to the driving and written tests than that.
>> >> >> >> Most sober Americans do very well when it comes to safety.
>> >> >> >> Those who have taken to the drink cause most of our problems.
>> >> >> >
>> >> >> > I am scared of drunk drivers - we all should be. What is more
>> >> >> > worrying,
>> >> >> > though, is that I am almost equally scared of sober ones in the US.
>> >> >> > Too
>> >> >> > many of them are just that incompetent, and ignorant of their own
>> >> >> > short
>> >> >> > comings..
>> >> >>
>> >> >> Come now, Mister Clark, methinks you embellish a bit.
>> >> >> But I've a solution to your problem.
>> >> >> If American drivers scare the bejayus out of you, stay in Ireland.
>> >> >> Now there's a happy thought.....................
>> >> >
>> >> >Why would I "stay in Ireland"? I have the driving skills and caution
>> >> >necessary to avoid the idiots. You almost certainly do not.
>> >> >
>> >>
>> >> And yet you're the one out there afraid of things. Don't you find
>> >> that interesting? I know I do...
>> >
>> >No, not really "afraid", just sufficiently aware of their lack of
>> >driving skills to want to give them a very wide berth.
>> >
>>
>> Gee, that must make things like driving down the street really tough
>> for you...
>>
>
>I suppose you go down the freeway at 70, sitting five feet of the trunk
>of the car in front of you?
>
>Welcome to the US drivers' club.
>

I know thought is hard for you, but lets give it a try, shall we?

YOU: "give them a very wide birth"

Now, how wide is the road? For you to "give them a very wide birth" I
assume you must be pulling up onto the sidewalks or something every
time a car comes down the street the other way...

--
"Some people get lost in thought because it's such unfamiliar
territory."
--G. Behn

Fred J. McCall

12/17/2010 3:51:00 PM

0

William Clark <wclark2@colnospamumbus.rr.com> wrote:

>In article <lrnlg6prnuc4k3logsk0brj0vj95djtqe6@4ax.com>,
> Fred J. McCall <fjmccall@gmail.com> wrote:
>
>> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>>
>> >In article <KKKdneASH4ks25fQnZ2dnUVZ_vmdnZ2d@giganews.com>,
>> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>> >
>> >> "William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
>> >> news:wclark2-42BA4D.10332316122010@charm.magnus.acs.ohio-state.edu...
>> >> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>> >> >
>> >>
>> >> >> Come now, Mister Clark, methinks you embellish a bit.
>> >> >> But I've a solution to your problem.
>> >> >> If American drivers scare the bejayus out of you, stay in Ireland.
>> >> >> Now there's a happy thought.....................
>> >> >
>> >> > Why would I "stay in Ireland"? I have the driving skills and caution
>> >> > necessary to avoid the idiots. You almost certainly do not.
>> >>
>> >> So far I've successfully avoided the idiots.
>> >> Except for a bit of a problem backing up, I am an exemplary driver.
>> >> (They are very quick around here to lift the license of us ancient ones if
>> >> we prove to be somewhat remiss in our driving habits.)
>> >
>> >"Backing up". Then I assume you never had to learn to reverse round a
>> >corner in order to pass your "driving test"? Tut, tut.
>> >
>>
>> Why would anyone want to test for that? I mean, that sounds like a
>> stupid thing to do, so why practice to pass a 'test' over it?
>
>Because, by Conway's own admission, he can't do it? I would pay the
>price of admission to see you try to get your car into a small parking
>space. It would be a good laugh.
>

What's that got to do with "reverse round a corner"?

>
>>
>> Besides which, how hard can it be? I mean, getting in and out of
>> parallel parking is SO hard for you Brits.
>>
>
>Sounds like it's too hard for your date.
>

No, she does it fine, too.

--
"Oooo, scary! Y'know, there are a lot scarier things
in the world than you ... and I'm one of them."

-- Buffy the vampire

conwaycaine

12/17/2010 6:41:00 PM

0


"William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
news:wclark2-7FECD0.16410116122010@charm.magnus.acs.ohio-state.edu...
> In article <KKKdneASH4ks25fQnZ2dnUVZ_vmdnZ2d@giganews.com>,
> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>
>> "William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
>> news:wclark2-42BA4D.10332316122010@charm.magnus.acs.ohio-state.edu...
>> > "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>> >
>>
>> >> Come now, Mister Clark, methinks you embellish a bit.
>> >> But I've a solution to your problem.
>> >> If American drivers scare the bejayus out of you, stay in Ireland.
>> >> Now there's a happy thought.....................
>> >
>> > Why would I "stay in Ireland"? I have the driving skills and caution
>> > necessary to avoid the idiots. You almost certainly do not.
>>
>> So far I've successfully avoided the idiots.
>> Except for a bit of a problem backing up, I am an exemplary driver.
>> (They are very quick around here to lift the license of us ancient ones
>> if
>> we prove to be somewhat remiss in our driving habits.)
>
> "Backing up". Then I assume you never had to learn to reverse round a
> corner in order to pass your "driving test"? Tut, tut.

No, I haven't.
The local law much prefers we go round corners front first rather than rear
first.
Is the reverse thingy one of those quaint Brit driving habits?


conwaycaine

12/17/2010 6:44:00 PM

0


"William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
news:wclark2-318623.09155817122010@charm.magnus.acs.ohio-state.edu...
> Fred J. McCall <fjmccall@gmail.com> wrote:
>>
>> Why would anyone want to test for that? I mean, that sounds like a
>> stupid thing to do, so why practice to pass a 'test' over it?
>
> Because, by Conway's own admission, he can't do it?

Bill (May I call you "Bill"?) is the world a literal place for you?

>I would pay the
> price of admission to see you try to get your car into a small parking
> space. It would be a good laugh.

Why would you assume Fred can't get into a small parking space.
And why must Brits back into a parking place?
Front first is so much easier.