Namespace in xml

Namespace

Since XML is able to communicate between several different platforms, overlapping of elements tends to be a problem that occurs from time to time.  Basically, when the writer has the freedom to create elements, there is a chance that the name used already exists for another element.  Take the example below where an XML document is being created that includes HTML and there are two different elements for “body”.

Example: <?xml version=”1.0″ encoding=”ISO-8859-15″?>

<html>

<body>

<p>My School Information</p>

</body>

 

<body>

<course>Calculus</course>

<grade>A</grade>

</body>

 

</html>

 

This problem can be solved by using an XML namespace to make a distinction between the two elements.  A namespace is a special attribute that can be placed in an XML tag or prefix: “xmlns:”  Like all other attributes, this attribute must be set to some value and that value cannot be the same as any other namespace in the document.  The item commonly used here is a Uniform Resource Identifier (URI) or a URL.   The example below shows how this is done.  In the example, the namespace attribute and its use is highlighted in red.

Example: <?xml version=”1.0″ encoding=”ISO-8859-15″?>

<html:html xmlns:html=’http://www.mysite.org/safe/space/’>

<html:body>

<html:p>My School Information</html:p>

</html:body>

 

<school:body xmlns:school=’http://www.myschool.edu/grades’>

< school:course>Calculus</course>

< school:grade>A</grade>

</ school:body>

 

</html:html>

This entry was posted in XML Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published.