From: Trans Date: 2007-11-27T09:07:40+09:00 Subject: Re: How to map XML attributes On Nov 26, 3:25 pm, Phrogz wrote: > On Nov 26, 9:37 am, Trans 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: > > > > > > > HTML Content > > > > > > > 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.: > > ... > > 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.