XML — Namespaces
Why Namespaces?
When combining XML documents, element names may conflict:
<!-- Conflicting names -->
<table>
<tr> <!-- Table row or table? -->
<td>Data</td>
</tr>
</table>
Namespace Syntax
<root xmlns:prefix="URI">
<prefix:child>Content</prefix:child>
</root>
Default Namespace
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<p>Content</p>
</body>
</html>
Practical Example
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:book="http://example.com/books"
xmlns:person="http://example.com/people">
<book:title>My Book</book:title>
<person:name>John Doe</person:name>
</catalog>
Common Namespaces
| Prefix | URI |
|---|---|
| xmlns | http://www.w3.org/2000/xmlns/ |
| xml | http://www.w3.org/XML/1998/namespace |
| xs | http://www.w3.org/2001/XMLSchema |
| xsl | http://www.w3.org/1999/XSL/Transform |
Mini Practice
- Create a namespace
- Use a default namespace
- Combine two namespaces
- Reference namespaced elements
Up Next
Continue with Encoding — character encoding in XML.
Related Topics
Frequently Asked Questions about Namespaces
What is Namespaces in XML?
Namespaces 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 Namespaces?
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 Namespaces.
Why is Namespaces important in XML?
Namespaces is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.