XML Namespaces

Estudies4you
XML Namespaces
XML Namespaces 
  • XML Namespaces avoid name conflicts in the XML document.
  • Name conflict occurs when we found two tags with the same name.
  • Name conflict can be resolved by using a prefix to the tag as shown below.
  • <P:name>Ramu</P:name>   ------------->  name of the person.
  • <A:name>Pappi</A:name> -------------> name of a dog.
  • When a namespace is defined for an element, all child elements with the same prefix are associated
  • with the same namespace.
  • In order to maintain uniqueness in the namespaces, we should define the namespaces xmlns' is an attributed tag used to define a name space. We can define Namespace in two ways, 
    • Defining at the time of usage,
<P:name xmlns:P="url">Ramu</P:name>
    • Defining at the root tag, 
<root xmlns:P="url">
<P:name> Ramu</P:name>
</root>

Entity References
Some characters have a special meaning in XML. If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. This will generate an XML error.
An example:
An XML tag: <message>if salary<2500 then </message>
Error Message: To avoid this error, replace the "<" character with an entity reference:
<message>if salary &lt; 2500 then </message>

There are five predefined entity references in XML: 
&It;
< 
 less than
&gt;
> 
greater than
&amp;
&
ampersand
&apos;
apostrophe
&quot;
quotation mark

Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->
With XML, White Space is Preserved.
HTML reduces multiple white space characters to a single white space:
HTML: Hello my name is Raju.
Output: Hello my name is Raju.

With XML, the white space in your document is not truncated.





To Top