Tuesday, November 8, 2011

What is Xml NameSpace

Hi
I am start with some xml example let check this out......
<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>
And another example of xml is
<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>
As per the example both xml tag start with Table tag . so when we combined to gather there may be the chance of conflict.
An XML parser will not know how to handle these differences.

We can resolve the problem by Tar prefix like 
<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table> 
The namespace is defined by the xmlns(xml namespace) attribute in the start tag of an element.
The namespace declaration has the following syntax. xmlns:prefix="URI".

<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
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:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture"
>

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root> 

Namespaces in Real Use

XSLT is an XML language that can be used to transform XML documents into other formats, like HTML.
In the XSLT document below, you can see that most of the tags are HTML tags.
The tags that are not HTML tags have the prefix xsl, identified by the namespace xmlns:xsl="http://www.w3.org/1999/XSL/Transform":

Thanks It all about xml namespace . I will  post further in our next session.



No comments:

Post a Comment