Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Documents : Page 10

XML documents are similar to HTML documents. They contain information and markup tags that define the information, and are saved as ASCII text.


advertisement
XML Entities
An entity is a short cut to a set of information.

When you use an entity, it "expands" to its full meaning, but you need only type the shorter entity name during data entry. You might think of an entity as being a bit like a macro—it is a set of information that can be used by calling one name.

XML defines two types of entities. The general entity, which we'll talk about here, is used in XML document. The parameter entity is used in DTDs. General entities are easy to spot: they begin with the ampersand and end with the semicolon, like this:



&entity-name;

Uses for Entities
Entities are a way to make entering and managing data easier.

You've probably already used entities without calling them that. If you've ever entered the characters &lt; to create the < symbol, you've used an entity. This keystroke combination is a standard predefined entity in both HTML and XML that lets you access a particular ascii character without having to memorize the character set number.

Here are a few reasons you might want to define and use entities:

  • Entities save typing. Suppose you have a paragraph, like a copyright notice, that you use in every single document. You could type that notice over and over again. Or, you could use an entity to call it forth in place.
  • Entities can reduce errors. By the 101st time you type that copyright notice, it is likely your poor fingers will be so tired you'll make an error and set your copyright for 1989 instead of 1999. Using an entity can reduce the potential for these types of errors.
  • Entities are easy to update. It is time to update that copyright notice—with an entity you can make the change in one place and be done with it. Without an entity you'd be searching and replacing throughout your document set.
  • Entities can act as placeholders for TBD information. Maybe legal hasn't quite finalized what they want that copyright notice to say. That doesn't have to stop production—you can use and entity and when the final wording comes down, the entity will automatically display the new, corrected version in all your documents.

You can get quite creative with the use of entities, and even have documents that are constructed entirely from entities. Here's an example:

You want to create different documents, each contains a set of bios for members of your staff. You'll have an executive set, a set for each product line, a set for six different regions around the world ... subsets of the same content appears in each.

One approach you could take is creating 10 or 12 separate flat files, with the appropriate biography information into each. But an easier way is to create a small file for each bio, then call each into the executive page, the European page, the Flying Toys Division page and so on via an entity.

Here's how the content code for your Flying Toys Division Page might look. Upon display, the entities would expand and you'd see the full bios of each person. If you needed to change the bios, you could do it in one place. If the product manager changed, all your pages would be automatically updated with the new person.

Click anywhere in the code to see how it might expand into a displayed document:

<HEAD>The Faces Behind Flying Toys!</HEAD>
<BIO>&bio-ft-div-head;</BIO>
<BIO>&bio-ft-prod-mgr;</BIO>
<BIO>&bio-ft-designer;</BIO>
<BIO>&bio-ft-lead-engineer;</BIO>

Defining Entities
You can define entities in your local document as part of the DOCTYPE definition. You can also link to external files that contain the entity data. This, too, is done through the DOCTYPE definition. A third option is to define the entities in your external DTD.

Use a local definition when the entity is being used only in this one particulars file. Use a linked, external file when the entity being used in many document sets.

To define an entity:

  1. Start your DOCTYPE definition as usual, like this:

    <!DOCTYPE

  2. Now mark that you are defining some data by entering a square bracket:

    <!DOCTYPE [

  3. Start the entity definition, with a less than sign, an exclamation mark, and the phrase ENTITY, all in caps:

    <!DOCTYPE [
    <!ENTITY

  4. Type the name of the entity. Type it using the capitalization that you will use when calling it later on.

    <!DOCTYPE [
    <!ENTITY copyright

  5. If you are defining the entity locally, type the value of the entity, surrounded by quotes, and then close the entity definition with a greater than sign.

    <!DOCTYPE [
    <!ENTITY copyright "Copyright 2000, As The World Spins Corp. All rights reserved. Please do not copy or use without authorization. For authorization contact legal@worldspins.com.">

  6. If you are defining an entity in an external, ascii text file, put in a pointer to the external file, then close the entity definition with a greater than sign.

    <!DOCTYPE [
    <!ENTITY copyright SYSTEM "http://www.worldspins.com/legal/copyright.xml">

  7. Create all your entity definitions. When you are done, close the DOCTYPE definition with a square brace and a greater than sign.

    <!DOCTYPE [
    <!ENTITY copyright "Copyright 2000, As The World Spins Corp. All rights reserved. Please do not copy or use without authorization. For authorization contact legal@worldspins.com.">
    <!ENTITY trademark SYSTEM "http://www.worldspins.com/legal/trademark.xml">
    ]
    >

Using Entities

To use an entity in your document, just call it by name. The name begins with an & and ends with a semi-colon.

Click anywhere on this code to see how it would display, assuming of course, that it was linked to a style sheet.

<?xml version="1.0">
<!DOCTYPE [
<!ENTITY copyright "Copyright 2000, As The World Spins Corp. All rights reserved. Please do not copy or use without authorization. For authorization contact legal@worldspins.com.">
<!ENTITY trademark SYSTEM "http://www.worldspins.com/legal/trademark.xml">
]
>

<PRESSRELEASE>

<HEAD>Mini-globe revolutionizes keychain industry</HEAD>
<LEAD>

Today As The World Spins introduces a new approach to key chains. With the new MINI-GLOBE keys can be kept inside a chain, called for upon demand, and stored safely. Never more will consumers lose a key or stand at a door flipping through a stack of keys seeking the right one.
</LEAD>
<LEGAL>
&trademark;
&copyright;
</LEGAL>
</PRESSRELEASE>





Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date