Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 2: XML Working with Namespaces. COMBINING XML VOCABULARIES IN A COMPOUND DOCUMENT Section 2.1.

Similar presentations


Presentation on theme: "Tutorial 2: XML Working with Namespaces. COMBINING XML VOCABULARIES IN A COMPOUND DOCUMENT Section 2.1."— Presentation transcript:

1 Tutorial 2: XML Working with Namespaces

2 COMBINING XML VOCABULARIES IN A COMPOUND DOCUMENT Section 2.1

3 parts part title description instock + The parts element must contain at least one part The parts vocabulary includes the title of each part, the part description, and the number of each part currently in stock (instock)

4 parts.css parts.xml rendered Web page

5 title description type model ordered parts The model vocabulary contains elements for a model’s title, description, type, the number of models that need to be assembled (ordered), and the parts required for the assembly

6 model.css model.xml rendered Web page

7 elements from the parts vocabulary elements from the model vocabulary Web page elements from the XHTML vocabulary

8 title description instock part parts title description type model ordered parts +

9 Rendered order.xml Web page style of the parts.xml content style of the model.xml content style of the combined document

10 title description instock part parts + title description type model ordered parts

11 Name Collision Name collision occurs when elements from two or more documents share the same name. Name collision will keep a document from being validated. Name collisions are often unavoidable.

12 Using Namespaces to avoid Name Collision A namespace is a defined collection of element and attribute names. – For example: the model namespace and the parts namespace 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

13 Working with Namespaces Applying a namespace to an XML document involves two steps: 1. Declaring the namespace 2. Identifying the elements and attributes within the document that belong to that namespace

14 Declaring a Namespace To declare a namespace, add the following: xmlns:prefix=“uri” – prefix is the namespace prefix and uri (used as an id) that uniquely identifies a resource – The namespace is applied to the element containing the xmlns attribute, as well as to its descendant elements. Namespaces can be declared in document’s root element

15 Applying a Namespace to Elements After declaring the namespace, identify the elements and attributes that belong to that namespace:... Examples: Laser4c (pr205)

16 Applying the models namespace prefix

17 Applying the parts namespace prefix

18 Applying a Default Namespace Declare a default namespace by omitting the prefix in the namespace declaration.... Laser4C (PR205) Entry level color laser printer color laser 320

19 Working with Attributes To apply a namespace to an attribute, add: Example: There is no default namespace for attribute names. An attribute name without a prefix is assumed to belong to the namespace as the element that contains it.

20 ADDING A NAMESPACE TO A STYLE SHEET Section 2.2

21 Declaring a Namespace in a Style Sheet Add the following rule: @namespace prefix url(uri); Both the prefix and uri must match the prefix and uri used in the XML document Example: @namespace mod url(http:...); Any @namespace rule must come after all @import and @charset rules and before any style declarations

22 Applying a Namespace to a Selector prefix|selector {attribute1:value1; attribute2:value2;...} prefix\:selector {attribute1:value1; attribute2:value2;...} Examples: – mod|title{width: 150px} – mod|*{font-size: 12pt} – mod\:title{width: 150px} For Internet browsers

23 Applying the models namespace to the style sheet

24 Applying the parts namespace to the style sheet

25 Web page combining styles from both namespaces styles applied from the models namespace styles applied from the parts namespace

26 Namespace URIs for standard vocabularies

27 Combining Standard Vocabularies The standard vocabularies can also be combined within single documents. For example, Firefox, using a built-in style sheet, is able to display the documents that combine both the XHTML and MathML language. The elements of almost any XML document can be displayed in a Web browser if you design style sheets to work with the elements of the XML vocabulary.

28 A compound XHTML and MathML document XML document with XHTML and MathML elements Document rendered by Firefox

29 Adding XML to an HTML document Leave the html elements in the default namespace, and provide namespace prefixes for all of the non-HTML elements Convert a HTML file into an XHTML file – adding an xml declaration – setting the default namespace of the document to the XHTML vocabulary

30 . <html xmlns=“http://www.w3.org/1999/xhtml” xmlns:pa=“http://jacksonelect.com/parts”>.. Parts List.. Every element that lacks a namespace prefix is considered part of the XHTML vocabulary

31 Adding descriptive text to each of the bulleted items

32

33 Completed compound report page


Download ppt "Tutorial 2: XML Working with Namespaces. COMBINING XML VOCABULARIES IN A COMPOUND DOCUMENT Section 2.1."

Similar presentations


Ads by Google