Section 19.4. XML Namespaces


19.4. 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. Like Visual Basic namespaces, XML namespaces provide a means for document authors to unambiguously refer to elements with the same name (i.e., prevent collisions). For example,

 <subject>Math</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

 <school:subject>Math</school:subject> 


and

 <medical:subject>Cardiology</medical:subject> 


Both school and medical 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.

Common Programming Error 19.7

Attempting to create a namespace prefix named xml in any mixture of uppercase and lowercase letters is a syntax errorthe xml namespace prefix is reserved for internal use by XML itself.


Differentiating Elements with Namespaces

Fig. 19.7 demonstrates namespaces. In this document, namespaces differentiate two distinct elementsthe file element related to a text file and the file document related to an image file.

Figure 19.7. XML namespaces demonstration.

  1  <?xml version = "1.0" ?>  2  <!-- Fig. 19.7: namespace.xml -->  3  <!-- Demonstrating namespaces -->  4  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> 

Lines 67 use the XML-namespace reserved attribute xmlns to create two namespace prefixestext 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 ResourceName (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 additional 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 Web site. 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 57 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 & Associates, Inc. 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 differentiate 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 911 use the text namespace prefix for elements file and description. Note that the end tags must also specify the namespace prefix text. Lines 1316 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) specifies 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 19.8 demonstrates using a default namespace (urn:deitel:textInfo) for element directory.

Figure 19.8. Default namespace demonstration.

  1  <?xml version = "1.0" ?>  2  <!-- Fig. 19.8: defaultnamespace.xml -->  3  <!-- Using default namespaces -->  4  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> 

Line 5 defines a default namespace using attribute xmlns 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 810) is in the default namespace urn:deitel:textInfo. Compare this to lines 810 of Fig. 19.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 different namespace for particular elements. For example, the file element in lines 1215 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 19.6), Extensible Stylesheet Language (XSL) (Section 19.7) and BizTalk (www.microsoft.com/biztalk), 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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net