Beginning XML 4th Edition
Chapter 3: XML Namespaces
Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes The syntax for using namespaces in XML documents What is a URI, URL, and URN
Why Do We Need Namespaces? Sir John Fitzgerald Johansen Doe Vice President of Marketing Resume of John Doe John Doe John’s a great guy, you know?
Using Prefixes Sir John Fitzgerald Johansen Doe Vice President of Marketing Resume of John Doe John Doe John’s a great guy, you know?
So Why Doesn’t XML Just Use These Prefixes? Sir
How XML Namespaces Work Sir
Try It Out Adding XML Namespaces to Your Document
Default Namespaces Sir <person xmlns=“ xmlns:xhtml=“ This is XHTML Default except for the tag Default
Try It Out Default Namespaces in Action
Declaring Namespaces on Descendants Example 1 This is XHTML Example 2 This is XHTML
Try It Out Default Namespaces for Children
Canceling Default Namespaces Jane Doe I’ve worked with Jane Doe for over a year now.
Do Different Notations Make Any Difference? You can fully qualify every name, like this: <pers:person xmlns:pers=" xmlns:xhtml=" This is XHTML Or, you can use one namespace as the default, and just qualify any names from other namespaces, like this: <person xmlns=" xmlns:xhtml=" This is XHTML Or, you can just use defaults everywhere, like this: This is XHTML
Do Different Notations Make Any Difference?
Namespaces and Attributes Example 1 Sir Example 2 Sir
Namespaces and Attributes Do namespaces work the same for attributes as they do for elements?
Try It Out Adding Attributes
What Exactly Are URIs? ProtocolURI
URNs urn:foo:a123,456 Examples Social security numbers Serial numbers NIC mac addresses
Why Use URLs for Namespaces, Not URNs?
RDDL Resource Directory Description Language
When Should You Use Namespaces?
Remember that a namespace is just a “bag of names”—that is, it’s a group of element names that belong together, and that are distinct from element names in other namespaces. The key is the phrase belong together.
Exercise Questions Question 1 In this chapter you saw the following XML document, in which you had to cancel the default namespace: Jane Doe I’ve worked with Jane Doe for over a year now. Assuming that this document is for Wiley’s HR department, create a namespace for employees, and use it in this document. Be sure to keep the XHTML elements in their namespace.
Exercise Questions Question 2 Imagine that Wiley has been going through the employee records and realized that they don’t have a good unique way to identify each employee. Create a global id attribute that can be attached to any XML element in the employee namespace you created earlier. Put this attribute into effect by modifying the XML you created in Question 1, and marking the Jane Doe employee as employee number x125.
Exercise Questions Question 3 Create a new XML file for an employee named Alfred Neuman, with employee number x393. In the notes for Alfred mention that he has worked closely with Jane Doe, being sure to use the element to refer to her.
Try It Out Adding XML Namespaces to Your Document Default Namespaces in Action Default Namespaces for Children Adding Attributes