XML elements can have attributes, just like HTML.
Attributes provide additional information about an element.
In HTML, attributes provide additional information about elements:
Attributes often provide information that is not a part of the data. In the example below, the
file type is irrelevant to the data, but can be important to the software that wants to
manipulate the element:
Attribute values must always be quoted. Either single or double quotes can be used. For a
person's sex, the person element can be written like this:
or like this:
If the attribute value itself contains double quotes you can use single quotes, like in this
or you can use character entities:
Take a look at these examples:
In the first example sex is an attribute. In the last, sex is an element. Both examples provide
the same information.
There are no rules about when to use attributes or when to use elements. Attributes are handy in
HTML. In XML my advice is to avoid them. Use elements instead.
The Below three XML documents contain exactly the same information:
A date attribute is used in the first example:
A date element is used in the second example:
An expanded date element is used in the third: (THIS IS MY FAVORITE):
Some of the error with using attributes are:
Attributes are difficult to read and maintain. Use elements for data. Use attributes for
information that is not relevant to the data.
Don't end up like this:
Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements
in much the same way as the id attribute in HTML. This example demonstrates this:
The id attributes above are for identifying the different notes. It is not a part of the note
What I'm trying to say here is that metadata (data about data) should be stored as attributes,
and the data itself should be stored as elements.
Your Query was successfully sent!