Presentation on theme: "Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes."— Presentation transcript:
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?
Default Namespaces Sir This is XHTML Default except for the tag Default
Declaring Namespaces on Descendants Example 1 This is XHTML Example 2 This is XHTML
Namespaces Look at Example 1 on p 73 of the book. Then look at example 1 on p 79 Both documents have the same semantic content
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: This is XHTML Or, you can use one namespace as the default, and just qualify any names from other namespaces, like this: This is XHTML Or, you can just use defaults everywhere, like this: This is XHTML
Namespaces and Attributes Example 1 Sir Example 2 Sir
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
Your consent to our cookies if you continue to use this website.