Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are,...

22
Chapter 3: XML Namespaces

Transcript of Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are,...

Page 1: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Chapter 3: XML Namespaces

Page 2: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

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

Page 3: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Why Do We Need Namespaces?<?xml version=“1.0”?>

<person>

<name>

<title>Sir</title>

<first>John</first>

<middle>Fitzgerald Johansen</middle>

<last>Doe</last>

</name>

<position>Vice President of Marketing</position>

<résumé>

<html>

<head><title>Resume of John Doe</title></head>

<body>

<h1>John Doe</h1>

<p>John’s a great guy, you know?</p>

</body>

</html>

</résumé>

</person>

Page 4: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Using Prefixes

<?xml version=“1.0”?><pers:person> <pers:name> <pers:title>Sir</pers:title> <pers:first>John</pers:first> <pers:middle>Fitzgerald Johansen</pers:middle> <pers:last>Doe</pers:last> </pers:name> <pers:position>Vice President of Marketing</pers:position> <pers:résumé> <xhtml:html> <xhtml:head><xhtml:title>Resume of John Doe</xhtml:title></xhtml:head> <xhtml:body> <xhtml:h1>John Doe</xhtml:h1> <xhtml:p>John’s a great guy, you know?</xhtml:p> </xhtml:body> </xhtml:html> </pers:résumé></pers:person>

Page 5: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

So Why Doesn’t XML Just Use These Prefixes?

<?xml version=“1.0”?>

<{http://www.wileycom/pers}person>

<{http://www.wileycom/pers}name>

<{http://www.wileycom/pers}title>

Sir

</{http://www.wileycom/pers}title>

<!--etc...-->

Page 6: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

How XML Namespaces Work

<pers:person xmlns:pers=“http://www.wileycom/pers”>

<pers:name>

<pers:title>Sir</pers:title>

</pers:name>

</pers:person>

Page 7: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Default Namespaces

<person xmlns=“http://www.wiley.com/pers"> <name> <title>Sir</title> </name></person>

<person xmlns=“http://www.wiley.com/pers” xmlns:xhtml=“http://www.w3.org/1999/xhtml”> <name/> <xhtml:p>This is XHTML</xhtml:p></person>

Default except for the <p> tag

Default

Page 8: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Declaring Namespaces on Descendants

Example 1

<person xmlns=“http://www.wileycom/pers”> <name/> <xhtml:p xmlns:xhtml=“http://www.w3.org/1999/xhtml”> This is XHTML </xhtml:p></person>

Example 2

<person xmlns=“http://www.wileycom/pers”> <name/> <p xmlns=“http://www.w3.org/1999/xhtml”>This is XHTML</p></person>

Page 9: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

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

Page 10: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Canceling Default Namespaces

<employee> <name>Jane Doe</name> <notes> <p xmlns=“http://www.w3.org/1999/xhtml”>I’ve worked with <name xmlns=““>Jane Doe</name> for over a <em>year</em> now.</p> </notes></employee>

Page 11: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Do Different Notations Make Any Difference?

You can fully qualify every name, like this: <pers:person xmlns:pers="http://www.wiley.com/pers" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <pers:name/> <xhtml:p>This is XHTML</xhtml:p></pers:person>Or, you can use one namespace as the default, and just

qualify any names from other namespaces, like this:<person xmlns="http://www.wiley.com/pers" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <name/> <xhtml:p>This is XHTML</xhtml:p></person>Or, you can just use defaults everywhere, like this:<person xmlns="http://www.wiley.com/pers"> <name/> <p xmlns="http://www.w3.org/1999/xhtml">This is XHTML</p></person>

Page 12: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Namespaces and Attributes

Example 1

<person xmlns=“http://www.wileycom/pers”> <name id=“25”> <title>Sir</title> </name></person>

Example 2

<a:person xmlns:a=“http://www.wileycom/pers”> <a:name a:id=“25”> <a:title>Sir</a:title> </a:name></a:person>

Page 13: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Try It Out

Namespaces in Action

Page 14: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

What Exactly Are URIs?

http://www.wileycom/default/home.htm

Protocol URI

Page 15: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

URNs

urn:foo:a123,456

Examples•Social security numbers•Serial numbers•NIC mac addresses

Page 16: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Why Use URLs for Namespaces, Not URNs?

Page 17: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

RDDL• Resource Directory Description

Language

Page 18: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

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.

Page 19: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Exercise QuestionsQuestion 1

In this chapter you saw the following XML document, in which you had to cancel the default namespace:

<employee> <name>Jane Doe</name> <notes> <p xmlns="http://www.w3.org/1999/xhtml">I’ve worked with <name xmlns="">Jane Doe</name> for over a <em>year</em> now.</p> </notes></employee>

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.

Page 20: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

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.

Page 21: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

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 <name> element to refer to her.

Page 22: Chapter 3: XML Namespaces. Chapter 3 Objectives Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes.

Try It Out

•Adding XML Namespaces to Your Document•Default Namespaces in Action•Default Namespaces for Children•Adding Attributes