tutorial 12

32
1 Tutorial 12 Working with Namespaces Combining XML Vocabularies in a Compound Document

Upload: chill

Post on 05-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Tutorial 12. Working with Namespaces Combining XML Vocabularies in a Compound Document. Combining XML Vocabularies. A document that combines several vocabularies is known as a compound document. Working with Namespaces. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tutorial 12

1

Tutorial 12

Working with Namespaces

Combining XML Vocabularies in a

Compound Document

Page 2: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

2

Combining XML Vocabularies

• A document that combines several vocabularies is known as a compound document

Page 3: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

3

Working with Namespaces

• Name collision occurs when elements from different XML vocabularies use the same name within a compound document

• 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: Tutorial 12

Name Collision

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

4

Page 5: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

5

Working with Namespaces

• 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: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

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 namespace, and prefix is a string of letters that associates each element or attribute in the document with the declared namespace.

Page 7: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

7

Declaring a Namespace

• For example,

<mode1 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 identifies a physical or an abstract resource.

Page 8: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

8

Understanding URIs

• The purpose of a URI is simply to provide a unique string of characters that identify a resource. A URL is one type of URI.

• Although the URI doesn’t actually need to point to a real site on the Web, it is often helpful to place documentation at the site identified by the URI so that users can go there to learn more about the XML vocabulary being referenced

Page 9: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

9

Applying a Namespace to an Element

• After you declare a namespace, you have to indicate which elements in the document belong to that namespace.

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

• Here, prefix is the namespace prefix and element is the local part of the element name.

Page 10: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

10

Applying a Namespace to an Element

• Prefixed names are called qualified names and an element name without a namespace prefix is called an unqualified name.

• Qualified names can be added to a document using code entered directly into the document.

Page 11: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

11

Applying a Namespace to an Element

• the code:<mod:model

xmlns:mod="http://jacksonelect.com/models"><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>

Page 12: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

12

Applying a Namespace to an Element

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

• While the “mod” prefix was only added to the model element name, the XML parser considers the other elements parts of the model namespace and they inherit the namespace.

Page 13: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

13

Applying a Namespace to an Element

• They are unqualified elements, though, because they lack a namespace prefix.

• Declaring a namespace by adding it as an attribute of the document’s root element places all elements in the namespace.

• All elements thus are children of the root element.

Page 14: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

14

Declaring a Default Namespace

• You can specify a default namespace by omitting the prefix in the namespace declaration.

• The element containing the namespace attribute and all of its child elements are assumed to be part of the default namespace.

Page 15: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

15

Working with Attributes

• Attributes, like elements, can become qualified by adding the namespace prefix to the attribute name. For example,

<element prefix:attribute="value"> ... </element>

Page 16: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

16

Working with Attributes

• No element may contain two attributes with the same name.

• No element may contain two qualified attribute names with the same local part, pointing to identical namespaces, even if the prefixes are different.

Page 17: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

17

Adding 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 “uri”;

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

• Example:

@namespace mod “http://jacksonelect.com/models”;

Page 18: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

18

Applying 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 19: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

19

Defining Namespaces with the Escape Character

• Not all browsers support the use of the @namespace rule

• A proposal implement in the Internet Explorer browser was to insert the backslash escape character before the namespace prefix in CSS style sheets:

prefix\:selector {attribute1:value1; attribute2:value2;…}

• Browsers like Firefox, Opera, and Safari do not support this method with XML documents

Page 20: Tutorial 12

20

Declaring and Applying a Namespace in a Style Sheet

• To declare a namespace in a CSS style sheet, add the rule

@namespace prefix "uri";

before any style declarations, where prefix is the namespace prefix and uri is the namespace URI. If no prefix is specified, the namespace URI is the default namespace for selectors in the style sheet.

• To apply a namespace to a selector, use the form

prefix|selector {attribute1:value1; attribute2:value2; ...}

where prefix is the namespace prefix and selector is a selector for an element or group of elements in the document.

• For Internet Explorer browsers, use the following form to apply a namespace to a selector:

prefix\:selector {attribute1:value1; attribute2:value2; ...}

Page 21: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

21

Combining Standard Vocabularies

• Standard vocabularies may be combined within single documents

Page 22: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

22

Converting HTML to XHTML

• Use your text editor to open the reptxt.htm file from the tutorial.12/tutorial folder. Enter your name and the date in the comment section at the top of the document. Save the file as report.htm.

• Insert the following xml declaration as the very first line in the file (above the comment section):

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

• Add the following attribute to the opening <html> tag:

xmlns="http://www.w3.org/1999/xhtml"

Page 23: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

23

Converting HTML to XHTML

Page 24: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

24

Adding the Elements of the Parts Vocabulary

• Return to the order.xml file in your text editor.

• Copy the contents of the document from the opening <pa:parts> tag through the closing </pa:parts> tag.

• Return to the report.htm file in your text editor and paste the copied elements directly below the h2 heading “Parts List.”

• Add the following attribute to the opening <html> tag:xmlns:pa="http://jacksonelect.com/parts"

• Below the link element that links the report.htm file to the report.css style sheet, insert the following link element:<link rel="stylesheet" href="parts.css" type="text/css" />

• Save the changes and open the report.htm file in your Web browser

Page 25: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

25

Adding the Elements of the Parts Vocabulary

Page 26: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

26

Describing the Items in the Parts List

• Return to the report.htm file in your text editor.

• Scroll down to the first title element in the parts namespace. Directly after the opening <pa:title> tag, insert the text

<span>Title</span>

• Directly after the opening <pa:description> tag in the next line, insert the text

<span>Description</span>

• Directly after the opening <pa:instock> tag in the following line, insert the text

<span>Parts in Stock</span>

• Repeat the previous 3 steps, as necessary

Page 27: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

27

Describing the Items in the Parts List

Page 28: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

28

Describing the Items in the Parts List

Page 29: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

29

Adding Elements from the Models Vocabulary

• Return to the report.htm file in your text editor and add the following namespace declaration to the opening <html> tag:

xmlns:mod="http://jacksonelect.com/models"• Add the following link to the document’s head:

<link rel="stylesheet" href="model.css" type="text/css" />

• In the table cell directly after the Title table heading, insert the element

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

Page 30: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

30

Adding Elements from the Models Vocabulary

• In the table cell directly after the Description table heading, insert the element

<mod:description>Entry level color laser printer</mod:description>

• In the table cell directly after the Type table heading, insert the element

<mod:type>color laser</mod:type>• In the table cell directly after the “Items to be Built” table

heading, insert the element

<mod:ordered>320</mod:ordered>

Page 31: Tutorial 12

Adding Elements from the Models Vocabulary

• Add the following tag directly after the opening <body> tag to include the root element of the models vocabulary:

<mod:model id="pr205">• Directly before the closing </body> tag, insert the

following closing tag for the root model element:

</mod:model>

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

31

Page 32: Tutorial 12

New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition

32

Adding Elements from the Models Vocabulary