XML Attributes
Adding extra information to XML elements
🏷️ What are XML Attributes?
XML attributes provide additional information about elements. They are name-value pairs placed inside the opening tag, helping to describe or modify element properties without adding extra child elements.
<!-- Attribute example -->
<book id="101" language="English">
<title>Learning XML</title>
</book>
Explanation:
id="101" and language="English" are attributes that add metadata to the book element.
Key Attribute Concepts
Syntax
Attributes use name="value" format
<person age="25"></person>
Quotes
Values must be in quotes
<item price="19.99"></item>
Multiple
Elements can have many attributes
<img src="pic.jpg" width="100" height="80"/>
Unique
No duplicate attribute names
<!-- Wrong: id appears twice -->
<book id="1" id="2"></book>
🔹 Basic Attribute Usage
Attributes provide metadata about elements. They're perfect for IDs, references, and properties that describe rather than contain content.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="B001" category="Fiction">
<title>The Great Novel</title>
<author>John Doe</author>
</book>
<book id="B002" category="Science">
<title>Physics Basics</title>
<author>Jane Smith</author>
</book>
</library>
Explanation:
Each book has id and category attributes for identification and classification.
🔹 Attributes vs Elements
You can store data as attributes or child elements. Both approaches work, but attributes are better for metadata while elements are better for actual content.
<!-- Using Attributes -->
<person name="Alice" age="30" city="New York"/>
<!-- Using Elements -->
<person>
<name>Alice</name>
<age>30</age>
<city>New York</city>
</person>
<!-- Mixed Approach (Recommended) -->
<person id="P001">
<name>Alice</name>
<age>30</age>
<city>New York</city>
</person>
Best Practice:
Use attributes for IDs and metadata, elements for actual data content.
🔹 Attribute Rules
XML attributes must follow specific rules to be valid. Understanding these rules helps you write correct XML documents.
Important Rules:
- Attribute values must always be quoted (single or double quotes)
- Attribute names are case-sensitive
- Each attribute can appear only once per element
- Attribute names cannot contain spaces
- Attributes cannot contain multiple values
<!-- Correct -->
<product id="123" name="Laptop" price="999.99"/>
<!-- Wrong: No quotes -->
<product id=123 name=Laptop/>
<!-- Wrong: Duplicate attribute -->
<product id="123" id="456"/>
<!-- Wrong: Space in name -->
<product product id="123"/>
🔹 Special Characters in Attributes
When attribute values contain special characters like quotes or angle brackets, you must use entity references to represent them properly.
<!-- Using entity references -->
<quote text="He said "Hello" to me"/>
<!-- Less than and greater than -->
<math expression="5 < 10 && 10 > 5"/>
<!-- Apostrophe in single-quoted attribute -->
<message text='It's a beautiful day'/>
Common Entity References:
< = < | > = > | & = & | " = " | ' = '