Introducing Entities


Elements and attributes are primarily logical mechanisms. They enable authors to specify the logical meaning of document content. On the other hand, entities are primarily physical mechanisms. They enable authors to manipulate the physical structure of documents. As physical concepts, entities are interesting mostly to programmers. However, they do provide useful features for organizing XML documents, and you should have a high-level understanding of how they work.

An entity associates an entity name with a fragment of content. Authors define entities with an "!ENTITY" declaration at the beginning of a document or DTD. There are four types of entities in XML, three that apply to documents and one that applies to DTDs.

  • Internal parsed entities. These entities allow authors to define an alias for a fragment of text within a document. So the declaration <!ENTITY author "Kevin Dick"> would allow me to type "&author" within a document and have it replaced by "Kevin Dick" when the document was processed . Using internal entities can reduce the size of documents by replacing large pieces of recurring text with a shorter name. It also makes documents more extensible. If I decided that I wanted my name to appear as "Kevin S. Dick," I would have to change only the ENTITY declaration.

  • External parsed entities. These entities allow authors to include an entire file as part of a document. Using external entities, we could decompose the order document from Example 2-6 into three components :

     <!ENTITY addressBlock SYSTEM "./address.xml"> <!ENTITY lineItemsBlock SYSTEM "./lineitems.xml"> <!ENTITY paymentBlock SYSTEM "./payment.xml"> &addressSection; &lineItemsSection; &paymentSection; 

    Such decomposition would be useful for managing long documents. It would also be useful if different authors were responsible for creating different parts of the document. They could work on these files independently without worrying about overlapping changes.

  • Unparsed entities. These entities allow authors to insert arbitrary data in a document. This data may not be XML. It may not even be text. The XML processor will obviously not attempt to parse such an entity.

  • Parameter entities. These entities work only within DTDs. They enable the DTD designer to create reusable and extensible design elements for document structure. Remember the DTD for our order document in Example 2-9. One of the attributes of the "Card" element was "cardType." This attribute provided a list of acceptable credit cards.

     cardType (VISA  MasterCard  Amex) #REQUIRED 

    What if the company using this DTD decided to take Discover cards as well? This company might have many other document types with the same information. A parameter entity would allow DTD designers a means to update the acceptable cards at the beginning of each document.

     <!ENTITY % cardList "VISA  MasterCard  Amex    Discover"> CARDTYPE (%cardList;) #REQUIRED> 

Although XML documents and DTDs deliver significant benefits in exchanging information, it's only natural to want to enhance these capabilities even further. Much as with peeling an onion, addressing the most visible information exchange problem on the Internet reveals further possibilities for improvement. Chapter 3 introduces some of the new related standards that deliver such improvements.



XML. A Manager's Guide
XML: A Managers Guide (2nd Edition) (Addison-Wesley Information Technology Series)
ISBN: 0201770067
EAN: 2147483647
Year: 2002
Pages: 75
Authors: Kevin Dick

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