DTD - Attribute

In DTDs werden Attribute durch die ATTLIST-Deklaration deklariert.

Deklaration von Attributen

Syntax für Attributdeklarationen:

<!ATTLIST Elementname Attributname Attributtyp Standardwert>

DTD-Beispiel:

<!ATTLIST payment type CDATA "check">

XML-Beispiel:

<payment type="check" />

Hier sindAttributtypOptionen:

Typ Beschreibung
CDATA Der Wert ist Zeichenkodatendaten (character data)
(en1|en2|..) Dieser Wert ist einer der Werte der Aufzählungsliste
ID Der Wert ist eine eindeutige ID
IDREF Der Wert ist die ID eines anderen Elements
IDREFS Liste von anderen ID-Werten
NMTOKEN Der Wert ist ein gültiger XML-Name
NMTOKENS The value is a list of valid XML names
ENTITY Value is an entity
ENTITIES Value is a list of entities
NOTATION This value is a symbolic name
xml: Value is a predefined XML value

Default value parameters can use the following values:

Value Explanation
Value The default value of the attribute
#REQUIRED Attribute value is required
#IMPLIED Attribute is not required
#FIXED value Attribute value is fixed

Define a default attribute value

DTD:

<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">

Valid XML:

<square width="100" />

In the above example, "square" is defined as an empty element with the CDATA type "width" attribute. If the width is not set, the default value is 0.

#IMPLIED

Syntax

<!ATTLIST ElementName AttributeName AttributeType #IMPLIED>

Example

DTD:

<!ATTLIST contact fax CDATA #IMPLIED>

Valid XML:

<contact fax="555-667788" />

Valid XML:

<contact />

If you do not want to force the author to include the attribute and you do not have a default value option, please use the keyword #IMPLIED.

#REQUIRED

Syntax

<!ATTLIST ElementName AttributeName AttributeType #REQUIRED>

Example

DTD:

<!ATTLIST person number CDATA #REQUIRED>

Valid XML:

<person number="5677" />

Invalid XML:

<person />

If you do not have a default value option but still want to force the author to submit the attribute, please use the keyword #REQUIRED.

#FIXED

Syntax

<!ATTLIST ElementName AttributeName AttributeType #FIXED "value">

Example

DTD:

<!ATTLIST sender company CDATA #FIXED "Microsoft">

Valid XML:

<sender company="Microsoft" />

Invalid XML:

<sender company="W3School" />

If you want an attribute to have a fixed value and do not allow the author to change this value, please use the #FIXED keyword. If the author uses a different value, the XML parser will return an error.

List of attribute values

Syntax:

<!ATTLIST ElementName AttributeName (en1|en2|..) DefaultValue>

DTD Example:

<!ATTLIST payment type (check|cash) "cash">

XML Example:

<payment type="check" />

Or

<payment type="cash" />

If the attribute value should be one of a series of fixed valid values, please use enumerated attribute values.