When to Use Namespaces in XML

by Dinesh 2012-07-22 23:25:34

When to Use Namespaces in XML

The namespaces in this example perform the basic function of namespaces to act as identifiers to group together logical segments of the XML document. Other XMLbased technologies such as XML Schemas, SOAP, and WSDL make heavy use of XML namespaces to identify data encoding types and important elements of their structure. I’ll be showing many more examples of namespaces being used in context to identify elements for data validation in the next chapter, and examples of namespaces used for encoding, and descriptions of SOAP and WSDL documents in the Web Services section of the book. For now, let’s look at the namespaces and how namespaces affect XML document structure.

Namespaces are useful in identifying sections of documents that are being parsed, transformed, or manipulated in some other way. The parser or transformation engine can identify groups of elements and attributes by their namespace prefix instead of by their element values alone, and this helps to keep logical portions of an XML document together during manipulation.

URIs, URLs, and URNs:

In order to understand namespaces, developers must first understand one of their basic components, URIs. HTTP URIs (Uniform Resource Identifiers) are a format specification for Uniform Resource Locators (URLs), which anyone who uses the Web is probably already familiar with, and Uniform Resource Names (URNs), which they may not be. The main difference is that URLs are used to specify a locationspecific resource on the Web, such as http://www.ibm.com, while URNs are used to describe any value, such as a relative /servlet subdirectory or a variable name. URNs and URLs can be assigned to a URI. In the case of namespaces, URNs are usually used to mask a complicated Namespace or value for later reference, similar to the way DNS replaces an IP address with a URL.

For example, we could have used the following URNs for namespace references:
xmlns:qtlist=”http://www.benztech.com/xsd/quotelist”
xmlns:azlist=”fred”
xmlns:ellist=”This is a urn, part of a uri”


The URIs in the declarations here are qtlist, azlist, and ellist. The first namespace declaration, assigned to the qtlist, is clearly a URL. The second assignment to the azlist is a URN. The last namespace declaration assigned to the ellist URI is a valid URN, but is formatted to make an important point. The URN value of the namespace can contain anything that the W3C namespace Recommendation allows, but because the URI will be used in element names, it has to adhere to the W3C XML document element name rules for characters. ellist is fine as a URI, but a URI formatted as ellist is not well-formed XML.

It’s worth noting that we specifically excluded the root quotedoc and the catalog elements in the document from Namespaces, because they are not part of any logical segment of the document, but just contain the logical segments. Therefore, anything that happens to them during XML document manipulation does not affect the other logical groupings that they contain.
874
like
0
dislike
0
mail
flag

You must LOGIN to add comments