0

Possible Duplicate:
XML attribute vs XML element

I'm creating an XML document that holds cinemas. I need a way to represent the facilities a certain cinema has, but am uncertain on how to structure it.

Should I have each facility as a node? And the text inside that node would either be a 1 or 0 (true of false)? Or would it be better to have each facility as an attribute or a "facilities" node?

Attributes:

<cinema id="1">
    ...
    <facilities advance_screenings="1" concessions="0" ... >1<facilities>
</cinema>

Nodes:

<facilities>
    <advance_screenings>1</advance_screenings>
    <audio_description>1</audio_description>
    <bar>1</bar>
    <concessions>1</concessions>
    ...
</facilities>

Thanks.

Community
  • 1
  • 1
Nick
  • 1,269
  • 5
  • 31
  • 45
  • @DarinDimitrov: That's not helpful. XML design is just as flexible, and just as important, as code design. – skaffman Feb 25 '12 at 23:14
  • @skaffman, of course, I completely agree with you, and it makes sense when someone provides context about what he is designing. This question for me is completely meaningless as it can be answered with both `use attributes` and `use elements` answers and there's nothing on this Earth that could justify the one or the other answer. – Darin Dimitrov Feb 25 '12 at 23:15
  • 1
    @Nick: Pedantic point of terminology: attributes *are* nodes. You mean attributes vs elements. – skaffman Feb 25 '12 at 23:15

3 Answers3

2

This is largely a matter of preference/style.

However - there are some rules of thumb:

  • Non-primitive types must be elements (you are using the term "node"). You can't have an attribute which represents a complex type - although you can do some pattern matching.

  • 1000 attributes on a single element is probably not a great design - strive for human readability.

  • As a corollary to the above point, avoid 1000 elements as direct descendants of a single element. (Nest/group elements in a logical structure).

Having some feeling for what a "nice structure" is vs. a "bad structure" is a complicated judgement which takes a lot of factors into account - such as the industry/context in which the data is being used, and what it represents. It's similar to class design or probably closer to database design. Sometimes a hack job won't make one iota of difference - other times semantic precision is of primary importance.

You'll only get a good grasp of it through experience.

Steve
  • 31,144
  • 19
  • 99
  • 122
1

Part of the beauty/pain of XML is that these kinds of decisions are entirely up to you.

Attributes are normally self-explanatory - they usually describe some property of the element. Subnodes are more of a sub-data type enclosed by the parent node.

But all of this is subjective and there really is no right answer - it's entirely how you want to structure the document.

adelphus
  • 10,116
  • 5
  • 36
  • 46
0

In this case, they should be sub-elements. To put it in MVC terms, attributes should hold information about the view, sub-elements should hold information about the model.

shawnhcorey
  • 3,545
  • 1
  • 15
  • 17