1

What is the preffered version of building XML tags for returning a list of name/value pais?

<variables>
  <item>ok</item>
  <another>123</another>
  <catch>me</catch>
</variables>

or

<variables>
  <variable name="item" value="ok"/>
  <variable name="another" value="123"/>
  <variable name="catch" value="me"/>
</variables>

I think the second form is better, but are there any official W3C recommendations or "desing patterns"?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ManieQ
  • 330
  • 2
  • 8

1 Answers1

1

I don't think there is any "official" way of doing this. For me personally, though, it depends on what is the meaning of these elements/variables. Are they supposed to behave fundamentally differently from one another? Or are their function the same and only the parameters are different?

In your example, it seems their function is the same - they are just variables. Their parameters are different - the name and the value. Therefore, I would say that the second form is better, since you want to handle them all in the same way - probably load them into some kind of map or the like, right?

On the other hand, if their function was supposed to be different, then I would suggest to use different tag names for each. For example:

<post>
    <content>Abc</content>
    <comment>Xyz</comment>
</post>

Is definitely better than

<post>
    <text type="content">Abc</text>
    <text type="comment">Xyz</text>
</post>

Since content and comment are fundamentally different entities that you probably want to handle differently.

So all in all it is pretty similar to OOP programming. Would you create a separate class for each variable? I don't think so, you would just create a single class named "Variable" and instantiate it with different constructor arguments. On the other hand, if the logic was different, like in the second example, you would probably prefer to create a separate class (possibly with different fields) for storing contents and for comments.

Seramme
  • 1,340
  • 7
  • 9
  • I see... So for entities like name/value variables (where even a name cannot be predicted) syntax should be preffered. But if an entity like is a common/obligatory part of object/element, it could be better to use it as a tag. Am I OK? – ManieQ Jan 14 '12 at 14:35
  • I would say so, yes. Just ask yourself if you want to interpret/process the entities in the same, or in a different way. There could be a valid case where the content/comment example could use the variable approach too: for example, if you handle both comments and posts in exactly the same way and the only thing they differ is the icon you mark them with (unlikely scenario, but possible). – Seramme Jan 14 '12 at 14:38