Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

VB.

NET & XML

Unit 10

Unit 10
Structure: 10.1 Introduction Objectives 10.2 10.3 10.4 10.5 Namespace Examples Summary Terminal Questions

XML Namespaces

10.1 Introduction
This unit introduces the concepts of XML namespaces along with few examples describing each. Objectives: The objective of this unit is to understand the XML namespaces. XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references. At the end of this unit students know how to use this namespace concept and the use of this concept.

10.2 Namespace
Namespace allows us to define the elements and attributes with a unique identification. This is similar to packages concept we have in java. This allows defining the elements in different XSD documents and references them uniquely.

Sikkim Manipal University

Page No. 206

VB.NET & XML

Unit 10

Example: <staff> <name>hai</name> <dept> <name>hello</name> </dept> <room>35536</room> </staff> In this example we have two elements called name but they each represent different things and have different meanings. Applications confuse these two items, thus rendering the whole XML document useless. In a small document that is not a problem as you can simply invent a new name for one of the elements. For large organizations there are more number of XML Schemas, for this purpose we use the concept called namespaces. Name Space Declaration To declare a name space for the types defined in our XML schema document, we use target Name Space attribute of <xsd: schema> tag. The target namespace takes a URI (Uniform Resource Indicator) that is used to uniquely refer the types. Ex: <xsd:schema targetNameSpace=http://URI/mytypes.> .. </xsd: schema> To refer types/elements with its unique name space: Option 1: General Namespace In this case we declare the name space with a prefix that can be used in the document to refer the types/elements.
Sikkim Manipal University Page No. 207

VB.NET & XML

Unit 10

Syntax: xmlns: <prefix>=URI Ex: <ele xmlns:mytypes=http://www.w3.org/mytypes > . </ele> NOTE: The scope of the declared prefix will be with in the element in which it is declared, that is it will be available for its Childs and descends including for itself. Option 2: Default Namespace In this case we declare a name space without a prefix as shown below: Syntax: <ele xmlns= http://www.w3.org.com/mytypes > </ele> So, that all the unqualified elements and attributes with in its scope comes under this name space (i.e. the elements and attributes without prefix are considered as unqualified elements/attributes).

10.3 Examples
Name Conflicts In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications. This XML carries information about a company (emp details): <company> <eno>1</eno>
Sikkim Manipal University Page No. 208

VB.NET & XML

Unit 10

<ename>Hello</ename> </company> This XML carries information about a company (dept details): <company> <dno>12345</dno> <dname>computers</dname> </company> If these XML fragments were added together, there would be a name conflict. Both contain a <company> element, but the elements have different content and meaning. Solving the Name Conflict Using a Prefix: Name conflicts in XML can easily be avoided using a name prefix. This XML carries information about an emp and dept: <e:company> <e:eno>1</e:eno> <e:ename>Hello</e:ename> </e:company> <d:company> <d:dno>12345</d:dno> <d:dname>computers</d:dname> </d:company> In the example above, there will be no conflict because the two <company> elements have different names. XML Namespaces The xmlns Attribute When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element.

Sikkim Manipal University

Page No. 209

VB.NET & XML

Unit 10

Option 1: General Name space Example The namespace declaration has the following syntax. xmlns:prefix="URI". <root> <e:company xmlns:e=http://www.w3.org/emp/> <e:eno>1</e:eno> <e:ename>Hello</e:ename> </e:company> <d:company xmlns:d=http://www.w3.org/dept/> <d:dno>12345</d:dno> <d:dname>computers</d:dname> </d:company> </root> In the example above, the xmlns attribute in the <company> tag give the e: and d: prefixes a qualified namespace. When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace. Namespaces can be declared in the elements where they are used or in the XML root element: <root xmlns: e=http://www.w3.org/emp/xmlns:d=http://www.w3.org/dept/ > <e:company > <e:eno>1</e:eno> <e:ename>Hello</e:ename> </e:company> <d:company > <d:dno>12345</d:dno> <d:dname>computers</d:dname> </d:company> </root>

Sikkim Manipal University

Page No. 210

VB.NET & XML

Unit 10

Note: The namespace URI is not used by the parser to look up information. The purpose is to give the namespace a unique name. However, companies often use the namespace as a pointer to a web page containing namespace information. Option 2: Default Namespaces Example Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax: xmlns="namespaceURI" This XML carries emp table information: <company xmlns=http://www.w3.org/emp/> <eno>1</eno> <ename>Hello</ename> </company> This XML carries information about a dept: <company xmlns=http://www.w3.org/dept/> <dno>12345</dno> <dname>computers</dname> </company>

10.4 Summary
XML Namespaces provide a method to avoid element name conflicts. To refer types/elements with its unique name space, we have two options. They are General, default namespaces.

10.5 Terminal Questions 1. Explain XML Namespaces with an example.


2. Write the syntax for general name space and give examples. 3. Write the syntax for default name space and give examples.
Sikkim Manipal University Page No. 211

You might also like