·  XML – Namespaces

 

- Info:

  - This tutorial explaines why namespaces were introduced in XML and how they are used.

  - This tutorial is based on http://www.w3schools.com/xml/xml_namespaces.asp.

 

- Explanation:

  - X in XML stands for "eXtensible".

  - This means that XML structure is ment to be "eXtensible".

  - This means that an existing XML structure can be extended by adding tags from another XML structure.

  - If those two XML structures were created by two different persons it is possible that those persons have decided to use

    the same name for one of their tags like <table> tag for instance.

 

  - First person might be using <table> tag to define appearance of HTML table:

 

    <table>

      <tr> <td>Name </td> <td>Price</td> </tr>

      <tr> <td>Apple</td> <td>14   </td> </tr>

    </table>

 

  - Second person might be using <table> tag to define 3D image of a dinig table you can sit at:

 

    <table>

      <color>red</color>

      <width>123</width>

    </table>

 

  - Then in the resulting XML structure, <table> tag would have two different meanings:

 

    <table>

      <tr> <td>Name  </td> <td>Price</td> </tr>

      <tr> <td>Apple </td> <td>14   </td> </tr>

    </table>

 

    <table>

      <color>red</color>

      <width>123</width>

    </table>

 

  - Parsing engine that decides what to do with each tag by only using tag name and not analysing it's contents, wouldn't

    know what to do when it encounters <table> tag.

    It wouldn't know if it should construct HTML table or if it should construct 3D image a dining table.

  - To avoid such confusions XML Name Spaces were introduced.

  - At the point where you are using a tag you simply define to which namespace this tag belongs to:

 

    <table xmlns="HTML">

      <tr> <td>Name  </td> <td>Price</td> </tr>

      <tr> <td>Apple </td> <td>14   </td> </tr>

    </table>

 

    <table xmlns="Dining">

      <color>red</color>

      <width>123</width>

    </table>

 

  - Parsing engine uses xmlns attribute to create full tag names for that tag and all of its children:

 

    <HTML:table>

      <HTML:tr> <HTML:td>Name </HTML:td> <HTML:td>Price</HTML:td> </HTML:tr>

      <HTML:tr> <HTML:td>Apple</HTML:td> <HTML:td>14   </HTML:td> </HTML:tr>

    </HTML:table>

 

    <Dining:table>

      <Dining:color>red</Dining:color>

      <Dining:width>123</Dining:width>

    </Dining:table>

 

  - Another way to define tag's namespace is to use root element.

  - There for each namespace you can define prefix which is then used before tag name to specify that that tag belongs to that namespace:

 

    <root xmlns:h="HTML" xmlns:d="Dining">

    

      <h:table>

        <h:tr> <h:td>Name </h:td> <h:td>Price</h:td> </h:tr>

        <h:tr> <h:td>Apple</h:td> <h:td>14   </h:td> </h:tr>

      </h:table>

    

      <d:table>

        <d:color>red</d:color>

        <d:width>123</d:width>

      </d:table>

    

    </root>

 

  - This solution can work only if namespace's identifiers, "HTML" and "Dining", are unique.

    To ensure that, it was decided to use URI as the namespace identifier like http://www.ivoronline.com/3DElements.

    Since no one else can have the same URI as you have, no one else can create a tag with duplicate full name.