Why does XML have attributes?
Have you ever wondered why there are also attributes in XML and not just elements, causing many never-ending "element vs attribute" discussions?
And why is XML so redundant - repeating the tag name in the ending tag, thus making the document much larger?
I asked Charles Goldfarb, the author of SGML - the original format from the 1960s (already had <tags> and attributes) from which XML and HTML were derived.
He was very kind to reply to me. Enjoy:
- In <elem>text</elem>, why repeat "elem" for the second time? In fact, there is an option in SGML that allows you to omit the element-type name from an end-tag, but it is rarely supported. (In XML the option does not exist at all.) The name is there primarily for the convenience of humans debugging the markup, as there are large document types, such as aircraft maintenance manuals, where elements can nest very deeply.
- Why are there attributes in SGML? One could express the same just with elements. In fact, one could just as easily express the same just with attributes! The basic notion is that elements represent objects while attributes represent their properties. However, the property "content" is given a convenient syntax -- everything between the start-tag and matching end-tag -- which makes it simpler to express content hierarchy by simply nesting elements. In a nutshell, if information is part of the content of the document, represent it as an element. If it is some other property, represent it as an attribute. I wish you the best of luck with your studies. Charles Goldfarb -- ©2008 Charles F. Goldfarb * www.xmlhandbook.com * www.xmlbooks.com The XML Handbook?* 5th Edition ISBN 0-13-049765-7 * 100,000 in print! --