Xml in Ruby

···

From: Erik Terpstra erik@terpnet.nl
Date: Fri, 3 Oct 2003 00:26:52 +0900
References: 83448
In-reply-to: 83448
Last year I made a quick hack that could do something
like that.
I didn’t have much experience with Ruby back then, so
it is a quick and
very dirty solution. But the following works:

require ‘XML’

y = XML %{


Joe
28

Engineering



Ken
26

Engineering



}

puts y.employees.employee[0].age
y.employees.employee[0].age = ‘9’
puts y.employees.employee[0].age

y.employees.employee.each do |employee|
puts “name: #{employee.name}, age:
#{employee.age}”
end
—>

for my third cent, the above syntax ‘just makes since’
to me. What could be more semantic than employee.name
without resorting to write your own class/accessors
for it? What’s wrong with that being a fundamental
data acess fascility? call me crazy

:pv


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

paul vudmaska wrote:

for my third cent, the above syntax ‘just makes since’
to me. What could be more semantic than employee.name
without resorting to write your own class/accessors
for it? What’s wrong with that being a fundamental
data acess fascility? call me crazy

The point of contention seems to be whether or not this is included in
the core language vs. a library, right? How does the Ruby community as a
whole benefit from core inclusion? What’s not satisfying to you about
this solution being contained in a library?

···

Chris
http://clabs.org/blogki

paul vudmaska wrote:

···

On Fri, 3 Oct 2003, Chris Morris wrote:

>for my third cent, the above syntax ‘just makes since’

>to me. What could be more semantic than employee.name

>without resorting to write your own class/accessors

>for it? What’s wrong with that being a fundamental

>data acess fascility? call me crazy

>

The point of contention seems to be whether or not this is included in

the core language vs. a library, right? How does the Ruby community as a

whole benefit from core inclusion? What’s not satisfying to you about

this solution being contained in a library?

One point I think the OP is also missing is that Erik’s solution is
implemented as a library and not as a modification to Ruby’s syntax. So,
in essence, you’ve already got what you want without hacking up the
interpreter.


“Chris Morris” chrismo@clabs.org
The point of contention seems to be whether or not
this is included in
the core language vs. a library, right? How does the
Ruby community as
a
whole benefit from core inclusion? What’s not
satisfying to you about
this solution being contained in a library?
<<------------

My appologies, i guess i was contentious but did not
mean to be.

My point is that that ruby might benefit from an xml
type, it need not be in the core if i were
burdensome/overkill/redundant- but use it much as i
would with rexml just snuggled up to ruby a little
more. So maybe we are not getting much. I hope you
guys dont think that i dont like rexml, its awesome. I
was thinking more of a promotion.

basically, i saw some other language implementing what
i think is a cool feature and let you know.

pv

···

Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

paul vudmaska wrote:

>for my third cent, the above syntax 'just makes

since’

>to me. What could be more semantic than

employee.name

>without resorting to write your own

class/accessors

>for it? What’s wrong with that being a

fundamental

>data acess fascility? call me crazy

>

The point of contention seems to be whether or not

this is included in

the core language vs. a library, right? How does

the Ruby community as a

whole benefit from core inclusion? What’s not

satisfying to you about

this solution being contained in a library?

One point I think the OP is also missing is that
Erik’s solution is
implemented as a library and not as a modification
to Ruby’s syntax. So,
in essence, you’ve already got what you want without
hacking up the
interpreter.

This is true!

···

— Chad Fowler chad@chadfowler.com wrote:

On Fri, 3 Oct 2003, Chris Morris wrote:


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

paul vudmaska wrote:


“Chris Morris” chrismo@clabs.org
The point of contention seems to be whether or not
this is included in
the core language vs. a library, right? How does the
Ruby community as
a
whole benefit from core inclusion? What’s not
satisfying to you about
this solution being contained in a library?
<<------------

My appologies, i guess i was contentious but did not
mean to be.

Sorry, poor word choice on my part, I didn’t intend for my comment or
questions to be harsh or negative. I just wanted to point out what I saw
as the key point and to try to get to the meat of it.

My point is that that ruby might benefit from an xml
type, it need not be in the core if i were
burdensome/overkill/redundant- but use it much as i
would with rexml just snuggled up to ruby a little
more. So maybe we are not getting much. I hope you
guys dont think that i dont like rexml, its awesome. I
was thinking more of a promotion.

I understood your desire to have something like REXML become more
inclusive, I just wasn’t hearing a clear argument as to the benefit of
having something like REXML be native rather than one require statement
away. I think we’re caught up now, though. Thx!

···

Chris
http://clabs.org/blogki

paul vudmaska wrote:

My point is that that ruby might benefit from an xml
type,

It already does: String.

Do you mean special type that defines a (presumably W3C) XML DOM,
other than a REXML Document?

See http://www.rubygarden.org/ruby?XMLinRuby

James Britt

paul vudmaska wrote:

My point is that that ruby might benefit from an
xml
type,

It already does: String.
Nope.

Do you mean special type that defines a (presumably
W3C) XML DOM,
other than a REXML Document?

Yes! REXML, supporting a quasi / ruby friendly DOM.

See http://www.rubygarden.org/ruby?XMLinRuby

James Britt

Wish i’d seen that thread awhile ago, thank you
:slight_smile:

Most peoples’ top priority is something like REXML,
so it’s item #1<<

perfect. it works good now but could be closer to
ruby.

What is meant when people refer to “XML”?
To me it means a stream of parsable tokens that any
xml parser wont die over. The api for getting that
data is sorta up in the air but rexml is far better
than any i’ve encountered (heavy on the ms side and
some javugly). The rexml site sums this up nicely.

What exactly is meant by “handle XML out of the
box”? <<
I mean something like this…

#(presuming %x creates an xml literal.)
x = %x{is your friend}

#access it like this…
print x.m.l #>is your friend

as opposed to
x = {‘m’=>{‘l’=>‘is your friend’}}

print x[‘m’][‘l’]#something like that.

I’m not a ruby guru and might not even be that good a
programmer but i like the first better. Much.

Will it mean the same thing to XML developers coming
to Ruby as it does to Ruby developers looking to work
with XML? <<

Accept the api will be better since it is ruby. It’s
just another tool. A way to create data structures. Of
any type.

Coming from both Asp Javascript(which has great xml
support but the api is not as awesome as rexml) and
PHP, whose xml api you can keep, i was anxious to find
a user friendly xml api. And then there was REXML.
That and finding e4x, prompted this thread.

Is there an “XML way”?<<
Probably. I hope it can conform to ruby :wink:

Who are the end users?<<
ME!Developers, but, subtly, its use brings it closer
to users as well, for the same reason templates have
gotten so popular. And html for that matter.

What are the requirements?<<
The ruby way or the highway. :slight_smile:

What are the time constraints? <<
REXML is fine and dandy now and for the foreseable
future, but strategically, it should be heavily
considered, pondered, kicked around, imo. And be ready
for production tommorrow.

[It should] features full XPath <<
Yes for more sophisticated queries that are not well
supported by a simple api. For instance multiple
changes of context and subqueries.

SAX<< …most helpful(necessary) for heavy sets, if
it’s that heavy maybe another means would be better
than having 2 apis. But maybe not :wink:

And then there is native Xsl(not really xsl but
programic styling of data,

http://martinfowler.com/bliki/movingAwayFromXslt.html

that hopefully, gleans some of xsl’s benefits without
its verbosity or syntax). But i’ll start that flame
war another day. Its been fun.

pv

···

— James Britt jamesUNDERBARb@seemyemail.com wrote:


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

paul vudmaska wrote:

I mean something like this…

#(presuming %x creates an xml literal.)
x = %x{is your friend}

#access it like this…
print x.m.l #>is your friend

as opposed to
x = {‘m’=>{‘l’=>‘is your friend’}}

print x[‘m’][‘l’]#something like that.

I’m not a ruby guru and might not even be that good a
programmer but i like the first better. Much.

How do you propose to handle an element like:

<xsl:import href=“…”/>

The set of allowable characters in an XML element is larger than the set of
allowable characters in a Ruby method name:

http://www.w3.org/TR/REC-xml#NT-NameChar

As for x = %x{is your friend}

There was recently a long discussion on this list about new “percent letter
brace” tricks. I think the general feeling was that it was simpler to just
create a method that takes a string as an argument and returns an XML element.

I don’t know if there are any methods that do that, but how would you feel
about:

x = XMLParser.parse “is your friend”

If that’s too wordy, you could always say:

def xml(arg)
XMLParser.parse(arg)
end

x = xml “is your friend”

Ben

paul vudmaska said:

I mean something like this…

#(presuming %x creates an xml literal.)
x = %x{is your friend}

#access it like this…
print x.m.l #>is your friend

as opposed to
x = {‘m’=>{‘l’=>‘is your friend’}}

print x[‘m’][‘l’]#something like that.

I’m not a ruby guru and might not even be that good a
programmer but i like the first better. Much.

Sorry to focus on just this aspect of this post… this would never work.
How would I translate this element into this format?

this

x = %x{stuff}
print x.element-name

would evaluate to x.element (minus) name.

Unfortunately, the characters allow in element names don’t allow this
“native” support. As a developer who uses XML daily, I find the current
interface extremely useful and powerful… especially with XPath. I don’t
want to have to DOM grovel down two different elements, when I can just as
easily do: x[‘//i-need-this-one’]

My 2c.

bs.

In article 20031002225021.59665.qmail@web10406.mail.yahoo.com,

paul vudmaska wrote:

#(presuming %x creates an xml literal.)
x = %x{is your friend}

#access it like this…
print x.m.l #>is your friend

Why the lucky stiff made a patch to do something like this with YAML using

From his site (http://whytheluckystiff.net):
"%y:

%y{ … }

I’ve been having a lot of fun with this Ruby 1.8.0 hack. This patch adds
the %y{ … } construct to Ruby. This way, you can embed YAML directly in
your Ruby script.

basic_map = %y{
— !omap

  • one: foo
  • two: bar
  • three: baz
    }

Kinda cool, eh?

Try:

patch -p1 -d ~/src/ruby-1.8.0 < ruby-1.8.0-yamlstr.patch"

···

paul vudmaska paul_vudmaska@yahoo.com wrote:

— James Britt jamesUNDERBARb@seemyemail.com wrote:

Ben Giddings wrote:

The set of allowable characters in an XML element is larger than the set
of allowable characters in a Ruby method name:

Actually, Ruby method names are pretty general (but I still agree with
your point)…

irb(main):006:0> x=
=>
irb(main):008:0> class << x; define_method :“&%(@)%&@” do puts “Hi.”
end; end
=> #Proc:0x401fbfac@:8(irb)
irb(main):009:0> x.send :“&%(@)%&@”

Hi.
=> nil

I’ve been searching archives, txt files, Matt’s documentation and Google to
figure out how you use RubyMail. Anybody have a clue?

I downloaded the latest release and in the Intro.txt Matt mentions to look
in the SMTP.txt file for a guide to send mail in RubyMail, but SMTP.txt
doesn’t exist. I checked his CVS tree do, and nothing.

Any ideas?

-Zach

paul vudmaska wrote:

I mean something like this…

#(presuming %x creates an xml literal.)
x = %x{is your friend}

#access it like this…
print x.m.l #>is your friend

as opposed to
x = {‘m’=>{‘l’=>‘is your friend’}}

print x[‘m’][‘l’]#something like that.

I’m not a ruby guru and might not even be that
good a
programmer but i like the first better. Much.

How do you propose to handle an element like:

<xsl:import href=“…”/>

One with a namespace ;/, i dont know, xpath maybe?

The set of allowable characters in an XML element is
larger than the set of
allowable characters in a Ruby method name:

Extensible Markup Language (XML) 1.0 (Fifth Edition)

As for x = %x{is your friend}

There was recently a long discussion on this list
about new “percent letter
brace” tricks. I think the general feeling was that
it was simpler to just
create a method that takes a string as an argument
and returns an XML element.

That is probably good. However in many cases with non
trivial xml, you’ll want to indent. Maybe heredoc
notation as an option?

#rexml
doc = Document.new(‘’)

and if it gets long

xml = << DOC

DOC
doc = Document.new(xml)

I was thinking of a native literal. Mas o menos.

As per the dot notation, good question. I’ve can think
of no compelling reason to use it in xml off hand.

Only hackish things come to mind that dont seem ruby
like.

#fragment
w = <x.y></x.y>
#maybe promoting the initial var.
w = x.y.w

So it looks as tho there’s an extra element.
Ambiguous. Hmmmmm. Don’t like/know. Maybe exclusively
relegated to xpath in that situation.

pv

···

— Ben Giddings bg-rubytalk@infofiend.com wrote:


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

BTW: self.gsub!(“Matt’s”, ‘matz’)

eek - gsub! of Matt Armstrong - RubyMail author :confused: