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.

They are just unique identifiers. . <cars:part xmlns:cars=³URI´> ± URIs are not a pointer to information about the Namespace. You cannot resolve XML namespace URIs.Definition ‡ Namespace is a mapping between an element prefix and a URI ± cars is the prefix in this example.

‡ Inside the DTD file. .Ways to Specify Namespaces ‡ Inside the XML 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. <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> . The specification given for the that element is also valid for all elements occurring inside it.

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> .

<!ELEMENT cars> <!ATTLIST cars xmlns:part CDATA #FIXED ³http://www.org/1999/cars´> .w3.DTD File Here is a sample Namespace specification within a DTD.

Questions ‡ Any questions? ‡ Any comments? .

Sign up to vote on this title
UsefulNot useful