Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - XML and RSS

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

XML Namespaces

XML allows document authors to create custom elements. This extensibility can result in naming collisions among elements in an XML document that each have the same name.

XML Namespaces

 

XML allows document authors to create custom elements. This extensibility can result in naming collisions among elements in an XML document that each have the same name. For example, we may use the element book to mark up data about a Deitel publication. A stamp collector may use the element book to mark up data about a book of stamps. Using both of these elements in the same document could create a naming collision, making it difficult to determine which kind of data each element contains.

 

An XML namespace is a collection of element and attribute names. XML namespaces provide a means for document authors to unambiguously refer to elements with the same name (i.e., prevent collisions). For example,

 

<subject>Geometry</subject>

 

and

 

<subject>Cardiology</subject>

 

use element subject to mark up data. In the first case, the subject is something one studies in school, whereas in the second case, the subject is a field of medicine. Namespaces can differentiate these two subject elements—for example:

 

<highschool:subject>Geometry</highschool:subject>

 

And

<medicalschool:subject>Cardiology</medicalschool:subject>

Both highschool and medicalschool are namespace prefixes. A document author places

 

a namespace prefix and colon (:) before an element name to specify the namespace to which that element belongs. Document authors can create their own namespace prefixes using virtually any name except the reserved namespace prefix xml. In the next subsections, we demonstrate how document authors ensure that namespaces are unique.

Differentiating Elements with Namespaces

 

Figure 14.7 demonstrates namespaces. In this document, namespaces differentiate two distinct elements—the file element related to a text file and the file document related to an image file.

 

1 <?xml version = "1.0"?>

 

2

3    <!-- Fig. 14.7: namespace.xml -->

 

4    <!-- Demonstrating namespaces -->

 

5    <text:directory

 

6          xmlns:text = "urn:deitel:textInfo"

 

7          xmlns:image = "urn:deitel:imageInfo">

8              

9          <text:file filename = "book.xml">

 

10               <text:description>A book list</text:description>

 

11         </text:file>

12

13         <image:file filename = "funny.jpg">

 

14               <image:description>A funny picture</image:description>

 

15               <image:size width = "200" height = "100" />

 

16         </image:file>

 

17  </text:directory>


 

Fig. 14.7 | XML namespaces demonstration.

 

 

Lines 6–7 use the XML-namespace reserved attribute xmlns to create two namespace prefixes—text and image. Each namespace prefix is bound to a series of characters called a Uniform Resource Identifier (URI) that uniquely identifies the namespace. Document authors create their own namespace prefixes and URIs. A URI is a way to identifying a resource, typically on the Internet. Two popular types of URI are Uniform Resource

Name (URN) and Uniform Resource Locator (URL).

 

To ensure that namespaces are unique, document authors must provide unique URIs.

 

In this example, we use the text urn:deitel:textInfo and urn:deitel:imageInfo as URIs. These URIs employ the URN scheme frequently used to identify namespaces. Under this naming scheme, a URI begins with "urn:", followed by a unique series of addi-tional names separated by colons.

 

Another common practice is to use URLs, which specify the location of a file or a resource on the Internet. For example,  www.deitel.com is the URL that identifies the home page of the Deitel & Associates website. Using URLs guarantees that the namespaces are unique because the domain names (e.g.,  www.deitel.com) are guaranteed to be unique. For example, lines 5–7 could be rewritten as

 

<text:directory

 

xmlns:text = "http://www.deitel.com/xmlns-text" xmlns:image = "http://www.deitel.com/xmlns-image">

 

where URLs related to the deitel.com domain name serve as URIs to identify the text and image namespaces. The parser does not visit these URLs, nor do these URLs need to refer to actual web pages. They each simply represent a unique series of characters used to differ-entiate URI names. In fact, any string can represent a namespace. For example, our image namespace URI could be hgjfkdlsa4556, in which case our prefix assignment would be

 

xmlns:image = "hgjfkdlsa4556"

 

Lines 9–11 use the text namespace prefix for elements file and description. Note that the end tags must also specify the namespace prefix text. Lines 13–16 apply namespace prefix image to the elements file, description and size. Note that attributes do not require namespace prefixes (although they can have them), because each attribute is already part of an element that specifies the namespace prefix. For example, attribute filename (line 9) is implicitly part of namespace text because its element (i.e., file) spec-ifies the text namespace prefix.

 

Specifying a Default Namespace

To eliminate the need to place namespace prefixes in each element, document authors may specify a default namespace for an element and its children. Figure 14.8 demonstrates using a default namespace (urn:deitel:textInfo) for element directory.

 

Line 5 defines a default namespace using attribute xmlns with no prefex specified, but with a URI as its value. Once we define this default namespace, child elements belonging to the namespace need not be qualified by a namespace prefix. Thus, element file (lines 8–10) is in the default namespace urn:deitel:textInfo. Compare this to lines 9–10 of Fig. 14.7, where we had to prefix the file and description element names with the namespace prefix text.

 

The default namespace applies to the directory element and all elements that are not qualified with a namespace prefix. However, we can use a namespace prefix to specify a

 

1  <?xml version = "1.0"?>

 

2

3    <!-- Fig. 14.8: defaultnamespace.xml -->

 

4    <!-- Using default namespaces -->

 

5    <directory xmlns = "urn:deitel:textInfo"

 

6          xmlns:image = "urn:deitel:imageInfo">

7              

8          <file filename = "book.xml">

 

9                 <description>A book list</description>

 

10         </file>

11

12         <image:file filename = "funny.jpg">

 

13                <image:description>A funny picture</image:description>

 

14                <image:size width = "200" height = "100" />

 

15         </image:file>

 

16   </directory>

 


Fig. 14.8 | Default namespace demonstration.

 

different namespace for a particular element. For example, the file element in lines 12– 15 includes the image namespace prefix, indicating that this element is in the

urn:deitel:imageInfo namespace, not the default namespace.

 

Namespaces in XML Vocabularies

XML-based languages, such as XML Schema (Section 14.6) and Extensible Stylesheet Language (XSL) (Section 14.8), often use namespaces to identify their elements. Each of these vocabularies defines special-purpose elements that are grouped in namespaces. These namespaces help prevent naming collisions between predefined elements and user-defined elements.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.