Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Tip: Simplify with entity references

Variable substitution in XML documents

Brett McLaughlin (brett@newInstance.com), Author and Editor, O'Reilly Media Inc.
Photo of Brett McLaughlin
Brett McLaughlin has been working in computers since the Logo days (remember the little triangle?). He currently specializes in building application infrastructure using the Java language and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Summary:  XML is primarily a static language. However, by using entity references, you can perform a limited amount of dynamic substitution. This tip explains how to use entity references, and shows you how they are useful.

View more content in this series

Date:  01 Jan 2003
Level:  Introductory

Comments:  

XML was originally created simply to allow for documents to be authored (marked up) in a variety of formats. Because XML is primarily a language for representing static data, the idea of variables, value substitution, and other dynamic data representations were not considered (at least, not much!). As a result, XML documents often end up with redundant data, inconsistent data, and a variety of other problems that result from purely static data formats.

XML does have a limited facility for dynamic data, and it turns out that this facility can empower XML authors greatly. That facility, of course, is entity references. In this tip, I examine entity references in detail, explaining how they are used and what they offer you, the XML document author.

I realize that this is one of those topics in XML that can seem a little mysterious and obscure. However, you can simply think of an entity reference as a variable in XML; that variable has a value that's declared somewhere, and every time the variable occurs, the parser substitutes that value in the output. More specifically and accurately, an entity reference is like a static, final variable in the Java language. It cannot change from its initial value, which is defined in a DTD somewhere.

While many times an entity reference refers to an online resource, it can also have a value defined for it in a DTD, as in Listing 1.

<!ENTITY variableName "variable value">

Instead of typing "variable value" several times in your XML document (and possibly introducing typos and user error), you can just refer to the value through the reference, as shown in Listing 2.

<content>The variable's value is &variableName;.</content>

Of course, this seems pretty trivial, so let's look at a more realistic example. Listing 3 shows a simple XML document fragment that's intended for display on a Web page somewhere.

<page>
  <title>Simplify with entity references</title>
  <content type="html">
    <center><h1>Simplify with entity references</h1></center>
    <p>
      This tip, <i>Simplify with entity references</i>, details an underused facet of
	  XML document authoring. So on and so on, ad nauseum, ad infinitum.
    </p>
  </content>
</page>

Notice that the title "Simplify with entity references" was repeated three times. Not only does this introduce room for error, it makes it a pain to change all occurrences (of which there may be a dozen or more in this and related documents in the future). This makes the document a good candidate for an entity reference. First, add the definition of the entity to your DTD, as in Listing 4.

<!ENTITY articleTitle "Simplify with entity references">

Then, change the XML to look like that in Listing 5.

<page>
  <title>&articleTitle;</title>
  <content type="html">
    <center><h1>&articleTitle;</h1></center>
    
    <p>
      This tip, <i>&articleTitle;</i>, details an underused facet of
	  XML document authoring. So on and so on, ad nauseum, ad infinitum.
    </p>
  </content>
</page>

Now, by simply changing the entity reference's value, you can change all references in the XML document to the new value.

In addition, you can move the entity definition from the DTD into the XML document itself, as seen in Listing 6.

<?xml version="1.0"?>
<!DOCTYPE page [
  <!ENTITY articleTitle "O'Reilly J2EE Best Practices">
]>

<page>
  <title>&articleTitle;</title>
  <content type="html">
    <center><h1>&articleTitle;</h1></center>
    <p>
      This tip, <i>&articleTitle;</i>, details an underused facet of
	  XML document authoring. So on and so on, ad nauseum, ad infinitum.
    </p>
  </content>
</page>

While this won't necessarily improve the performance of your document parsing, it certainly is a better organizational approach, and makes maintenance significantly easier. The only time you might want to move the entity reference outside of the document is when multiple documents share data, and all use a shared entity reference.

This tends to be one of those features of XML that many people see and dismiss as trivial. However, in large documents entity references can be a life-saver, allowing you to avoid typos that result from typing the same data over and over again. Adding entity references to your arsenal of XML tools will only improve your document authoring. So brush up on the entity reference, and I'll see you soon!


Resources

  • Check out the SAX API to find out more about the Simple API for XML.

  • Take an in-depth look at SAX right here on developerWorks with Nicholas Chase's tutorial Understanding SAX, which examines the use of SAX 2.0. (September 2001)

  • Doug Tidwell's Introduction to XML tutorial on developerWorks provides a solid foundation for understanding XML issues like document rules, defining document content, programming interfaces, and XML standards. (August 2002)

  • Find more XML resources on the developerWorks XML zone. For a complete list of XML tips to date, check out the tips summary page.

  • IBM trial software: Build your next development project with trial software available for download directly from developerWorks.

  • Find out how you can become an IBM Certified Developer in XML and related technologies.

  • Want us to send you useful XML tips like this every week? Sign up for the developerWorks XML Tips newsletter.

About the author

Photo of Brett McLaughlin

Brett McLaughlin has been working in computers since the Logo days (remember the little triangle?). He currently specializes in building application infrastructure using the Java language and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12199
ArticleTitle=Tip: Simplify with entity references
publish-date=01012003
author1-email=brett@newInstance.com
author1-email-cc=htc@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).