XML Namespaces Andrey Smirnov CSCI 7818 September 21
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>
DTD File Here is a sample Namespace specification within a DTD. <!ELEMENT cars> <!ATTLIST cars xmlns: part CDATA #FIXED “http: //www. w 3. org/1999/cars”>
Questions • Any questions? • Any comments?
- Slides: 13