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: Customize a DTD with parameter entities

Allow users to choose which form of the DTD they want

Nicholas Chase (nicholas@nicholaschase.com), President, Chase and Chase, Inc.
Photo of Nicholas Chase
Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, FL, USA, and is the author of three books on Web development, including Java and XML From Scratch (Que) and the upcoming Primer Plus XML Programming (Sams). He loves to hear from readers and can be reached at: nicholas@nicholaschase.com.

Summary:  Creating a Document Type Definition (DTD) and using it to validate your document is a good way to ensure that the data fits a particular structure. In some cases, however, you want to give users some control over the structure. Parameter entities enable you to create a structure that allows a document author to choose from two or more possible DTD structures without giving that person control over the actual DTD.

View more content in this series

Date:  01 Jul 2002
Level:  Introductory

Comments:  

Note: This tip assumes that you have a basic understanding of DTDs and a validating parser to check your structure. Any validating parser, such as JAXP, will do; the tip discusses only XML and DTD files.

The document

In this tip, I'm dealing with documents from the Millennium Memory Project, which catalogs home movies submitted to a central location. In some cases, these documents take the long, or full form, as in Listing 1:


Listing 1. The document -- full form
                

<?xml version="1.0"?>
<memories>
 <memory tapeid="T1">
  <media mediaid="T1" status="vhs" />
  <subdate>2001-05-23</subdate>
  <donor>John Baker</donor>
  <subject>Fishing off Pier 60</subject>
  <location>
   <description>Outside in the woods</description>
  </location>
 </memory>
 <memory tapeid="T2">
  <media mediaid="T2" status="vhs"/>
  <subdate>2001-05-18</subdate>
  <donor>Elizabeth Davison</donor>
  <subject>Beach volleyball</subject>
  <location>
   <place>Clearwater beach</place>
  </location>
 </memory>
</memories>

Sometimes users will submit only the very basic information or the short form, as in Listing 2:


Listing 2. The document -- short form
                

<?xml version="1.0"?>
<memories>
   <memory tapeid="T1">
      <media mediaid="T1" status="vhs" />
      <subdate>2001-05-23</subdate>
      <subject>Fishing off Pier 60</subject>
   </memory>
   <memory tapeid="T2">
      <media mediaid="T2" status="vhs"/>
      <subdate>2001-05-18</subdate>
      <subject>Beach volleyball</subject>
   </memory>
</memories>

The DTD will need to permit the user to choose between these two structures.


A conditional DTD

To make this possible, you need to create a DTD that includes both definitions -- but not at the same time. To do this, you'll create a conditional section within a DTD:


Listing 3. Conditional sections within a DTD
                

<!ELEMENT memories (memory)* >

<!-- Short form -->
<![IGNORE[
   <!ELEMENT memory (media | subdate | subject+)* >
]]>

<!-- Full form -->
<![INCLUDE[
   <!ELEMENT memory (media | subdate | donor?| subject+| location)* >
   <!ELEMENT location (description|place) >
   <!ELEMENT description (#PCDATA) >
   <!ELEMENT place (#PCDATA) >
   <!ELEMENT donor (#PCDATA) >
]]>

<!ATTLIST memory tapeid IDREF #REQUIRED>
<!ELEMENT subdate (#PCDATA) >
<!ELEMENT subject (#PCDATA) >
<!ELEMENT media EMPTY >
<!ATTLIST media mediaid ID #REQUIRED
                status CDATA #IMPLIED >

In Listing 3, only the full form definitions are actually included in the DTD, because the short form definitions are excluded through the IGNORE keyword. You can include as many of these conditional sections as you wish within a DTD, controlling each of them individually.

To control these sections from the actual document, however, you need to create parameter entities.


Creating parameter entities

A parameter entity is a placeholder for a value, similar to a variable. Parameter entities can only be used within the DTD itself, and are distinguished by the percent sign (%).


Listing 4. Creating parameter entities
                
                <!ENTITY % short "IGNORE">
<!ENTITY % full "INCLUDE">

<!ELEMENT memories (memory)* >

<!-- Short form -->
<![%short;[
   <!ELEMENT memory (media | subdate | subject+)* >
]]>

<!-- Full form -->
<![%full;[
   <!ELEMENT memory (media | subdate | donor?| subject+| location)* >
   <!ELEMENT location (description|place) >
   <!ELEMENT description (#PCDATA) >
   <!ELEMENT place (#PCDATA) >
   <!ELEMENT donor (#PCDATA) >
]]>

<!ATTLIST memory tapeid IDREF #REQUIRED>
<!ELEMENT subdate (#PCDATA) >
<!ELEMENT subject (#PCDATA) >
<!ELEMENT media EMPTY >
<!ATTLIST media mediaid ID #REQUIRED
                status CDATA #IMPLIED >

When an application evaluates the DTD, it replaces the parameter entity with its value -- in this case IGNORE or INCLUDE -- so the full form definitions are still in effect. To change the DTD so it uses the short form, simply change the values of the short and full entities to INCLUDE and IGNORE, respectively.

This is certainly more convenient than having to rebuild the DTD, but the real power comes when you can allow the user to make the choice from within the XML document itself.


Enabling the user to choose

You can permit the user to choose which form of the DTD he or she wants based on two factors:

  • It is possible to have both an internal and external subset of a DTD
  • The internal subset always takes precedence over the external subset

For these reasons, the user can redefine the values of the short and full parameter entities from within the document:


Listing 5. Redefining the parameter entities
                

<?xml version="1.0"?>
<!DOCTYPE memories SYSTEM "memory.dtd" [
   <!ENTITY % short "INCLUDE">
   <!ENTITY % full "IGNORE">
]>
<memories>
   <memory tapeid="T1">
      <media mediaid="T1" status="vhs" />
      <subdate>2001-05-23</subdate>
      <subject>Fishing off Pier 60</subject>
   </memory>
   <memory tapeid="T2">
      <media mediaid="T2" status="vhs"/>
      <subdate>2001-05-18</subdate>
      <subject>Beach volleyball</subject>
   </memory>
</memories>

The external DTD subset, represented by the definitions in the memory.dtd file, is still in effect, but the definitions in the internal subset override the parameter entities, enabling the short form and disabling the full form.


Summary

This tip demonstrates how to create conditional DTDs and allow the user to choose a particular form from within the document. You can create unlimited permutations in this way, including the inclusion or exclusion of external information.


Resources

About the author

Photo of Nicholas Chase

Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, FL, USA, and is the author of three books on Web development, including Java and XML From Scratch (Que) and the upcoming Primer Plus XML Programming (Sams). He loves to hear from readers and can be reached at: nicholas@nicholaschase.com.

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=12126
ArticleTitle=Tip: Customize a DTD with parameter entities
publish-date=07012002
author1-email=nicholas@nicholaschase.com
author1-email-cc=dwxed@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).