January 28, 2010

0 XML, NameSpaces

The XML namespace attribute is placed in the start tag of an element and has the following syntax:


When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

Note that the address used to identify the namespace is nto used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing
information about the name space.

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,Geometry
and Cardiology  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:

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.

Common Programming Error 12.7
Attempting to create a namespace prefix named xml in any mixture of uppercase and lowercase
letters is a syntax error—the xml namespace prefix is reserved for internal use by XML itself. 12.7

Differentiating Elements with Namespaces

Figure 12.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.

6 xmlns:text = "urn:deitel:textInfo"
7 xmlns:image = "urn:deitel:imageInfo">

10 A book list

14 A funny picture


Fig. 12.7

Fig. 12.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 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 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 
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
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 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) 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 12.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. 12.7, where we had to prefix the file and description element names with the
namespace prefix text. 

6 xmlns:image = "urn:deitel:imageInfo">
9 A book list

13 A funny picture


 Fig. 12.8 | Default namespace demonstration.

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 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 12.6) and Extensible Stylesheet
Language (XSL) (Section 12.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


Post a Comment

Blogger Themes

Powered by Blogger