9
Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School — Cambridge, MA The Web Wizard’s Guide to XML by Cheryl M. Hughes

Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Embed Size (px)

Citation preview

Page 1: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-1

Created by Cheryl M. Hughes, Harvard University Extension School — Cambridge, MA

The Web Wizard’s Guide to XML by Cheryl M. Hughes

Page 2: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-2

CHAPTER 5Namespaces in XML

Page 3: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-3

Overview of Namespaces

A namespace is a group, or set, of element and attribute names that belong to or describe a document type

Each name in a namespace must be unique within that namespace

A naming collision occurs when an element name has two different meanings within a document

XML namespaces are used to avoid naming collisions and to assign elements to different groupings within a document

Page 4: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-4

Naming Collision Example

1 <catalog>

2 <course>

3 <title>Introduction to XML</title>

4 <professor>Jane Smith</professor>

5 <meeting_time>Monday, 5:30-7:30</meeting_time>

6 <description>This course covers the basics of XML…</description>

9 <title>The Web Wizard’s Guide to XML</title>

10 <author>C Hughes</author>

11 <publishing_company>Addison Wesley</publishing_company>

14 </course>

15 </catalog>

Page 5: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-5

Namespace Syntax

Namespaces must be declared before they are used Namespaces are declared on elements in the start tag Two types of namespace declarations:

Default: xmlns=“URI”xmlns=“http://www.w3c.org/1999/xlink”

Prefixed: xmlns:prefix=“URI”xmlns:xlink=“http://www.w3c.org/1999/xlink”

Prefixed namespaces are referred to as “qualified names” Both default and prefixed declarations can exist within a

single document

Page 6: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-6

Uniform Resource Identifier (URI)

URI’s can take two forms: Uniform Resource Locator (URL)

Example: http://www.w3c.org/1999/xlink Uniform Resource Name (URN)

Example: urn:local.gov:book Either form of URI can be used with both

default and prefixed namespaces

Page 7: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-7

Scope of a Namespace

The “scope” of a namespace determines which elements can belong to a namespace

Namespace scope cannot ascend above the element it is declared on within the document hierarchy

Namespaces can be declared on sibling and child elements of the element on which it is declared

When namespaces are declared on elements outside of the proper hierarchy, and “out-of-scope” error occurs

Page 8: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-8

Default Namespace Example

1 <newsletter xmlns=”http://chughes.com/firstnamespace”>

2 <name>ACME Company Newsletter</name>

3 <article>

4 <title>Company Employees Participate in 5K Run</title>

5 <author>J. Fraser</author>

6 </article>

7 <issue>July, 2001</issue>

8 </newsletter>

All elements in this document belong to the http://chughes.com/firstnamespace namespace

Page 9: Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide

Copyright © 2003 Pearson Education, Inc. Slide 5-9

Prefixed Namespace Example

1 <newsletter xmlns=”http://chughes.com/firstnamespace”>

2 <name>ACME Company Newsletter</name>

3 <flag:article xmlns:flag=”http://chughes.com/secondnamespace”>

4 <flag:title>Company Employees Participate in 5K Run</flag:title>

5 <author>J. Frasier</author>

6 </flag:article>

7 <issue>July, 2001</issue>

8 </newsletter>

Only the <article> and <title> elements belong to the http://chughes.com/secondnamespace namespace because they include the prefix “flag”