DTD ATTLIST

DTD > Structure > DTD ATTLIST

The ATTLIST declaration defines an attribute on a previous defined ELEMENT definition within a DTD.

The following EBNF describes the form the declaration takes.

[52]   AttlistDecl         ::=       '<!ATTLIST' S Name AttDef* S? '>'
[53] AttDef ::= S Name S AttType S DefaultDecl
[54] AttType ::= StringType | TokenizedType | EnumeratedType
[55] StringType ::= 'CDATA'
[56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
[57] EnumeratedType ::= NotationType | Enumeration
[58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' [VC: Notation Attributes]
[59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' [VC: Enumeration]
[60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)

Declaration Types

#REQUIRED

<!ATTLIST ElementName AttributeName AttributeType #REQUIRED>

Defines a required attribute AttributeName on the element ElementName. The attribute must exist, and value of the attribute must comply with the type rules for AttributeType.

#IMPLED

<!ATTLIST ElementName AttributeName AttributeType #IMPLED>

Defines an optional attribute AttributeName on the element ElementName. The attribute does not have to exist, but if it does the value of the attribute must comply with the type rules for AttributeType.

#FIXED

<!ATTLIST ElementName AttributeName AttributeType #FIXED AttributeValue>

Defines an attribute AttributeName on the element ElementName. The value of the attribute can only be the value specified in AttributeValue, and must comply with the type rules for AttributeType. If the attribute not present in the containing element, then as the XML Parser reads the document it will automatically add it,  giving it the value specified in AttributeValue.

AttributeType

The type of an attribute can be either 'CDATA', 'ID', 'IDREF', 'IDREFS', 'ENTITY', 'ENTITIES', 'NMTOKEN', 'NMTOKENS'. Each of these types has its own rules describing the ranges of values that they are allowed to contain.

The AttributeType can also be an enumeration, that is to say it can contain one of a number of pre defined values. There are 2 forms of enumerations.

  • A list of values that must comply with the type rules for 'NMTOKEN'.
  • A NOTATION attribute, in with case the values that must match the name of NOTATION's declared elsewhere within the DTD, this NOTATION data is an instruction to the processing application, and not strictly speaking considered part of the XML Data. An element can only have 1 NOTATION attribute.

AttributeValue

A quoted string containing the fixed value that the attribute must take. The standard rules for escaping attribute values apply (i.e. '&', '<' and '"' must be escaped if a double quote is used around the value, and '&', '<' and ''' must be escaped if a single quote is used around the value.

Examples

DTD Sample XML
<!ATTLIST test myAttr CDATA #IMPLIED>
Valid Copy Code
<test myAttr="some value"/>
Valid Copy Code
<test/>
<!ATTLIST test myAttr CDATA #REQUIRED>
Valid Copy Code
<test myAttr="some value"/>
Invalid - missing attribute Copy Code
<test/>
<!ATTLIST test version CDATA #FIXED '1.2'>
Valid Copy Code
<test version="1.2"/>
Valid - XML Parser assumes the version attributes value when its read Copy Code
<test/>
Invalid - Not the specified Fixed value Copy Code
<test version="5.7"/>
<!ATTLIST test userID ID #REQUIRED>
Valid Copy Code
<test userID="123456"/>
Invalid - the rules for the ID type do not allow spaces Copy Code
<test userID="Joe Bloggs"/>
<!ATTLIST test fruit (apple | pear | bannan) #REQUIRED>
Valid Copy Code
<test fruit="pear"/>
Invalid - the value is not in the enumeration list Copy Code
<test userID="pineapple"/>
<!NOTATION js PUBLIC "JavaScript 1.0">
<!NOTATION cs PUBLIC "CSharp 2.0">
<!NOTATION js PUBLIC "Perl 4.0">

<!ATTLIST test 
    scriptEngine NOTATION (js|cs|perl) #REQUIRED>
Valid Copy Code
<test scriptEngine="cs"/>
This tells the reading application that the NOTATION data PUBLIC "CSharp 2.0" should be used when interpreting this element.

See Also

Try Liquid XML Free and see how we can help you today Free Trial