Monday, October 10, 2005

Power of XML Entities

I have been using XML for quite sometime in my projects and was quite comfortable with the semantics and syntax. Today, I added a new tool to my XML arsenal - realized the true power of "entities". So far, my understanding of entities is that Entities are variables used to define shortcuts to common text viz. "& copy ;" for © etc. Now, I would like to paraphrase the statement to Entity references are references to entities. The reason for this ...

My requirement was to copy the contents of two config (XML) files into one single config file. The two files are in different directories. I was evaluating the possible solutions to pick an optimal option. A few lines of code did the work, in a elegant way:



<!ENTITY file1 PUBLIC "path1/file1.xml" "path1/file1.xml">
<!ENTITY file2 PUBLIC "path2/file2.xml" "path2/file2.xml">
<root>
&file1;
&file2;
</root>


The entities would get resolved while parsing the file and the contents get copied dynamically. This is such a useful feature, saved lot of LOC and moreover the bottleneck of "transformation" has been avoided. And to display the code fragment correctly on this page, I had to use "& lt ;" and "& gt ;" :-)

No comments: