XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / Attribute declarations
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
A valid document must declare attributes as well as elements, using ATTLIST declarations. One ATTLIST can declare several attributes for one element type. Shared attributes must be declared separately for each element. Later pages explain how parameter entity references simplify repetition.
For example, this ATTLIST declares the attribute quelle on the element bild:
<!ATTLIST bild quelle CDATA #REQUIRED> It says the element bild has an attribute named quelle. The quelle value is character data, and quelle is required on every bild instance in the document.
One ATTLIST declaration can define several attributes for one element. This ATTLIST declaration defines not only quelle on the element bild but also breite, höhe and alt:
<!ATTLIST bild quelle CDATA #REQUIRED
breite CDATA #REQUIRED
höhe CDATA #REQUIRED
alt CDATA #IMPLIED
>Here quelle, breite and höhe are required, whereas alt is optional and may be omitted from bild elements. All four are character data, CDATA, the commonest type.
This means the same as four separate ATTLIST declarations. Using one ATTLIST declaration per attribute is a matter of taste, but experienced DTD designers usually prefer multiple attributes, equally readable with sufficient whitespace.
Attribute types
In merely well-formed XML, values may be any text, provided < and & are escaped as < and &. The quote character delimiting a value must also be escaped internally. DTDs impose tighter restrictions, even tighter than for element content: values can be unique, legal name tokens, or chosen from fixed lists.
XML has ten attribute types:
CDATANMTOKENNMTOKENSEnumeration
ENTITYENTITIESIDIDREFIDREFSNOTATION
These are the only types. A DTD cannot require an integer or a date between 1966 and 2004.
CDATA
A CDATA attribute accepts any text legal in a well-formed attribute. This most general type suits the alt attribute of a bild element, with no specific text structure.
<!ATTLIST bild alt CDATA #IMPLIED> It also covers prices, URLs, email and postal addresses, quotations and other data types more structured than plain strings but fitting no other attribute type:
<!ATTLIST sku
listenpreis CDATA #IMPLIED
empfohlener_verkaufspreis CDATA #IMPLIED
tatsächlicher_preis CDATA #IMPLIED
>
<!-- Alle drei Attribute sollten die Form $XX.YY haben. -->NMTOKEN
An XML name token closely resembles an XML name. It uses the same alphanumeric or ideographic characters and punctuation _ , - , . and :, with no whitespace. Unlike XML names, which start with letters, ideograms or underscore, any allowed character may begin a name token. 12 and .cshrc are therefore valid tokens but not names. Every XML name is a name token, but not conversely.
An attribute of type NMTOKEN holds an XML name token. For an attribute jahr on an element journal intended for integer years such as 1990 or 2015, choose NMTOKEN because all years are name tokens:
<!ATTLIST journal jahr NMTOKEN #REQUIRED>
This still permits years such as 99 or März but excludes some bad values, especially those containing spaces, such as A. D. 1990 or Sally had a little lamb.
NMTOKENS
An attribute of type NMTOKENS holds whitespace-separated name tokens, for example an attribute termine of an element auftritte with dates in the form 08-26-2000:
<auftritte termine="08-21-2001 08-23-2001 08-27-2001">Kat and the Kings</auftritte>Its declaration is:
<!ATTLIST auftritte termine NMTOKENS #REQUIRED> Dates such as 08/27/2001 do not work: slash is not a legal name character.
Enumeration
Enumeration alone is not an XML keyword: it lists allowed name-token values separated by vertical bars. These declarations require the attribute monat of the element datum to be a month name, the attribute tag a number from 1 to 31, and the attribute jahr a positive integer from 1970 to 2009:
<!ATTLIST datum monat (Januar | Februar | März | April | Mai | Juni
| Juli | August | September | Oktober | November | Dezember) #REQUIRED
>
<!ATTLIST datum tag (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25
| 26 | 27 | 28 | 29 | 30 | 31) #REQUIRED
>
<!ATTLIST datum jahr (1970 | 1971 | 1972 | 1973 | 1974 | 1975 | 1976
| 1977 | 1978 | 1979 | 1980 | 1981 | 1982 | 1983 | 1984 | 1985 | 1986
| 1987 | 1988 | 1989 | 1990 | 1991 | 1992 | 1993 | 1994 | 1995 | 1996
| 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006
| 2007 | 2008 | 2009 ) #REQUIRED
>
<!ELEMENT datum EMPTY>With this DTD, this element datum is valid:
<datum monat="Januar" tag="22" jahr="2001"/>These datum elements are invalid:
<datum monat="01" tag="22" jahr="2001"/>
<datum monat="Jan" tag="22" jahr="2001"/>
<datum monat="Januar" tag="02" jahr="2001"/>
<datum monat="Januar" tag="2" jahr="1969"/>
<datum monat="Janvier" tag="22" jahr="2001"/>This works because all values are legal name tokens; it fails for whitespace or punctuation other than underscore, hyphen, colon and period.
ID
An attribute of type ID must hold a unique XML name, not merely a token. No other attribute of type ID may share its value. Non-ID attributes do not count. Each element may have at most one ID attribute.
As the name suggests, ID attributes identify elements uniquely. ID attributes need not be named “ID” or “id”, though often they are. This ATTLIST declaration requires every element angestellter to have an attribute sozialversicherungsnummer of type ID:
<!ATTLIST angestellter sozialversicherungsnummer ID #REQUIRED> ID numbers are tricky: numbers are not XML names and hence not valid XML ID . Prefix an underscore or ordinary letter:
<angestellter sozialversicherungsnummer="_078-05-1120"/> IDREF
An attribute of type IDREF refers to an ID attribute on an element in the document, so must be an XML name. IDREF attributes usually express relationships beyond simple containment.
Imagine a document listing projekt and angestellter elements. Each projekt element has a projekt_id attribute of type ID, and each angestellter element has a sozialversicherungsnummer attribute of type ID. Each projekt element also has teammitglied children identifying its workers. Multiple workers per project and multiple projects per worker prevent simple parent-child modeling. Use the IDREF attribute as follows:
<projekt projekt_id="p1">
<ziel>Entwicklung eines strategischen Plans</ziel>
<teammitglied person="ss078-05-1120"/>
<teammitglied person="ss987-65-4320"/>
</projekt>
<projekt projekt_id="p2">
<ziel>Einsatz von Linux</ziel>
<teammitglied person="ss078-05-1120"/>
<teammitglied person="ss9876-12-3456"/>
</projekt>
<mitarbeiter sozialversicherungsnummer="ss078-05-1120">
<name>Fred Smith</name>
</mitarbeiter>
<mitarbeiter sozialversicherungsnummer="ss987-65-4320">
<name>Jill Jones</name>
</mitarbeiter>
<mitarbeiter sozialversicherungsnummer="ss9876-12-3456">
<name>Sydney Lee</name>
</mitarbeiter>Here the attribute id of the element projekt and the attribute sozialversicherungsnummer of the element mitarbeiter would have type ID. The attribute person of the element teammitglied would have type IDREF. Their ATTLIST declarations are:
<!ATTLIST mitarbeiter sozialversicherungsnummer ID #REQUIRED>
<!ATTLIST projekt projekt_id ID #REQUIRED>
<!ATTLIST teammitglied person IDREF #REQUIRED>These require the attribute person of the element teammitglied to match the ID of anything in the document. They do not require the attribute person of the element teammitglied to match only mitarbeiter IDs. A teammitglied could contain the ID of another project or itself: not necessarily correct, but valid.
IDREFS
An attribute of type IDREFS holds a whitespace-separated list of XML names, each an element’s ID in the document. It references several elements. Rewrite the project example, replacing the teammitglied children of the element projekt with a team attribute:
<projekt projekt_id="p1" team="ss078-05-1120 ss987-65-4320">
<ziel>Entwicklung eines strategischen Plans</ziel>
</projekt>
<projekt projekt_id="p2" team="ss078-05-1120 ss9876-12-3456">
<ziel>Einsatz von Linux</ziel>
</projekt>
<mitarbeiter sozialversicherungsnummer="ss078-05-1120">
<name>Fred Smith</name>
</mitarbeiter>
<mitarbeiter sozialversicherungsnummer="ss987-65-4320">
<name>Jill Jones</name>
<mitarbeiter sozialversicherungsnummer="ss9876-12-3456">
<name>Sydney Lee</name>
</mitarbeiter>The declarations are:
<!ATTLIST mitarbeiter sozialversicherungsnummer ID #REQUIRED>
<!ATTLIST projekt projekt_id ID #REQUIRED
team IDREFS #REQUIRED>ENTITY
An attribute of type ENTITY holds the name of an unparsed entity declared in the DTD. For example, a film element might have an entity attribute identifying an MPEG or QuickTime file to play when activated:
<!ATTLIST film quelle ENTITY #REQUIRED>
If the DTD declares an unparsed entity named X-Men-Trailer, this film element embeds the video in the document:
<film quelle="X-Men-Trailer"/>
See the page General entity declarations for a fuller discussion of unparsed entities.
ENTITIES
An attribute of type ENTITIES holds names of one or more unparsed entities declared in the DTD, separated by whitespace. A präsentation element might have an attribute ENTITIES listing JPEG files in playback order:
<!ATTLIST diashow dias ENTITIES #REQUIRED> If the DTD declares unparsed entities named dia1, dia2, dia3 and so on through dia10, this element diashow embeds the show in the XML:
<diashow dias="dia1 dia2 dia3 dia4 dia5 dia6 dia7 dia8 dia9 dia10"/> NOTATION
An attribute of type NOTATION holds a DTD-declared notation name, probably the rarest attribute type. In theory it types elements or limits associated types. These declarations define four image notations, then require each bild element to have a typ attribute selecting one:
<!NOTATION gif SYSTEM "bild/gif">
<!NOTATION tiff SYSTEM "bild/tiff">
<!NOTATION jpeg SYSTEM "bild/jpeg">
<!NOTATION png SYSTEM "bild/png">
<!ATTLIST bild typ NOTATION (gif | tiff | jpeg | png) #REQUIRED>The typ attribute on each bild element accepts one of four values: gif, tiff, jpeg or png, and nothing else. Unlike enumeration, the actual MIME type is available. Enumeration could not allow bild/png or bild/gif because slash is illegal in XML names.
Default attribute values
Besides the type, each ATTLIST declaration specifies a default in one of four ways:
#IMPLIEDOptional attribute; instances may supply or omit it. No default is provided.
#REQUIREDRequired attribute; every instance must supply it. No default is provided.
#FIXEDConstant, unchangeable value, present whether explicit or not. If explicitly supplied, it must equal the fixed value.
- Literal
The actual default value is quoted.
This ATTLIST declaration lets person elements have attributes geboren and gestorben without requiring them:
<!ATTLIST person geboren CDATA #IMPLIED
gestorben CDATA #IMPLIED
>This ATTLIST declaration requires every kreis element to have attributes mittelpunkt_x, mittelpunkt_y and radius:
<!ATTLIST kreis mittelpunkt_x NMTOKEN #REQUIRED
mittelpunkt_y NMTOKEN #REQUIRED
radius NMTOKEN #REQUIRED
>This ATTLIST declaration says every element biographie has an attribute version whose value is 1.0 even if the start-tag does not explicitly supply version:
<!ATTLIST biographie version CDATA #FIXED "1.0"> This ATTLIST declaration says every element webseite has an attribute protokoll. If a webseite element lacks an explicit protokoll attribute, the parser supplies one with value http:
<!ATTLIST webseite protokoll NMTOKEN "http">
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de