Declaring Attributes
Home ] Up ] Operators & Keywords ] Sharing DTDs ] [ Declaring Attributes ]

 

 

The example we have just seen contains only XML element (tag) declarations:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT memo (from, to+, body, priority?)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT to (name | department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT priority (high | medium | low)>
<!ELEMENT high EMPTY>
<!ELEMENT medium EMPTY>
<!ELEMENT low EMPTY>

How do we declare XML attributes in the DTD?

Here's how:

Imagine that we have an XML document for a video store.  The document contains an element <movie>, which has a number of attributes. For example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE video-store SYSTEM "movies1.dtd">
<video-store>
<movie id="m783564" name="The Magnificent Seven" rating="G"/>
<movie id="m14745" name="The Color Purple" rating="PG"/>
<movie id="m84526" name="The Piano"/>
<movie id="m274098" name="Animal House" released="1981"/>
</video-store>

We want to enforce the following rules about these attributes:

  • The movie must have a unique identifier (id)
  • It must have a name
  • It may optionally have a rating.  The allowed ratings are:
    • G - General Audiences - All ages admitted.
    • PG - Parental Guidance Suggested - Some material may not be suitable for young children. These films contain some mild language, humor, and/or violence.
    • PG-13 - Parents Strongly Cautioned - Some material may be inappropriate for children under 13. These films are sexuality, language, humor, and/or violence.
    • R - Restricted - Under 17 requires accompanying parent or adult guardian 21 years or older with photo I.D. These films contain, strong adult language, strong sexuality, nudity, strong violence, and/or gore, and drug use.
    • NC-17 - No One 17 And Under Admitted (18 and older ONLY) Films contain excessive graphic violence, sex, aberrational behavior, drug abuse, strong adult language, or any other elements which, when present, most parents would consider too strong and therefore off-limits for viewing by their children.
    If a rating is not explicitly specified, the rating defaults to R.
  • It may optionally have a year of release (released).  There is no default value for this attribute.

Here is the movies1.dtd for this document:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Enter you external DTD here -->
<!ELEMENT video-store (movie+)>
<!ELEMENT movie EMPTY>
<!ATTLIST movie
          id          ID       #REQUIRED
          name        CDATA    #REQUIRED
          rating      (G|PG|PG13|R|NC17)        "R"
          released    CDATA    #IMPLIED
>

The format of an <!ATTLIST> is:

<!ATTLIST element-name
          attribute-name attribute-type default-declaration
	  ...
>

where:

  • element-name and attribute-name are any valid XML name
  • attribute-type may be any one of:
    • CDATA -- any string of text
    • NMTOKEN --  an XML name token
    • NMTOKENS -- one or more XML name tokens separated by whitespace
    • An enumeration -- a list of all possible values for the attribute, separated by vertical bars.
    • ID -- an XML name (not a name token) that is unique within the XML document.
    • IDREF -- refers to the ID type attribute of an element in the document.
    • IDREFS -- a whitespace-separated list of IDREFs.
    • ENTITY -- the name of an unparsed entity declared elsewhere in the DTD.
    • ENTITIES -- a whitespace-separated list of ENTITYs.
    • NOTATION -- the name of a notation declared in the document's DTD.
  • default-declaration may be one of:
    • #IMPLIED -- the attribute is optional; no default is specified
    • #REQUIRED -- the attribute is required.
    • #FIXED value -- the attribute value is constant and immutable;  the attribute has the specified value, whether or not the attribute is specified.
    • value -- the actual default value as a quoted string.
 
The page was last updated February 19, 2008