3.3 Attributes in the DTD

3.3 Attributes in the DTD

Attributes are listed after the element they identify in the Document Type Definition. Attribute definitions use the keyword !ATTLIST, followed by the name of the element and the name of the attribute. Attributes can be of string type, tokenized type, or enumerated type. Attributes can also list a default value if there is one. You can include attributes in element start markup or empty element markup. Attributes should contain something unique and be brief, pertaining only to the element it refines.

<!ATTLIST theNameOfTheElement theNameOfTheAttribute typeOfAttribute

String type attributes are CDATA (or character data) and only contain content, not markup. Most attributes will probably be string type. String type attributes may not be specific enough, so tokenized or enumerated attribute types can be defined. Tokenized type attributes include an ID for an element. These ID values will be unique for each element in the document, much like the record ID that Filemaker Pro assigns to each record. Enumerated attribute types can list a precise choice of values, as shown in Listing 3.7. If you validate a field in FileMaker Pro to contain only values from a list, it could have an enumerated attribute. Attributes can also have default values, just as FileMaker Pro fields can have auto-enter data.

Listing 3.7: Elements with single attribute and default values
Start example
<!ELEMENT phone (#PCDATA)>
      <!ATTLIST phone location (work | home | pager | mobile) "work">
<!-- the element "phone" has an attribute of "location" -->
<!-- it is not a required attribute of the element -->
<!-- if it is used, the allowed values and a default are listed -->
<!ELEMENT constant (#PCDATA)>
      <!ATTLIST constant value CDATA #FIXED "1">
<!-- the element "constant" has one attribute, "value" -->
<!-- the fixed content of the attribute is "1" -->
End example

Default types of attributes can be required and always have a value. This default type of attribute uses the keyword #REQUIRED. If the attribute is optional, use the keyword #IMPLIED as the default value, as seen in Listings 3.8 and 3.9. A default value for an attribute is designated with the keyword #FIXED, and the value should be added automatically by the XML processors. Default values can be listed as a pipe-separated () choice list and can include the literal value in quotes. Because these are attribute lists, you can define all the attributes for a single element together, as seen in Listing 3.9.

Listing 3.8: An element with multiple attributes and separate definitions
Start example
      <!ATTLIST line width "1">
      <!ATTLIST line height "1">
      <!ATTLIST line color #IMPLIED>
      <!ATTLIST line fill #IMPLIED>
End example
Listing 3.9: An element with multiple attributes and one definition
Start example
      <!ATTLIST line
            width "1"
            height "1"
            color #IMPLIED
            fill #IMPLIED>
End example
Listing 3.10: Attribute list for element IDs
Start example
<!ATTLIST record
      SerialNumber ID #REQUIRED>
<!-- there should be a unique piece of data for each element named
  "record" -->
<!-- this is for results from an -fmp_xml or -dso_xml request -->
<!-- each row (record) is unique in a single database -->
End example

The creation of definitions for elements and attributes for a particular XML document is demonstrated in the next section. You can read more about the construction of element definitions in the document "Extensible Markup Language (XML) 1.0 (Second Edition)", http://www.w3.org/TR/2000/REC-xml-20001006#elemdecls. Attribute definitions are in the same document found at http://www.w3.org/TR/2000/REC-xml-20001006#attdecls.