XML attributes can be defined as the extra information given by the coder/programmer about his tags. These attributes can be
given by the side of the tag names.
- <student college=”Delhi College of Engg”>
this is a student tag that also contains an attribute by the name of college. The attributes value is “Delhi College of
Engg”. Remember to always enclose the attributes value in inverted commas (“”). This is the only rule for using
attributes in XML.
But, I like many other XML developers would ask you rather to avoid using the attributes. Attributes are not included
in the XML data. Use elements instead.
- <student id=”10234″>
instead we can always use,
Thus, the above two tags are giving the same information but in a different approach. I would prefer the second one
(it’s my choice !!).
Moreover, some things that you can enjoy with tags and elements and not with attributes are:
- attributes cannot contain multiple values (elements can as they can have a many tags in one tag)
- attributes cannot contain tree structures (elements can as the XML elements exist in tree structures )
- attributes are not easily expandable (for future changes, and elements can be extended very easily)