XML — Attributes
Basic Attributes
<book genre="fiction" pages="200">
<title>My Book</title>
</book>
Attribute Rules
- Must be quoted (single or double)
- Cannot contain
<or& - Cannot contain same quote as delimiter
<!-- Correct -->
<name first="John" last="Doe"/>
<!-- Incorrect -->
<name first=John/>
Element vs Attribute
<!-- Using attributes -->
<person name="John" age="30"/>
<!-- Using elements -->
<person>
<name>John</name>
<age>30</age>
</person>
| Use When | Recommendation |
|---|---|
| Metadata about data | Attribute |
| Data itself | Element |
| Multiple values | Element |
| Small simple values | Attribute |
Attribute Defaults
<!ATTLIST stock
currency CDATA "USD"
quantity CDATA #REQUIRED>
| Default | Description |
|---|---|
| Value | Default value |
| #REQUIRED | Must be present |
| #IMPLIED | Optional |
| #FIXED | Always this value |
Mini Practice
- Add attributes to elements
- Choose between attribute and element
- Use both single and double quotes
- Create a document using attributes effectively
Up Next
Continue with Comments — commenting in XML documents.
Related Topics
Frequently Asked Questions about Attributes
What is Attributes in XML?
Attributes is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Attributes?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Attributes.
Why is Attributes important in XML?
Attributes is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.