20
XML 2nd EDITION Tutorial 2 Working With Namespaces

XML 2nd EDITION Tutorial 2 Working With Namespaces

Embed Size (px)

Citation preview

Page 1: XML 2nd EDITION Tutorial 2 Working With Namespaces

XML2nd EDITION

Tutorial 2Working With Namespaces

Page 2: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPCombining XML Vocabularies• A document that combines several

vocabularies is known as a compound document

Page 3: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPWorking With Namespaces• Name collision occurs when elements from two or

more documents share the same name.

• Name collision is not a problem if you are not concerned with validation. The document content only needs to be well-formed.

• However, name collision will keep a document from being validated.

Page 4: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPName CollisionThis figure shows name collision

Partsvocabulary

Modelvocabulary

Page 5: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPDeclaring a Namespace• A namespace is a defined collection of element and

attribute names.

• Names that belong to the same namespace must be unique. Elements can share the same name if they reside in different namespaces.

• Namespaces must be declared before they can be used.

Page 6: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXP

6

Declaring a Namespace• A namespace can be declared in the prolog or as an

element attribute. The syntax for an attribute used to declare a namespace in the prolog is:

xmlns:prefix=“URI”

where URI is a Uniform Resource Identifier that assigns a unique name to the namespaceprefix is a string of letters

“Short hand name” Full, unique name

Page 7: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPDeclaring a Namespace• For example,

xmlns:mod=“http://jacksonelect.com/models”

declares a namespace with the prefix “mod” and the URI

http://jacksonelect.com/models

• The URI is not a Web address. A URI (Uniform Resource Identifier) identifies a physical or an abstract resource.

URI (Uniform Resource Identifier)

URL (Uniform Resource Locator)Physical Resource: file, a Web page, or an e-

mail address

URN (Uniform Resource Name)Persistent name for a resource

Page 8: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPURIs, URLs, and URNs• URN (Universal Resource Name) is a persistent

resource identifier, meaning the user need only know the name of a resource.

• URNs take the form: urn:NID:NSSNID = namespace identifierNSS = text string specific to that the namespace

• Example– urn: isbn: 0-619-01969-7

Page 9: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPApplying a Namespace to an Element• Use prefix instead of the URI to qualify element

names• General form:

<prefix:element> content</prefix:element>

• For examplexmlns:mod=http://jacsonelect.com/models

<mod:title>Laser4C (PR205) </mod:title>

Page 10: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPApplying a Namespace to an Element• Prefixed names are called qualified names• Example:

<mod:title>Laser4C (PR205) </mod:title>

• A name without a namespace prefix is called an unqualified name.

• Example: <title>Laser4C (PR205) </title>

Page 11: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPDeclaring a Namespace as an Element Attribute• For example, the code:

<mod:model xmlns:mod=http://jacksonelect.com/modes ><mod:title>Laser4C (PR205)</mod:title><mod:description>Entry level color laser printer</mod:description><mod:type>color laser</mod:type><mod:ordered>320</mod:ordered><mod:parts list="chx201,fa100-5,eng005-2,cbx-450V4,tn01-53" />

</mod:model>

• …applies the namespace http://jacksonelect.com/modelsto the model element and all of its child elements.

Page 12: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPDeclaring a Default Namespace• You can specify a default namespace by omitting the

prefix in the namespace declaration. • Example:

<model xmlns=http://jacksonelect.com/modes ><title>Laser4C (PR205)</title><description>Entry level color laser printer</description><type>color laser<type><ordered>320</ordered><parts list="chx201,fa100-5,eng005-2,cbx-450V4,tn01-53" />

</model>

• All elements including the model element, are considered to be part of the name space

:mod removed

Page 13: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPUsing Namespaces with Attributes• Attributes, like elements, can become qualified by adding the namespace

prefix to the attribute name. The syntax is: <element prefix:attribute = “value”> … </element>

• For example:<mod:model xmlns:mod=http://jacksonelect.com/models

mod:id=“pr205”> ….</mod:model>

• Attributes inherit their namespace from their parent element:

• For example:<mod:model

xmlns:mod=http://jacksonelect.com/models id=“pr205”>

…..</mod:model>

Page 14: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPAdding a Namespace to a Style Sheet: Declaring a Namespace• To declare a namespace in a style sheet, you add the

following rule to the style sheet file@namespace prefix url(uri);

Where prefix is the namespace prefix and uri is the URI of the namespace

• Example:@namespace mod url(http://jacksonelect.com/models)

Page 15: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPApplying a Namespace to a Selector• Once you’ve declared a namespace in a style sheet, you can

associate selectors with that namespace using the syntax:prefix|selector {attribute1:value1; attribute2:value2;

…}

• For example:mod|title {width: 150px}

• You also can use the wildcard symbol (*) to apply a style to any element within a namespace or to elements across different namespaces

Page 16: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPApplying a Namespace to a Selector• You also can use the wildcard symbol (*) to apply a

style to any element within a namespace or to elements across different namespaces

mod | title {width: 15px}applies width style to all title elements in the models namespace

mod | * {font-size: 12pt}applies the font-size style to any element within the models namespace

title {width: 15pt}applies a width of 150 pixels to any element named title

• Not supported by Firefox, Opera, and Netscape

Page 17: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPCombining Standard Vocabularies• Standard vocabularies may be combined

within single documents

Page 18: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPA compound XHTML and MathML document

Page 19: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPXHTML

Page 20: XML 2nd EDITION Tutorial 2 Working With Namespaces

XPXPXPXPXPSummary• Combining vocabularies in a single document can

result in name collisions• Use namespaces to resolve collisions• Namespaces use URI to provide unique identification

for each vocabulary in a document• CSS style sheets can be modified to accommodate

namespaces• You can create an XHTML document that combines

features from XHTML and other XML vocabularies