News for hackers. Stuff that matters.
Re:A Brief Defense of XML by ahepp on Friday January 06, 2023 @07:50PM (#34283658)
I often feel confused about when to use tags and when to use attributes. Is there principled guidance on this?
Re:A Brief Defense of XML by chipsa on Friday January 06, 2023 @08:35PM (#34284052)
No? But I think that if you have substructure, like a position or can reasonably have multiples of the thing, it should be a tag.
Re:A Brief Defense of XML by epalm on Friday January 06, 2023 @08:19PM (#34283941)
I generally try to reserve attributes for metadata. That is, data about the data contained in the tag.
Re:A Brief Defense of XML by int_19h on Friday January 06, 2023 @08:47PM (#34284150)
There is none. In general, the main difference is that elements are all ordered wrt each other, while attributes are not. This ought to make the choice simple, except that attributes also cannot contain nested data structures.

A particularly unusual take is to ignore the difference and allow either attributes or child elements as different ways to do the same thing. This is what XAML does; e.g. this:

   <Button Content="Foo" />
is identical to:

   <Button>
       <Button.Content>Foo</Button.Content>
   </Button>
Re:A Brief Defense of XML by dwaite on Friday January 06, 2023 @08:55PM (#34284199)
There is for user-viewable markup-enhanced documents, like SVG and HTML - which is pretty much what you experience with SVG and HTML authorship by hand.

If data should be presented to the user by a user agent that doesn't understand extensions to the markup, it should be using text nodes.

If it is a modification to a subset of a block of text, such as emphasizing a word, it should be a new element.

If it is a block of semantic metadata, you would structure that as a hierarchy of elements with attributes.

Otherwise, it is usually an attribute on an existing element.

SVG is a bit of a zany format in XML because so much of the content is points or encoded data, which get stuffed in some truly epic attributes.

XML wasn't really made for pure data formats, even though shoehorning that in was the majority of the market's effort.

Re:A Brief Defense of XML by magicalhippo on Friday January 06, 2023 @10:14PM (#34284763)
I'd say if it's for general data exchange, I'd shy away from attributes as much as possible. Not all tools handle them well, and XML novices can get confused about their usage.

Using plain tags is so obvious that just about anyone can get it, and in my experience there's no real advantage to using attributes.

At least that's my experience at work, where we use XML as our primary integration format for others that want to deliver data to our system. When going through with a new customer, the few attributes we have almost always cause some kind of issue or misunderstanding.