Chapter 3: XML Namespaces - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 3: XML Namespaces

Description:

Chapter 3: XML Namespaces – PowerPoint PPT presentation

Number of Views:144
Avg rating:3.0/5.0
Slides: 23
Provided by: Lind3336
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3: XML Namespaces


1
Chapter 3 XML Namespaces
2
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

3
Why 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
4
Using 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

5
So 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

6
How XML Namespaces Work
Together they are guaranteed to be unique
  • ltpersperson xmlnspershttp//www.wileycom/pers
    gt
  • ltpersnamegt
  • ltperstitlegtSirlt/perstitlegt
  • lt/persnamegt
  • lt/perspersongt

7
Default 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
8
Declaring 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

9
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

10
Canceling 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

11
Do 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

12
Namespaces 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

13
Try It Out
Namespaces in Action
14
What Exactly Are URIs?
  • http//www.wileycom/default/home.htm

Protocol
URI
URL Protocol URI
15
URNs
urnfooa123,456
  • Examples
  • Social security numbers
  • Serial numbers
  • NIC mac addresses

16
Why Use URLs for Namespaces, Not URNs?
XML uses URLs not URNs... by the way!
17
RDDL
  • Resource Directory Description Language

It's the human-readable stuff in the namespaces.
18
When 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.

19
Exercise 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.
20
Exercise 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.
21
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 ltnamegt element to refer to her.
22
Try It Out
  • Adding XML Namespaces to Your Document
  • Default Namespaces in Action
  • Default Namespaces for Children
  • Adding Attributes
Write a Comment
User Comments (0)
About PowerShow.com