Tags, Elements and Attributes
An XML document is made up of tags just like HTML. The tags that compose the XML document are known as elements. Elements are used to classify data in an XML document so that the data becomes "self-explanatory". Let's understand what an element is.This code is very much valid in HTML but not in XML. With XML each and every element must have a closing tag.A new paragraph will start now. New paragraph has started.
This mail will not reach the recepient. This mail will reach the recepient.
The first statement will be valid in HTML but XML will give an error. Hence, the correct statement is the second one. It is best to use proper nesting in HTML also to avoid mistakes.This is invalid in XML.
This is valid in XML.
XML attributes define elements. They provide extra information about the element that is not really relevant to the data. Let's take an example of the XML document discussed earlier and add an attribute in it.
Here, we have added an attribute, "category" to the element, "book". One thing to note here is that attribute value is written in quotes-either single or double. Also remember that the attributes are written in the start tag of the element. Hence, an attribute is something that defines the element but is not the data itself. It is used because it might help the programmer or the system to understand the XML document.XML for Dummies Introduction to XML Markup Languages Features of XML XML Syntax Elements must be enclosed in tags Elements must be properly nested
Have a look at the XML document given below:
This is also very much valid and provides the same information.computer XML for Dummies Introduction to XML Markup Languages Features of XML XML Syntax Elements must be enclosed in tags Elements must be properly nested
So what is the difference between an element and an attribute?
The difference between an element and an attribute is in their definition and the way you want your information to be interpreted. There is no such definite rule as to when to use them. Any of them can be used but there are certain drawbacks associated with attributes and hence the reason why a programmer generally avoids it. Some of them are:
Hence, use elements for data and use attributes for information that is not relevant to the data but is needed by the system or the programmer.
