[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to map XML attributes

Trans

11/26/2007 4:38:00 PM

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.

3 Answers

Phrogz

11/26/2007 8:23:00 PM

0

On Nov 26, 9:37 am, Trans <transf...@gmail.com> wrote:
> 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.

I don't share your opinion on xmlns implying the latter. All
attributes are attributes of an element; the first totally makes
sense.

How would you get the attributes of the rootmost element, e.g.:
<root id="page3">
...
</root>
Just attributes( :root )? Bleah, I say.


If I were writing an XML-like DOM, I'd use dot notation (method call)
for the child axis and [] notation for the attributes axis.
foo.bar[:type] #=> ...

Trans

11/27/2007 12:08:00 AM

0



On Nov 26, 3:25 pm, Phrogz <phr...@mac.com> wrote:
> On Nov 26, 9:37 am, Trans <transf...@gmail.com> wrote:
>
>
>
> > 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.
>
> I don't share your opinion on xmlns implying the latter. All
> attributes are attributes of an element; the first totally makes
> sense.
>
> How would you get the attributes of the rootmost element, e.g.:
> <root id="page3">
> ...
> </root>
> Just attributes( :root )? Bleah, I say.
>
> If I were writing an XML-like DOM, I'd use dot notation (method call)
> for the child axis and [] notation for the attributes axis.
> foo.bar[:type] #=> ...

Thanks guys,

I've taken your advice(s).

The reason I thought maybe to do it otherwise is b/c of a basic coding
principle --eg. the object referenced by an instance var doesn't know
the instance var's name. In mapping XML elements to Ruby objects I
felt like I was violating that principle if I carried the name along
with the element's body, and subsequently the attributes too.

I think I have a suitable compromise though. I attached the name and
attributes to an "Element" object which delegates via method_missing
to the underlying body object --which itself has no idea what the name
and attributes are.

Make sense?

T.

Mark T

11/27/2007 12:32:00 AM

0

http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Sc...

This is such a good article.

Any attempt at or>xml mapping could be caught/fraught with issues.
Another boundary is that of the distinction of data & information.
Data is raw. Information is meaningful data.
Meaningful is contextual.
Context is a matter of perception.
Perception is not always reality.
</end.of.suck.eggs.yak>

Not that I have any real answer to this.
Though I have noticed that to some, xml is refreshingly raw.
For instance I understand that some airport baggage handling software uses xml.
It's data, it shifts the bag, it expires.

One possible reason to map xml may be to avoid the knarly world of REXml.
Which is actually pretty good in a martial sort of way.
Avoidance does not mean not using.
http://xml-simple.ruby...
This wraps around Rex in a Ruby sort of way.
Not ORMing.
Though even when using a wrapper like this, usually there will be a
0-day when one has to go under the hood.

http://www.ibm.com/developerworks/xml/library/x-matt...
There is another article in alphaworks, canna find now.
It described a wrapper technique using method_notfound.
There was trouble with the '.' occasionally found in the element name..
(Just read your post Trans).

xmlification has been discontinued?
http://newsattic.com/d/hl/xmlific...

Advantages & disadvantages:
XML is an adv. when debugging, being simple.
Mapping via the xml may be better than via the DTD as flexibility
could be a adv.
In that when the data structure is not totally defined, the mapping
regenerates a 'schema'.
In the arena of impedance mis-match, 2 many mode-shifts betwixt disk &
browser can send you to the corner shop.
When moving towards an object design there is a mapping back to get to
a structure which can be delivered to an xml-database.
Going raw to BDBxml is a major performance advantage.

Design can also factor in the cost of debugging.
If this is to be a tool of general purpose, then it has a high-hit factor.
Would the mapper be suitable if there were to be two storage regimes?
One for development (easy to debug), another for deployment (performance).

How many frameworks are ultimately involved?
xml implies browser interaction?
xml being manipulated by ECMAscript?

These are of course, questions I've been rattling with recently.

Is Ruby too much fun?

MarkT