0% found this document useful (0 votes)
29 views

XMLNamespaces

This document discusses XML namespaces and how they are used to distinguish between duplicate element and attribute names. It defines a namespace as a mapping between an element prefix and a URI, and describes different ways to specify namespaces within an XML file or DTD file, including assigning namespaces to root elements, individual elements, attributes, and overriding namespaces. The purpose is to eliminate ambiguity when the same element or attribute name is used in different contexts.

Uploaded by

dnshkum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

XMLNamespaces

This document discusses XML namespaces and how they are used to distinguish between duplicate element and attribute names. It defines a namespace as a mapping between an element prefix and a URI, and describes different ways to specify namespaces within an XML file or DTD file, including assigning namespaces to root elements, individual elements, attributes, and overriding namespaces. The purpose is to eliminate ambiguity when the same element or attribute name is used in different contexts.

Uploaded by

dnshkum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

XML Namespaces

Andrey Smirnov
CSCI 7818
September 21, 2000
Overview
• Why do we need Namespaces in XML?
• Definition of Namespace.
• How do we specify Namespaces?
• Questions.
Why Namespaces?
The purpose of XML Namespaces is to distinguish
between duplicate element and attribute names.

For example:
<vehicles>
<sedans><price>$</price></sedans>
<trucks><price>$</price></trucks>
</vehicles>

Both “sedans” and “trucks” have the same “price”


element, so the parser doesn’t know which one is which.
Definition
• Namespace is a mapping between an element prefix
and a URI
– cars is the prefix in this example,
<cars:part xmlns:cars=“URI”>
– URIs are not a pointer to information about the
Namespace. They are just unique identifiers. You
cannot resolve XML namespace URIs.
Ways to Specify Namespaces
• Inside the XML file.
• Inside the DTD file.
XML File
Specify a Namespace for every element.

<lower:aaa xmlns:lower = “http://website/lowercase”>


<lower:bbb xmlns:lower = “http://website/lowercase”>
<lower:ccc xmlns:lower = “http://website/lowercase” />
</lower:bbb>
<upper:BBB xmlns:upper = “http://website/uppercase” >
<upper:CCC xmlns:upper = “http://website/uppercase” />
</upper:BBB>
</lower:aaa>
XML File
Specify all Namespaces within the root element. The
specification given for the that element is also valid for
all elements occurring inside it.

<lower:aaa xmlns:lower = “http://website/lowercase”


xmlns:upper = “http://website/uppercase”>
<lower:bbb >
<lower:ccc />
</lower:bbb>
<upper:BBB >
<upper:CCC />
</upper:BBB>
</lower:aaa>
XML File
Namespaces do not have to be specified explicitly with
prefixes. The attribute xmlns defines the default
namespace which is used for the element.

<aaa >
<bbb xmlns = "http://website/lowercase“>
<ccc />
</bbb>
<BBB xmlns = "http://website/uppercase" >
<CCC />
</BBB>
</aaa>
XML File
Attributes can be explicitly assigned a value and be
associated with a Namespace by using prefixes.

<lower:aaa xmlns:lower = “http://website/lowercase”


xmlns:upper = “http://website/uppercase”>
<lower:bbb lower:zz = “11” >
<lower:ccc upper:VV = “22” />
</lower:bbb>
<upper:BBB lower:sss = “***” />
</lower:aaa>
XML File
Attributes without a prefix do not belong to any
Namespace.

<lower:aaa xmlns:lower = “http://website/lowercase”


xmlns:upper = “http://website/uppercase”>
          <lower:bbb zz = “11” >
               <lower:ccc WW = “22” />
          </lower:bbb>
          <upper:BBB sss = “***” />
</lower:aaa>
XML File
A Namespace specification can be overridden using the
prefixes.

<lower:aaa xmlns:lower="http://website/lowercase">
<lower:bbb>
<lower:ccc xmlns:lower="http://website/uppercase">
<lower:ddd>It’s uppercase now.</lower:ddd>
</lower:ccc>
</lower:bbb>
</lower:aaa>
DTD File
Here is a sample Namespace specification within a DTD.

<!ELEMENT cars>
<!ATTLIST cars
xmlns:part CDATA #FIXED “http://www.w3.org/1999/cars”>
Questions
• Any questions?
• Any comments?

You might also like