Thursday, January 22, 2009

How to create entity in xml files?

Xml file which is defined with several tags and elements in it, some times may require repeating the same text or content. In such case it is not needed to re write the same content again and again.
Example:-

For this below xml content
<?xml version="1.0" encoding="utf-8" ?>
<Tags>
<Tag1>
abcde
</Tag1>
<Tag2>
abcde
</Tag2>
</Tags>
</xml>
You can observe text “abcde” is been repeated twice.
Instead of repeating, we can declare “abcde” text at once and then re use the variable again and again where it is required.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE example [
<!ENTITY VAR1 “abcde”>]>
<Tags>
<Tag1>
&VAR1;
</Tag1>
<Tag2>
&VAR1;
</Tag2>
</Tags>
</xml>

To know in very clear about this, if you are aware about &nbsp; which we use in html or aspx or asp pages, they help to insert space “  “ charater.
For this it has an in built entity that places a space character when ever we refer &nbsp;


<!DOCTYPE example [
<!ENTITY &nbsp “ ”;>]>
So we can define many different kind of entities as explained above, which can be used at multiple instants by just calling
&[variable name];

No comments: