ITworld.com
  Search  
Menu Changing the way you view IT
General Entities
Sign up for XML IN PRACTICE
More Newsletters
 
 

XML IN PRACTICE --- 11/16/2000



Mark Johnson

You're creating XML content and you’re repeatedly using some strings. Or, maybe there are strings you would like to change from document to document. Or, perhaps some strings in your documents, like copyright years, change over time.
Advertisement
On this topic




If you have any of these concerns, then general entities may be for you. Inherited from SGML, a general entity allows you to define a symbolic name for a chunk of text. You have likely already used general entities without knowing it. The XML and HTML notation < is a reference to the general entity lt, defined in the XML and HTML specifications. It evaluates to a single left angle bracket, or less- than sign ('<').

However, you may not have known that you can define your own entities in your DTD, using the <!ENTITY> notation. Here's an example:

<pre>
<xml version="1.0"/> <font color="red"><!-- Define general entities in the DTD --></font> <!DOCTYPE Letter [ <font color="blue"> <!ENTITY recipient 'Mom'> <!ENTITY closing 'Love'> <!ENTITY relationship 'mother'> <!ENTITY sender 'Your loving son'></font> ]>

<Letter>
Dear <font color="blue">&recipient;</font>,

You're the best <font color="blue">&relationship;</font> I could ever hope for. Can I borrow some money?

<font color="blue">&closing;</font>,

<font color="blue">&sender;</font>
</Letter> </pre>

This document will be parsed as:

Dear Mom

You're the best mother I could ever hope for.
Can I borrow some money?

Love,

Your loving son

The <!ENTITY...> lines define entities, and entity names delimited by ‘&' and ';' reference those entities. (Entity definitions, entity references, and replacement text all appear in blue above.) Parsers replace entity references with the text definitions of those entities.

Entities can be used to manage parametric text in structured text documents. So, if you don't get any money from Mom, try sending the same letter, but change the DTD:

<pre>
<!DOCTYPE Letter [ <!ENTITY recipient 'Dad'> <!ENTITY closing 'Cheers, Pop!'> <!ENTITY relationship 'father'> <!ENTITY sender 'Your favorite kid'> ]> </pre>

 

Mark Johnson is president of Elucify Technical Communications, a Colorado-based training and consulting company dedicated to clarifying novel or complex ideas through clear explanation and examples.

Sponsored links
KODAK i1400 Series Scanners stand up to the challenge
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.