Download presentation
Presentation is loading. Please wait.
Published byAlberta Richards Modified over 9 years ago
1
Tutorial 2: XML Working with Namespaces
2
2 Name Collision This figure shows two documents each with a Name element
3
3 Layout of XML Document This figure shows the structure of the xml file and the collision between the Name element.
4
4 Using Namespaces to Avoid Name Collision This figure shows how to use a namespace to avoid collision
5
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)
6
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
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
title description instock part parts + title description type model ordered parts
10
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.
11
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 To disambiguate between two elements that happen to share the same name To group elements relating to a common idea together A namespace is a unique URI (Uniform Resource Locator) uri
12
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
13
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
14
Applying a Namespace to Elements After declaring the namespace, identify the elements and attributes that belong to that namespace:... Examples: Laser4c (pr205)
15
Defining One Prefix for a Namespace Here’s an example where we define one prefix for a namespace: An example document a simple document Another element
16
Defining Multiple Prefixes for the Same Namespace It is possible for different prefixes to actually refer to the same namespace, as follows: An example document a simple document Another element
17
Applying the models namespace prefix
18
Applying the parts namespace prefix
19
Multiple Namespaces If you’re using namespaces, you will almost certainly need to use several namespaces at once — so how can you declare more than one namespace at a time? What you do is use more than one xmlns declaration, like this: An example document a simple document Another element
20
20 The elements that are used to create an XML Schema come from the XMLSchema namespace <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified">
21
21 Default XMLSchema, Qualify targetNamespace BookCatalogue Book Title Author Date ISBN Publisher http://www.publishing.org element annotation documentation complexType schema sequence http://www.w3.org/2000/10/XMLSchema
22
22 Referencing a schema in an XML instance document <BookCatalogue xmlns ="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:schemaLocation="http://www.publishing.org BookCatalogue.xsd"> My Life and Times Paul McCartney July, 1998 94303-12021-43892 McMillin Publishing... 1. First, using a default namespace declaration, tell the schema-validator that all of the elements used in this instance document come from the publishing namespace. 2. Second, with schemaLocation tell the schema-validator that the http://www.publishing.org namespace is defined in BookCatalogue.xsd. 3. Third, tell the schema-validator that schemaLocation attribute we are using is the one in the schema instance namespace.
23
23 Referencing a schema in an XML instance document BookCatalogue.xml BookCatalogue.xsd targetNamespace="A" schemaLocation="A BookCatalogue.xsd" - defines elements in namespace A - uses elements from namespace A
24
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
25
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.
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.