The best XML elements and attributes comparison Tutorial In 2024, In this tutorial you can learn Use elements vs. attributes,I like the way,Avoid using attributes?,Exception to a rule of property,
In XML, and there are no provisions when to use attributes, and when to use child elements.
Data can be stored in child elements or attributes.
Let's look at these examples:
In the first example, "sex" is an attribute. In one example, back in, "sex" is a sub-element. But both provide the same information.
No special provisions for when to use attributes, and when to use child elements. My experience is that the use of multi-attribute in HTML heavy, but in XML, the use of sub-elements, it will feel more like data.
I like to store data in child elements
The following three XML documents contain exactly the same information:
This example uses a "date" attribute:
This example uses a "date" element:
This example uses the extension "date" element: (This is my favorite way):
You should avoid using attributes?
Some attribute has the following problems:
If you use the property as a data container, the resulting XML document will be difficult to read and maintain. Try to useelements to describe data.to describe data. Data is only providing irrelevant information when we recommend the use of property.
Do not end like this (which is not XML should be used):
There is always another rule
Rules on property I have an exception.
Sometimes I use elements specified ID. These applications can be used as ID NAME attribute or ID to access XML elements in HTML in the same situation a lot. The following example illustrates this approach:
XML documents in the above examples, the ID of only one counter, or a unique identifier to identify the different notes, rather than as part of the data.
I want to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.