Namespaces


We now explain namespaces, which are identifiers that place element and attribute names in categories. If you're reading only to get a sense of SOA, you can skim this section, but you may have reasons to read straight through:

  • Knowledge of namespaces can help you avoid feeling intimidated by the seemingly complex data at the top of most XML documents.

  • A developer needs to know the subject at least occasionally.

  • The material on XML Schema is best understood by someone with prior knowledge of namespaces.

  • Later chapters in this book refer to namespaces.

Purpose of Namespaces

Namespaces are categories for ensuring that names in an XML document are unique in that document. With namespaces, a developer can combine different XML vocabularies without fear that identical names in different vocabularies will be handled in the same way. An XML processor, for example, might use a namespace to distinguish the rules for validating a name that is used multiple times for different purposes. Similarly, code that you write to review a complex XML-based message might need to identify the namespace associated with a name.

Before we tell you how to specify a namespace, consider an example that includes two instances of the element name Title. One instance refers to a book, and one refers to a person.

 <TitleTypes>   <Title xmlns="BookTitleNamespace">     <Name>Oliver Twist</Name>   </Title>   <Title>Duke of Windsor</Title> </TitleTypes> 

If an element name is in a namespace, the XML processor interprets the name in a way that includes the namespace identifier. We might express the first instance of the name Title as follows:

 {BookTitleNamespace}Title 

An element or attribute name can be outside any namespace, as is true of the second instance of Title. In the absence of a namespace, the XML processor interprets the name without referring to a namespace identifier. We might express that second instance simply as Title.

The implication of a namespace depends solely on the software that processes a given XML document. An XML processor might use a namespace identifier to determine which of several versions of an XML vocabulary is being used or to determine which XML Schema validates a given element.

Namespace Identifiers

A namespace is usually a character string that is formatted as if to refer to a Web-based resource such as a Web site or file. But a namespace may be any Universal Resource Identifier (URI) - that is, any unique value that conforms to the URI rules defined by the Internet Engineering Task Force (IETF). Here are some examples of namespace URIs:

 http://www.w3.org/2001/XMLSchema ISBN:0452010624 

Later, we show how to declare a namespace.

One kind of URI is a Uniform Resource Locator (URL), which can be used as a namespace identifier. The specific URL may refer to a Web site that describes the purpose of the namespace or of the XML document. In most cases, however, the XML processor does not access a Web site, and the URI often does not refer to a Web site at all.

The namespace URI is just an identifier, with no meaning other than to place a set of names in a category. Often, your organization's Internet domain name (such as www.w3.org) is appropriately included to ensure uniqueness.

Namespace Qualification and Declarations

An XML-based name is said to be namespace-qualified if the XML associates the name with a namespace. A name that is not associated with a namespace is not in a default namespace; instead, the XML processor interprets the name as being outside any namespace.

A name can be namespace-qualified in either of two ways. Each way requires you to first declare a namespace by using the identifier xmlns, which is an abbreviation for XML namespace.

You declare a non-default namespace by following xmlns with a colon (:) and a second identifier of your choice (for instance, target), as in the following start-tag.

 <Insured xmlns:target="http://www.IBM.com/HighlightInsurance"> 

Later in the XML document, you can use that second identifier as a prefix to indicate that an element or attribute name (such as Model) is in a non-default namespace.

 <target:Model>Spitfire</target:Model> 

You declare a default namespace by specifying xmlns without a subsequent colon, as in the following start-tag.

 <Insured xmlns="http://www.IBM.com/software"> 

In the absence of other namespace syntax, the element name (Insured, in this case) is in the default namespace, as is the name of each element descended from that element.

The start-tag of the XML root element often includes one or more namespace declarations, as shown here.

 <wsdl:definitions    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"    xmlns:tns="http://www.transunion.com/"    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

Namespace Rules

Before we review an example, consider the following rules:

  • Any namespace declaration is in scope (that is, available) in the element in which the declaration appears and in the element's descendant elements.

  • An element name that is in the scope of a default namespace and is not prefixed is in the default namespace.

  • An attribute name can never be in a default namespace:

    • If the attribute name has a prefix, the name is in the namespace referenced by that prefix.

    • If the attribute name is not prefixed, the name is outside any namespace. You can never use a default namespace to reference an attribute.

  • A default namespace can be overridden for a given element and for all the descendant elements. In the following example, the element name CarPolicy is in the PolicySpace namespace; and the element names Vehicle, Make, and Model are in the VehicleSpace namespace.

     <CarPolicy xmlns="PolicySpace" PolicyType="Auto">    <Vehicle xmlns="VehicleSpace" Category="Sedan">       <Make>Honda</Make>       <Model>Accord</Model>    </Vehicle> </CarPolicy> 

  • A default namespace can be removed from scope for a given element and for all the descendant elements. In the following example, the names Vehicle, Make, and Model are not in any namespace because the default namespace is set to an empty string ("").

     <Vehicle xmlns="" Category="Sedan">    <Make>Honda</Make>    <Model>Accord</Model> </Vehicle> 

  • Multiple non-default namespaces can be available for a given element and for all descendant elements, and those namespaces are also available for the attributes in those elements. In the following example, the element names Order and Customer are in OrderSpace, while the element names Company and Buyer are in CustSpace, as is the attribute Number.

     <ord:Order xmlns:ord="OrderSpace">    <ord:Customer xmlns:cust="CustSpace">       <cust:Company cust:Number="123">       ABC       </cust:Company>       <cust:Buyer>       Smith       </cust:Buyer>    </ord:Customer> </ord:Order> 

Namespace Example and Terminology

Consider the XML shown in Listing 4.2.

Listing 4.2: Sample XML with namespaces

image from book
 <?xml version="1.0" encoding="ISO-8859-1"?> <!- an order -> <Order xmlns="http://www.ibm.com/software"    xmlns:prod="http://www.ibm.com/next">    <Customer Cust Code="1234">      <prod:Product prod:>        <prod:Type>Blackboard</prod:Type>        <prod:Quantity>2</prod:Quantity>      </prod:Product>      <prod:Product prod:>        <prod:Type>Whiteboard</prod:Type>        <prod:Quantity>3</prod:Quantity>      </prod:Product>      <prod:Product prod:/>    </Customer> </Order> 
image from book

In this example two element names, Order and Customer, are in the default namespace, http://www.ibm.com/software. The element names Product, Type, and Quantity are in the namespace http://www.ibm.com/next. Given the prefix in front of the attribute name ID in the Product element, the name ID is also in that second namespace. The names of the Customer element's CustID and Code attributes are not in a namespace.

The following terminology is in use:

  • The name of a namespace (such as http://www.ibm.com/software) is the namespace URI.

  • The element or attribute name can include a prefix and a colon (as in prod:Quantity). A name in that form is called a qualified name, or QName, and the identifier that follows the colon is called a local name. If a prefix is not in use, neither is the colon, and the QName and local name are identical.

  • An XML identifier (such as a local name) that has no colon is sometimes called an NCName. (The NC comes from the phrase no colon.)




SOA for the Business Developer. Concepts, BPEL, and SCA
SOA for the Business Developer: Concepts, BPEL, and SCA (Business Developers series)
ISBN: 1583470654
EAN: 2147483647
Year: 2004
Pages: 157
Authors: Ben Margolis

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