1

I have recently started learning XML and very much confused about the use of elements and attributes. I have read many articles and some rules of thumb but still unable to get a clear picture of when to use what. For example one good rule of thumb is to use single valued data as attributes and multiple valued data as elements. Another to represent data as elements and meta data as attributes. Some articles say describe several other approaches or rules. I have several questions in mind and will much appreciate if any one can help me in understanding

  1. What if the data is single valued ? Should I make it an attribute or an element. E.g. Marital Status of a person is single valued at the same time its data. What will be a difference in an attribute and element in such a case?

  2. For example Date of birth is also a single valued data but many forums suggested making separate elements for day month and year and some suggested making a date of birth element and day year and month its attributes. What is the difference How will either approach help?

  3. what is more useful for search operations and how ? If I want to design it in such a way that its more useful and efficient for searching what shall I do. For example what difference an element or an attribute of date of birth will make for a search operation I.e. if I want to search people born in 1980 or blah blah.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ashamim
  • 73
  • 5

3 Answers3

2

First, you need to understand that there is no right answer. In many cases it makes little difference. When a choice makes very little difference, it's not worth spending much time on the decision, because the penalty of getting it wrong is not high.

You won't go very far wrong if you adopt the following strategy:

(a) in document-oriented XML, put the user-visible running text in elements, and annotations that aren't intended for the ordinary user to see ("metadata") in attributes.

(b) in data-oriented XML, use elements for everything, with the possible exception of ID attributes.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

if you are a programmer ull understand this better
though the attributes and elements are interchangeable.
when ever i make a XML structure i try to figure out it as a class of some programming language lets say c#
so for all the Fields i make attributes and all the Propertiese i make elements so a class with the following structure

class SomeClass

    {
    int a;
    int b{get; set;}
    }

will be changed by me into

<xml>
<object a="some value"><b>"some value"</b></object>
</xml>
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

I think there are many different interpretations on how you should use both and I've been trying to figure them out too lately. Here's my personal point of view:

  • Attributes are in-tag informations you can use to make a tag "richer", I think at it as if I had to reference a specific html tag to apply it some sort of styling or behaviour, so for instance I'd look for all tags with attribute lang = "ita" and stick a "translate this" function to them, or I can look for all tags with attribute sex="female" and render them pink... the point is a reader can tell wether a text is in english or not, or wether Sarah is a male or a female, but you want to "make the parser know that too" and you can xpath a tag (select a tag) better by attributes than by inner value.

  • Everything else is an element.

Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41