(Sorry about top posting - Outlook...)
Personally I prefer handling the different pieces of the XML in a consistent
manner. That is, if you have the XML you provided, plus a little:
<foo>
<bar xmlns="http://..." type="html">
HTML Content
</bar>
<bar xmlns="http://..." type="rss">
RSS Content
</bar>
</foo>
And I wanted to process everything under <foo>, since that tag contains the
data that I am interested in, I'd want to be able to pass around the
<bar>...</bar> elements.
<bar xml...
Content
</bar>
Therefore, attributes would need to be associated with the tag where they
appear, not for the entire DOM.
So lets say that I (hypothetically) want to write a recursive XML parser
that reads data from XML and uses an activerecord object to insert it into a
database (This is a fairly common use case, at least for me).
The pseudoRUBYcode for this would be:
def parse(xml)
if !xml.nil?
parse (xml.child_nodes) # parse all XML elements that are my children
xml.save # Save the XML to the DB
end
end
Since I need to access the type attribute in the xml.save method (so I can
handle HTML and RSS differently, for example), then I will need to pass it
along with the XML elements that make up the child nodes.
I like to think of each XML node as a crude Object, thus all data contained
within that Object should remain with that Object.
Make sense?
Jamie
···
-----Original Message-----
From: ruby-talk-admin@ruby-lang.org [mailto:ruby-talk-admin@ruby-lang.org]
On Behalf Of Trans
Sent: Monday, November 26, 2007 11:38 AM
To: ruby-talk ML
Subject: How to map XML attributes
I'm working on a DSL that maps XML <=> Object, and I'm stuck on where
to store tag attributes. Do attributes belong to the tag or to the
content?
For example:
<foo>
<bar xmlns="http://..." type="html">
HTML Content
</bar>
</foo>
In mapping this to an object, lets say:
foo.bar #=> "HTML Content"
Does is make sense to ask:
foo.bar.attributes #=> { 'xmlns'=>..., 'type'=>... }
or should the attributes be tied to the "attributation" of foo, so:
foo.attributes(:bar) #=> { 'xmlns'=>..., 'type'=>... }
The 'type' attribute makes me think the first makes the most sense,
but the 'xmlns' makes me think the later.
Any insights?
Thanks,
T.