Untitled Document



 Introduction to XML
 Data Definition and Data  Modeling
 Namespaces and  Schemas
 Namespaces
 
   Namespaces
 Declaring a  Namespace
   Scope
   Default
 Qualified
 Schemas
 
   Schemas
   Role of a Schema
   DTD as a Schema
 Schema as a set of  constraints
 Schema as an  Explanation
   DTD Vs XML Schema
   Structures
   Preamble
   Sample Preamble
 Attributes and  Attribute Groups
   Content Models
   Element Declaration
   Derivation
   Data Types
   Primitive Types
 Generated and User  Defined Types
 Linking and Querying
 Ecommerce Application  using XML

Copyrights : Layout Galaxy All Rights Reserved
No part of this tutorial may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, electrostatic, magnetic tape, mechanical or otherwise, without prior permission in writing from Layout Galaxy.




 Namespaces and Schemas > Namespaces

  Namespaces

A namespace is a collection of names that is identified by a Uniform Resource Identifier (URI). Namespaces is a methodology for creating universally unique names in an XML document by identifying element names with a unique external resource.

Namespaces help XML vocabulary designers to break complex problems into smaller pieces. Namespaces mix multiple vocabularies as needed to fully describe a problem in a single XML document.

A Uniform Resource Identifier (URI) is a unique name for resource, which resides in a network.
A Uniform Resource Locator (URL) locates the resource in terms of an access protocol and network location.

  Declaring a Namespace

Two XML documents might contain elements with the same names but different meanings. If both the documents need to be used in a single environment, there will be confusion about the overlapping elements. For example, consider the following XML code.

<CUSTOMER>
              <NAME>Morgaret</NAME>
</CUSTOMER>
<BOOK>
              <NAME>Brain Teasers</Name>
</BOOK>

<BILL>
     <CUSTOMER>
              <NAME>Morgaret</NAME>
     </CUSTOMER>
     <BOOK>
              <NAME>Brain Teasers</NAME>
     </BOOK>
</BILL>

Here the CUSTOMER element and the BOOK element have NAME element, but the NAME element has different meanings in each case. If these elements are combined into a single document as shown in the following code, the NAME elements will lose their meaning.

This is a very big problem and the solution is XML namespaces, which offers a way to create names that remain unique no matter where the elements are used.

A Namespace can be qualified or unqualified. It does us no good to declare a namespace if we can't tie it to a specific name we want to use. This is done through the use of qualified names.
The two parts of qualified name are Namespace and Local part. The namespace name is a URI and selects the namespace. The local part is the local document element or attribute name. For using a namespace in an XML document, a namespace declaration in the prolog of the document must be included. A namespace prefix can also be included in the declaration. The prefix with a colon can be attached to local part to associate the local part with the namespace name.

The following code declares two namespaces with prefixes and then uses those namespaces in the document.
Here cust:NAME and book:NAME are fully qualified names and they will be unique no matter where they are used.

<xmlns version = "1 . 0"?>
<xmlns : namespace ns="http://books/schema/ns" prefix = "books"?>
<xmlns : namespace ns="http://customer/schema/ns" prefix = "cust"?>
<BILL>
    <CUSTOMER>
                  <cust:NAME>Morgaret</cust:NAME>
    </CUSTOMER>
    <BOOK>
                  <books:NAME>Brain Teasers</books:NAME>
    </BOOK>
</BILL>

Since one of our prime motivations for using namespaces was to be able to mix name from different sources. It might be useful for you to be able to provide an alias you could use throughout a document that would refer to the declaration. You do this by appending a colon and your alias to xmlns. Diagram shown on the screen will help us to understand the concept.


  Scope

Namespace declarations have scope in the same way that variable declarations do in the programming. This is important because it is not always the case that namespaces are declared at the beginning as XML documents, they can be included within a later section of the document.

A name can refer to a namespace only if it is used within the scope of the namespace declaration. However, we will also need to mix namespaces where elements would otherwise inherit the scope of a namespace, so there are two ways in which scope can be declared. It can be either default or qualified.

  Default

To use namespaces we need to prefix every name in a document, this could be tiresome when we have many namespaces in the document.

By introducing the concept of name scope to our tool set, we can dispense with a lot of prefixes. If we define a default namespace, all unqualified names within the scope of the declaration are presumed to belong to that default. So, if you declare a default namespace in the root element, it is treated as default namespace for the whole document, and can only be overridden by a more specific namespace declared within the document.

Here's how you might use default scope to embed some HTML within an XML document marked up according to a DTD designed for book content, called BookContent.dtd. The elements <Title>, <Author>, <Content>, and <Paragraph> and the attribute number come from the default namespace defined in the <Chapter> element.
Within the Chapter element, however, you can see the table element and its children - tr and td. These belong to the HTML namespace declared in the table element. Note that scope of the HTML namespace declaration in this example ends when the table element closes. The second occurrence of Paragraph does not come from the HTML namespace.

<Chapter xmlns=http://www.wrox.com/bookdefs/BookContent.dtd>
         <Title number="7">Namespaces and Schemas</Title>
         <Author>I. M. Named</Author>
         <Content>
                 <Paragraph>
                       Let's have a table:
                       <table xmlns=http://www.w3.org/TR/REC/REC-html40>
                            <tr>
                                 <td>
A tisket</td><td>A tasket</td>
                            </tr>
                            <tr>
                                 <td>One fish</td><td>
Two fish</td>
                            </tr>
                       </table>

                 </Paragraph>
                 <Paragraph>This is a very short paragraph</Paragraph>
         </Content>
</Chapter>

  Qualified

Though we clearly separate the various namespaces, sometimes we need to sprinkle names from foreign namespaces through a document. For this, a finer degree of granularity is needed. Hence, we can make use of qualified names instead of declaring namespaces all over the space. The namespaces are to be declared at the beginning of the document and then qualified at the point of use.

<Measurements xmlns="urn:mydecs-science-measurements">
        xmlns:units="urn:mydecs-science-unitsofmeasure"
        xmlns:prop="urn:mydecs-science-thingsmeasured"
       <OutsideAir units:units="Fahrenheit">86</OutsideAir>
       <FuelTank>
             <prop:Volume units:units="liters">120</prop:Volume>
             <prop:Temperature units:units="Celsius">20</prop:Temperature>
       </FuelTank>
</Measurements>

In the root element, Measurements, we have declared three namespaces. The default takes care of the elements <OutsideAir>, <FuelTank>, and <Measurements>. However, we need to qualify some readings with units of measure, which we have done with the units namespace and the attribute units:units drawn from that namespace. Finally, we need to differentiate between some types of measurements, prop:Volume and prop:Temperature.

Back Next


Copyrights : Layout Galaxy All Rights Reserved
No part of this tutorial may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, electrostatic, magnetic tape, mechanical or otherwise, without prior permission in writing from Layout Galaxy.




17, Vadsarvala Nivas, 65-A, J. Nehru Road, Mulund (W), Mumbai - 400 080 INDIA
Tel : 91-22-25795588, 91-22-25780444 Fax : 91-22-25793397
Email : ionline@vsnl.com
© Image Online 2001-2003