Using Namespaces in XML

To clearly distinguish the order element here you would need to provide more information about the element type name than simply order:

<order> 
 <givenBy>General B. Smart</givenBy> 
 <Content>Relocate 6 jet fighters to MacDill Air Force 
  Base</Content> 
</order> 

from the order element here

<order> 
 <Location>Seattle</Location> 
 <Description>A riot lasted several hours following clashes 
  with anti-globalization protesters. 
</order> 

The solution to this, in XML 1.0, is a qualified name. This is often abbreviated as QName.

A qualified name is an XML element type name that consists of two parts—a namespace prefix and a local part separated by the colon character (:).



Qualified Names

You can distinguish elements using qualified names (QNames). Referring back to the earlier example, the military version of an order could use the QName mil:order. This would distinguish it from possible QNames for the other two order element types, perhaps expressed as business:order and civil:order.

Each QName consists of a namespace prefix, a colon character, and a local part. The local part is what we have called the element type name in a non–namespace-aware document.

Note

The colon character can legally be used for any purpose in XML. To avoid confusion and unpredictable results, it is wise to reserve the use of the colon character as a separator in QNames only.



If you use QNames on their own, you might run into problems similar to those you are trying to avoid. For example, the mil:order element might itself lead to ambiguity. Are you referring to military orders or militia orders? Similarly, if you refer to business:order elements, which of potentially many element types created by individual businesses or consortia are you referring to?

You need a more universal way to distinguish namespaces than simply using namespace prefixes alone. You achieve potentially unique identification of a namespace using a uniform resource identifier and mapping a namespace prefix to it.

URIs Represent Namespaces

In XML namespaces, the namespace name is a uniform resource identifier (URI).

A URI is a potentially lengthy sequence of characters. For example, you might want to create a document type for a particular structure of document. You could choose a namespace URI http://www.XMML.com/myVeryOwnNamespace.

So, why aren’t URIs used directly in QNames? Three reasons are worth mentioning:

First, URIs often make use of the colon character as a separator. With the following start tag, ambiguity exists regarding which of the two colon characters is the separator between the namespace prefix and the local part:

<http://www.XMML.com/myVeryOwnNamespace:document> 

Does this refer to an element whose namespace prefix is http and local part is //www.XMML.com/myVeryOwnNamespace:document, or does this refer to an element whose namespace prefix is http://www.XMML.com/myVeryOwnNamespace and local part is document? For the human reader, it is pretty obvious that the second possibility is much more likely; for an XML processor, however, serious ambiguity arises.

Second, if you write elements using the literal URI throughout the XML document, it would soon become difficult for a human reader to decipher what the document is about. This is particularly true if lines must be broken to squeeze the lengthy URI onto the page:

<http://www.XMML.com/myVeryOwnNamespace:document> 
 <http://www.XMML.com/myVeryOwnNamespace:introduction> 
  Some content goes here. 
 </http://www.XMML.com/myVeryOwnNamespace:introduction> 
<!-- Many more lengthy elements could go here. --> 
</http://www.XMML.com/myVeryOwnNamespace:document> 

Third, URIs can contain characters that are not allowed in XML names.

The problem is solved by using a succinct namespace prefix that complies with the rules for XML names and that is mapped to a namespace URI. Declaring a namespace involves associating the namespace prefix with the namespace URI. In XML, you do this using a special type of attribute called a namespace declaration.

Namespace Declarations

To be used in an XML document, a namespace must be declared. A namespace declaration is made in the start tag of the element to which it refers.

A namespace declaration has a special structure. For most namespace declarations, the attribute name begins with the character sequence xmlns, followed by a colon and the namespace prefix. These are followed by an equal sign (=) and the namespace URI enclosed in a pair of double or single quotation marks:

xmlns:namespacePrefix='namespaceURI' 

Note

XML names that begin with the character sequence XML (in any case combination) are reserved for W3C use, as in the namespace declarations demonstrated in this section. As a result, do not attempt to create your own namespace named xml:.



The document element in the http://www.XMML.com/myVeryOwnNamespace namespace can be declared as follows, assuming that you map the namespace URI to the namespace prefix XMML:

<XMML:document xmlns:XMML="http://www.XMML.com/ 
 myVeryOwnNamespace> 

The short sample document then would be written as follows:

<XMML:document 
 xmlns:XMML="http://www.XMML.com/myVeryOwnNamespace> 
  <XMML:introduction> 
   Some content goes here. 
  </XMML:introduction> 
 <!-- Many more lengthy elements could go here. --> 
</XMML:document> 

The alternative form of namespace declaration is the character sequence xmlns followed by the equal sign and the namespace URI enclosed in paired quotation marks:

xmlns="namespaceURI" 

Note

A namespace declared using the xmlns="namespaceURI " namespace declaration syntax is termed the default namespace.



Using this approach, you could express your document as follows:

<document 
  xmlns="http://www.XMML.com/myVeryOwnNamespace> 
  <introduction> 
   Some content goes here. 
  </introduction> 
 <!-- Many more lengthy elements could go here. --> 
</document> 

For the human reader, the immediately preceding version is perhaps easier to read, but the earlier version using the XMML namespace prefix is less ambiguous. For the XMML processor, these documents are identical in a namespace sense. All the elements in each document are associated with the namespace URI http://www.XMML.com/myVeryOwnNamespace, and the XML processor uses the namespace URI for identifying namespaces.

Caution

Namespace URIs in XML must be identical before they are considered to refer to the same namespace. So, if you create your own namespace, be sure that you are totally consistent in how you use uppercase and lowercase characters.



If document authors use URIs for domains that they own or otherwise legitimately use, a namespace URI should be unique. In addition, it is important that namespace URIs are persistent, to instill confidence that URIs and their related XML vocabularies remain stable.

Note

The namespace URI need not point to any particular document. In particular, it need not contain any schema for the class of documents. The primary purpose of the namespace URI is to provide a unique and persistent identifier for an XML vocabulary.



Namespace declarations should be explicit in the start tag of an appropriate element or should be declared in the internal subset of the DTD. Namespace-aware processors are not required to be validating processors. Therefore, external declarations may not be accessed by a nonvalidating processor.

Namespaces and Attributes

An attribute is assumed to be in the same namespace as the element with which it is associated. The namespace prefix of the element need not be expressed on the attribute, too. For example, both the XMML:chapter element and the number attribute in the following code are in the same namespace:

<XMML:chapter 
xmlns:XMML=http://www.XMML.com/ABookNamespace 
number="3"> 
<!-- Chapter content goes here. --> 
</XMML:chapter> 

An attribute need not share the same namespace prefix as the start tag on which it is placed. You have seen this already for the reserved attributes whose qualified names begin with the character sequence xmlns.

More generally, to use an attribute with a different namespace, the namespace prefix must be declared on either the element or an ancestor of it. Consider this example:

<someNamespace:someElement 
xmlns:someNamespace=http://www.XMML.com/someNamespace 
xmlns:someOtherNamespace=http://www.XMML.com/someOtherNamespace 
someOtherNamespace:someAttribute="something" 
/> 

In principle, the value of an XML attribute may contain the colon character. However, in namespace-aware XML documents, if the attribute is declared to be of type ID, IDREF, IDREFS, ENTITY, ENTITIES, or NOTATION, use of the colon character is not permitted.

Namespace Well-Formedness

The “Namespaces in XML” Recommendation (http://www.w3.org/TR/1999/REC-xml-names-19990114), or, more precisely, an erratum to it (http://www.w3.org/XML/xml-names-19990114-errata) , specifies a concept of namespace well-formedness.

Namespace well-formedness includes the XML well-formedness criteria defined in Chapter 2, “The Structure of an XML Document,” and Chapter 3, “XML Must Be Well-Formed,” together with the following constraints:

  • Element type names and attribute names may contain either zero or one colon characters.

  • No entity names or processing instruction targets or notation names may contain a colon character.

  • No attribute that is declared to be of type ID, IDREF, ENTITY, ENTITIES, or NOTATION may contain a colon character in its value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset