Title: Chapter 3: XML Namespaces
1Chapter 3 XML Namespaces
2Chapter 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
3Why Do We Need Namespaces?
lt?xml version1.0?gt ltpersongt ltnamegt
lttitlegtSirlt/titlegt ltfirstgtJohnlt/firstgt
ltmiddlegtFitzgerald Johansenlt/middlegt
ltlastgtDoelt/lastgt lt/namegt ltpositiongtVice
President of Marketinglt/positiongt ltrésumégt
lthtmlgt ltheadgtlttitlegtResume of John
Doelt/titlegtlt/headgt ltbodygt lth1gtJohn
Doelt/h1gt ltpgtJohns a great guy, you
know?lt/pgt lt/bodygt lt/htmlgt
lt/résumégt lt/persongt
4Using Prefixes
- lt?xml version1.0?gt
- ltperspersongt
- ltpersnamegt
- ltperstitlegtSirlt/perstitlegt
- ltpersfirstgtJohnlt/persfirstgt
- ltpersmiddlegtFitzgerald Johansenlt/persmiddlegt
- ltperslastgtDoelt/perslastgt
- lt/persnamegt
- ltperspositiongtVice President of
Marketinglt/perspositiongt - ltpersrésumégt
- ltxhtmlhtmlgt
- ltxhtmlheadgtltxhtmltitlegtResume of John
Doelt/xhtmltitlegtlt/xhtmlheadgt - ltxhtmlbodygt
- ltxhtmlh1gtJohn Doelt/xhtmlh1gt
- ltxhtmlpgtJohns a great guy, you
know?lt/xhtmlpgt - lt/xhtmlbodygt
- lt/xhtmlhtmlgt
- lt/persrésumégt
- lt/perspersongt
5So Why Doesnt XML Just Use These Prefixes?
URI's and URN's are used for uniqueness
- lt?xml version1.0?gt
- lthttp//www.wileycom/perspersongt
- lthttp//www.wileycom/persnamegt
- lthttp//www.wileycom/perstitlegt
- Sir
- lt/http//www.wileycom/perstitlegt
- lt!--etc...--gt
6How XML Namespaces Work
Together they are guaranteed to be unique
- ltpersperson xmlnspershttp//www.wileycom/pers
gt - ltpersnamegt
- ltperstitlegtSirlt/perstitlegt
- lt/persnamegt
- lt/perspersongt
7Default Namespaces
- ltperson xmlnshttp//www.wiley.com/pers"gt
- ltnamegt
- lttitlegtSirlt/titlegt
- lt/namegt
- lt/persongt
- ltperson xmlnshttp//www.wiley.com/pers
- xmlnsxhtmlhttp//www.w3.org/1999/xhtml
gt - ltname/gt
- ltxhtmlpgtThis is XHTMLlt/xhtmlpgt
- lt/persongt
Default
Default except for the ltpgt tag
8Declaring Namespaces on Descendants
- Example 1
- ltperson xmlnshttp//www.wileycom/persgt
- ltname/gt
- ltxhtmlp xmlnsxhtmlhttp//www.w3.org/1999/xht
mlgt - This is XHTML
- lt/xhtmlpgt
- lt/persongt
- Example 2
- ltperson xmlnshttp//www.wileycom/persgt
- ltname/gt
- ltp xmlnshttp//www.w3.org/1999/xhtmlgtThis is
XHTMLlt/pgt - lt/persongt
9Namespaces
- 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
10Canceling Default Namespaces
- ltemployeegt
- ltnamegtJane Doelt/namegt
- ltnotesgt
- ltp xmlnshttp//www.w3.org/1999/xhtmlgtIve
worked - with ltname xmlnsgtJane Doelt/namegt for over
a ltemgtyearlt/emgt - now.lt/pgt
- lt/notesgt
- lt/employeegt
11Do Different Notations Make Any Difference?
- You can fully qualify every name, like this
- ltpersperson xmlnspers"http//www.wiley.com/pers
" - xmlnsxhtml"http//www.w3.org/1999/x
html"gt - ltpersname/gt
- ltxhtmlpgtThis is XHTMLlt/xhtmlpgt
- lt/perspersongt
- Or, you can use one namespace as the default, and
just qualify any names from other namespaces,
like this - ltperson xmlns"http//www.wiley.com/pers"
- xmlnsxhtml"http//www.w3.org/1999/xhtml"
gt - ltname/gt
- ltxhtmlpgtThis is XHTMLlt/xhtmlpgt
- lt/persongt
- Or, you can just use defaults everywhere, like
this - ltperson xmlns"http//www.wiley.com/pers"gt
- ltname/gt
- ltp xmlns"http//www.w3.org/1999/xhtml"gtThis is
XHTMLlt/pgt - lt/persongt
12Namespaces and Attributes
- Example 1
- ltperson xmlnshttp//www.wileycom/persgt
- ltname id25gt
- lttitlegtSirlt/titlegt
- lt/namegt
- lt/persongt
- Example 2
- ltaperson xmlnsahttp//www.wileycom/persgt
- ltaname aid25gt
- ltatitlegtSirlt/atitlegt
- lt/anamegt
- lt/apersongt
13Try It Out
Namespaces in Action
14What Exactly Are URIs?
- http//www.wileycom/default/home.htm
Protocol
URI
URL Protocol URI
15URNs
urnfooa123,456
- Examples
- Social security numbers
- Serial numbers
- NIC mac addresses
16Why Use URLs for Namespaces, Not URNs?
XML uses URLs not URNs... by the way!
17RDDL
- Resource Directory Description Language
It's the human-readable stuff in the namespaces.
18When Should You Use Namespaces?
- Remember that a namespace is just a bag of
namesthat is, its 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.
19Exercise Questions
Question 1 In this chapter you saw the following
XML document, in which you had to cancel the
default namespace ltemployeegt ltnamegtJane
Doelt/namegt ltnotesgt ltp xmlns"http//www.w
3.org/1999/xhtml"gtIve worked with ltname
xmlns""gtJane Doelt/namegt for over a
ltemgtyearlt/emgt now.lt/pgt lt/notesgt lt/employeegt
Assuming that this document is for Wileys HR
department, create a namespace for employees, and
use it in this document. Be sure to keep the
XHTML elements in their namespace.
20Exercise Questions
Question 2 Imagine that Wiley has been going
through the employee records and realized that
they dont 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.
21Exercise 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 ltnamegt element to refer to her.
22Try It Out
- Adding XML Namespaces to Your Document
- Default Namespaces in Action
- Default Namespaces for Children
- Adding Attributes