Sie sind auf Seite 1von 21

WEB PROGRAMMING

MODULE 1
Namespaces: Organizing Elements with Namespaces
1. Namespaces and URI
2. Namespace Declarations
3. Examples
4. Types: Default and Explicit Namespaces

UDAY BABU P
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VIDYA ACADEMY OF SCIENCE AND TECHNOLOGY

NAMESPACES: Definition & syntax for declaring


namespaces

An XML namespace is a collection of element and


attribute names used in XML documents.
The name of a namespace usually has the form of
a uniform resource identifier(URI).
A namespace for an element is declared as the
value of the attribute xmlns.
The form of a namespace declaration for an
element is
<element_name xmlns[:prefix] = URI>

NAMESPACE in detail
For example, consider the element 'name' in an XML document. The
name element could contain the name of a person, an organization, a
country and so on. One of the name element in the XML document
could store customer name, while another name element could store
product names. This conflict can be resolved using namespaces.
Namespaces can be used to uniquely identify each name element.
Namespaces help the applications using XML documents to clearly
identify its elements and attributes, resolving any conflicts with
identically named elements and attributes in other XML documents.
Namespaces are thus used to organize the elements and attributes of
an XML documents into separate collections.

NAME SPACES in detail


For example, When an XML document have a <table> tag for a category of
furniture and a HTML <table> tag for information tables. Clearly, software
systems that process this XML document must be capable of
unambiguously recognizing the element names in the XML document.
<?xml version="1.0" encoding="UTF-8" ?>
<imports>
<table>
<tr><td>Kerala</td><td>Rosewood</td></tr>
<tr><td>Tamil Nadu</td><td>Teak</td></tr>
</table>
<table>
<cost> $123</cost>
<type>Teak</type>
<shape>square</shape>
</table>
</imports>

Solution using NAME SPACES


Namespaces is used to resolve the conflict as follows:<?xml version="1.0" encoding="UTF-8" ?>
<imports xmlns:c=http://it.site.jusl.ac/ns/client
xmlns:t= http://www.furnitureitem.com/ns/furniture>
<c:table>
<c:tr><c:td>Kerala</c:td><c:td>Rosewood</c:td></c:tr>
<c:tr><c:td>Tamil Nadu</c:td><c:td>Teak</c:td></c:tr>
</c:table>
<t:table>
<t:cost> $123</t:cost>
<t:type>Teak</t:type>
<t:shape>square</t:shape>
</t:table>
</imports>

Namespaces and NEED FOR


NAMESPACES

XML Namespace is a mechanism to avoid name conflicts by


differentiating elements or attributes within an XML document
that may have identical names, but different definitions.
XML namespaces are used for providing uniquely
named elements and attributes in an XML document.
A namespace name is a uniform resource identifier (URI)
XML Namespace creates a qualified name for an XML
Element. This provides a unique identifier for the element or
attribute. The namespace is identified as a URI e.g.http://
www.somecomapny.com/order/ADDR

NAMESPACE and URI


URI Uniform Resource Identifier is a generic term for identifying
namespaces (or any resources) and addresses on the world wide web.
URL Universal Resource Locator is a unique address that identifies a
web page.
A URL is a URI that happens to be the Internet address of some
resource.
Namespaces are identified by a URI because it is guaranteed to be
unique which means that an associated namespace is also unique.
URL that you use to identify a namespace does not need to exist. The
URL associated with a namespace only needs to be a unique name to
identify the namespace.

NAMESPACE declaration
The form of a namespace declaration for an element is
<element_name xmlns[:prefix] = URI>

The square brackets indicate that what is within them is


optional.

xmlns attribute identifies the namespace and makes


association between a prefix and the created namespace.

The prefix, if included, is the name that must be attached to


the names in the declared namespace. If the prefix is not
included, the namespace is the default for the document.

Prefix and colon is needed for the opening as well as ending


tags for each element .

NAMESPACE DECLARATION

<element_name xmlns[:prefix] = URI>


A prefix is used for two reasons. First, most URIs are
too long to be typed on every occurrence of every
name from the namespace.
Second, a URI includes characters that are invalid in
XML.
Note that the element for which a namespace is
declared is usually the root of a document.

EXAMPLE FOR NAMESPACES

As an example of a prefixed namespace declaration, consider


the following:
<birds xmlns:bd = http://www.audubon.org/names/species>

Within the birds element, including all of its children elements, the
names from the given namespace must be prefixed with bd, as
in the following element:
<bd:lark>

EXAMPLE FOR NAMESPACES

If an element has more than one namespace declaration, they


are declared as in the following example:

One of the namespaces can be specified as the default by


omitting the prefix in any namespace declaration (as in the
example in the next slide).

EXAMPLE FOR NAMESPACES

The following example declares two namespaces. The first is


declared to be the default namespace; the second defines the
prefix, cap. One of the namespaces can be specified as the
default by omitting the prefix in any namespace declaration as
can be seen in this example.

Each state element has name and population child elements


from both namespaces.

Example for NAMESPACES

XHTML documents declares the xmlns


namespace on the root element, html:
<html xmlns = http://www.w3.org/1999/xhtml>

This declaration defines the default namespace


for XHTML documents, which is
http://www.w3.org/1999/xhtml.

DEFAULT NAMESPACE
Default namespace is applied to all the elements and nested
elements beneath the element that declares the namespace.
Prefix may be omitted.
A default namespace for an entire XML document can be
selected by using the xmlns attribute in the documents root
element.

Example for default namespaces


<?xml version="1.0" encoding="UTF-8" ?>
<purchaseOrder xmlns="http://www.somecomapany.com/order/PO>
<firstname>Fred</firstname>
<surname>Bloggs</surname>
<address>
<addressLine1>2 South Road</address1>
<addressLine2/>
<town>Colchester</town>
<county>Essex</county>
<postcode>CO8 9SR</postcode>
</address>
<telephone>01334 234567</telephone>
</purchaseOrder>

A default namespaces is defined within the purchaseOrder element. All


elements descending the purchaseOrder element will inherit the
namespace.

EXAMPLE FOR DEFAULT NAMESPACE : A default


namespace is created by assigning URL
www.GosselinConsulting.com/ns/renovation to the
xmlns attribute in the root element
<?xml version=1.0 encoding=iso-8859-1 standalone=yes?>
<renovation xmlns=http://www.GosselinConsulting.com/ns/renovation>
<hardware>
<description>plumbing</description>
<cost>$15,000</cost>
</hardware>
<hardware>
<description>electrical</description>
<cost>$11,000</cost>
</hardware>
</renovation>

Example for DEFAULT NAMESPACE : Here there are


two default namespaces, one for the plumbing
<hardware> element and one for the electrical
<hardware> element
<?xml version=1.0 encoding=iso-8859-1 standalone=yes?>
<renovation>
<hardware xmlns=http://www.GosselinConsulting.com/ns/plumbing >
<description>plumbing</description>
<cost>$15,000</cost>
</hardware>
<hardware xmlns=http://www.GosselinConsulting.com/ns/electrical >
<description>electrical</description>
<cost>$11,000</cost>
</hardware>
</renovation>

Explicit NAMESPACE
Namespaces that are assigned to individual elements in an XML
document are called explicit namespaces.
To explicitly declare a namespace for a specific element in an XML
document, you must assign a prefix to the namespace declaration using
the following syntax
<element_name xmlns:prefix=URI>

Prefix and colon is needed for the opening as well as ending tags
for each element.

EXAMPLE FOR EXPLICIT NAMESPACE: A default and


an explicit namespace within the root element
'renovation' of the XML document
<?xml version=1.0 encoding=iso-8859-1 standalone=yes?>
<renovation xmlns=http://www.GosselinConsulting.com/ns/construction
xmlns:computers=http://www.GosselinConsulting.com/ns/computers >
<hardware >
<description>plumbing</description>
<cost>$15,000</cost>
</hardware>
<computers:hardware>
<computers:description>electrical</computers:description>
<computers:cost>$11,000</computers:cost>
</computers:hardware>
</renovation>

NAMESPACE : any elements that do not contain an explicit


namespace declaration belong to default namespace
<?xml version=1.0 encoding=iso-8859-1 standalone=yes?>
<weather xmlns= http://www.DonGosselin.com/ns/SanFrancisco
xmlsns:losangeles=http://www.DonGoselin.com/ns/LosAngeles>
<weather_reading>
<date>january 27, 2005 </date>
<tempature>74.0</temperature>
<rainfall>0 cm</rainfall>
<humidity>20</humidity>
</weather_reading>
<weather_reading>
<date>january 28, 2005 </date>
<tempature>50.0</temperature>
<rainfall>0.1 cm</rainfall>
<humidity>26</humidity>
</weather_reading>
</weather>

Example for Explicit NAMESPACE


<?xml version=1.0 encoding=iso-8859-1 standalone=yes?>
<weather xmlns= http://www.DonGosselin.com/ns/SanFrancisco
xmlsns:losangeles=http://www.DonGoselin.com/ns/LosAngeles>
<losangeles:weather_reading>
<losangeles:date>january 27, 2005 </losangeles:date>
<losangeles:tempature>74.0</losangeles:temperature>
<losangeles:rainfall>0 cm</losangeles:rainfall>
<losangeles:humidity>20</losangeles:humidity>
</losangeles:weather_reading>
<weather_reading>
Explicit namespace for
<date>january 28, 2005 </date>
weather in Los Angeles
<tempature>50.0</temperature>
<rainfall>0.1 cm</rainfall>
<humidity>26</humidity>
</weather_reading>
</weather>

Das könnte Ihnen auch gefallen