3.4 General Entity Declarations

     

As you learned in Chapter 2, XML predefines five entities for your convenience:


<

The less-than sign, a.k.a. the opening angle bracket ( < )


&amp;

The ampersand ( & )


&gt;

The greater-than sign, a.k.a. the closing angle bracket ( > )


&quot;

The straight, double quotation marks (")


&apos;

The apostrophe, a.k.a. the straight single quote (')

The DTD can define many more entities. This is useful not just in valid documents, but even in documents you don't plan to validate.

Entity references are defined with an ENTITY declaration in the DTD. This gives the name of the entity, which must be an XML name , and the replacement text of the entity. For example, this entity declaration defines &super; as an abbreviation for supercalifragilisticexpialidocious:

 <!ENTITY super "supercalifragilisticexpialidocious"> 

Once that's done, you can use &super; anywhere you'd normally have to type the entire word (and probably misspell it).

Entities can contain markup as well as text. For example, this declaration defines &footer; as an abbreviation for a standard web page footer that will be repeated on many pages:

 <!ENTITY footer '<hr size="1" noshade="true"/> <font CLASS="footer"> <a href="index.html">O&apos;Reilly Home</a>  <a href="sales/bookstores/">O&apos;Reilly Bookstores</a>  <a href="order_new/">How to Order</a>  <a href="oreilly/contact.html">O&apos;Reilly Contacts</a><br> <a href="http://international.oreilly.com/">International</a>  <a href="oreilly/about.html">About O&apos;Reilly</a>  <a href="affiliates.html">Affiliated Companies</a> </font> <p> <font CLASS="copy"> Copyright 2004, O&apos;Reilly Media, Inc.<br/> <a href="mailto:webmaster@oreilly.com">webmaster@oreilly.com</a> </font> </p> '> 

The entity replacement text must be well- formed . For instance, you cannot put a start-tag in one entity and the corresponding end-tag in another entity.

The other thing you have to be careful about is that you need to use different quote marks inside the replacement text from the ones that delimit it. Here we've chosen single quotes to surround the replacement text and double quotes internally. However, we did have to change the single quote in "O'Reilly" to the predefined general entity reference &apos; . Replacement text may itself contain entity references that are resolved before the text is replaced . Self-referential and circular references are forbidden, however.

General entities insert replacement text into the body of an XML document. They can also be used inside the DTD in places where they will eventually be included in the body of an XML document, for instance in an attribute default value or in the replacement text of another entity. However, they cannot be used to provide the text of the DTD itself. For instance, this is illegal:

 <!ENTITY coordinate  "((x, y)  (y, x)  (  q  , r)  (r,  q  ))" > <!ELEMENT polygon (&coordinate;, &coordinate;, &coordinate;+)> 

Shortly, we'll see how to use a different kind of entitythe parameter entityto achieve the desired result.



XML in a Nutshell
XML in a Nutshell, Third Edition
ISBN: 0596007647
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net