ATTLIST tag

ATTLIST tag

An ATTLIST tag is used to specify attributes for use with an element.

An ATTLIST tag contains:
<!ATTLIST the-element an-attribute attribute-value-declaration>

  • there is an exclaimation point ! before the word ATTLIST
  • ATTLIST tag does NOT have a closing tag
  • ATTLIST tag does NOT have a slash/ at the end
  • ATTLIST is in upper case letters
  • the element being used is specified immediatly after the word ATTLIST
  • next is the name of the attribute.
  • last the attribute value is declared
  • the attribute value declaration is NOT in parentheses.

single attribute

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #REQUIRED >
This means the   name   element has a   citizenship   attribute.
The   citizenship   attribute has text in the CDATA format.
The   citizenship   attribute is required, and has no default value.
Discussions of CDATA and #REQUIRED are later in this page.

list of attributes

As the name implies, an ATTLIST tag can be used to specify a list of several attributes for an element.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #REQUIRED
  residence CDATA #REQUIRED
>

The   name   element is not repeated, but it is followed by two attribute specifications, one for   citizenship   and one for   residence

two attributes

If you prefer, you can use several ATTLIST tags to specify the attributes for an element.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #REQUIRED >
<!ATTLIST name residence CDATA #REQUIRED >
This produces the same reault as the list of two attributes.

CDATA

CDATA is different in XML and DTD.

In XML you code: <![CDATA[
Any kind of junk !<&* here
]]>

The text in the CDATA section in XML is not parsed. This means if you type <, you get <, and if you type &, you get &.

In DTD you code:
<!ATTLIST name citizenship CDATA #REQUIRED >
This means in the XML document, the name element has a citizenship attribute. In the XML document, the value for the citizenship attribute:

  • may NOT contain <
  • May contain & only as the beginning of an entity reference. The parser will substitute for the entity value specified after the &

For example, in the value for the citizenship attribute, when you code &lt; you get <

order

You can specify element contents of an ELEMENT tag to be ordered, such as   (a, b, c). Or you can specify element contents of an ELEMENT tag to not be ordered, such as   (a | b | c)*.

Attributes never have a specified order. The attributes in XML can always be written in any order. The order cannot be controlled by the DTD.

DTD organization

As you begin to write more tags in a DTD, you need to keep them in some order, so you can find them. You might consider putting the ATTLIST tags just after their ELEMENT tag.

Attribute value declaration

Attribute Value Declarations determine if the attribute is required, and control the default for the attribute value. There are four kinds of attribute value declarations:

  • default values
  • #REQUIRED
  • #IMPLIED
  • #FIXED

default attribute value

A default value can be specified for an attribute.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA "France" >
The default value is   "France".
The default value must be in quotes.
No keyword is needed, just code the default value.
The attribute is optional; the user is not required to code it.
If the user does not code this attribute, it will have a value of   "France".

required attribute

The user can be required to provide an attribute.
No default value is provided.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #REQUIRED >
The keyword is   #REQUIRED.
The attribute is required; the user must code it.
There is no default value; the user must provide a value.

optional attribute (implied)

An attribute can be optional.
This is coded with the keyword   #IMPLIED .
There is no default value.
If the user does not code the attribute, it has no value.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #IMPLIED >
The keyword is   #IMPLIED.
There is no default value.
The attribute is optional; the user is not required to code it.
If the user does not code this attribute, it will have no value.

fixed attribute value

The user does not need to code the attribute.
The value is fixed, and cannot be changed.

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #FIXED "Mexico">
The keyword is   #FIXED.
The fixed value is coded in quotes.
The attribute is optional.
If the user does provide the attribute, it must be the specified the fixed value; no other value is acceptable.

Attribute data types

The type of text in the attribute value can be specified. Different types are processed in different ways. The attribute value types are:

  • CDATA
  • ID
  • IDREF
  • IDREFS
  • ENTITY
  • ENTITIES
  • NMTOKEN
  • NMTOKENS
  • Enumerated list

CDATA

We have already been using CDATA.
CDATA is text, except < is not allowed, and & is the start of an enitiy, such as &lt;

In the source of the example to the left, you can see:
<!ATTLIST name citizenship CDATA #REQUIRED >
The value is "United Kingdom", which is valid CDATA.

ID

  • The ID value must be unque within the entire document.
  • The ID value must meet the XML name requirements.
  • The attribute value declaration must be #IMPLIED or #REQUIRED
  • Within an element, there may be only one ID.

In the source of the example to the left, you can see:
<!ATTLIST name id ID #IMPLIED >
The value is "run", which must be unique within the document.

IDREF

The IDREF value must be the same as the value of the ID given in a different element.

In the source of the example to the left, you can see:
<!ATTLIST person see-also IDREF #IMPLIED >
The value of see-also is "run", which is the ID of a different element.

IDREFS

The IDREFS value is a space seperated list of IDREF values, referring to the ID values given in different elements.

In the source of the example to the left, you can see:
<!ATTLIST person see-also IDREFS #IMPLIED >
The value of see-also is "run tales", which are the ID values of two different elements.