An XML attribute is a name-value pair written in an element's start tag. Attributes are useful for compact metadata or identifiers associated with an element.
<address type="residential">
<door_no>234-ac</door_no>
<street>Great Wall</street>
<city>Alabama</city>
</address>
Here, type is the attribute name and residential is its value.
& for an ampersand in ordinary attribute data.There is no universal rule that all “data” must be elements and all “metadata” must be attributes. The best structure depends on the XML vocabulary and how the data will be processed.
Attributes work well for compact values such as identifiers, categories, flags or types. Child elements are often easier when values can repeat, contain structured content, or need to grow into a richer structure.
<member id="M42">
<name>Ravi</name>
<address type="office">...</address>
</member>
Open sample XML file with attributes
Must XML attribute values use quotes?
Yes. Attribute values must be quoted.
Can two attributes on one element have the same name?
No. Duplicate attribute names on the same element make the XML not well-formed.
Should all values be XML attributes?
No. Choose attributes or child elements according to the structure and semantics of the XML vocabulary.
What is XML? XML file structure
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.