Declaring Attributes in DTDs

An XML element in a well-formed document may not have two attributes of the same name. Therefore, cardinality operators are not required when attributes are declared.

The declaration of attributes takes the following general form:

<!ATTLIST 
 attributeName attributeType defaultDeclaration 
> 

The attributeType is any of the values in the following list:

  • CDATA— Any legal XML string.

  • ENTITY— Value that must match the name of an external unparsed entity.

  • ENTITIES— An ENTITY, except that more than one whitespace-separated name may occur.

  • ID— Value that must begin with a letter and then must consist of letters, numeric characters, hyphens, underscores, and period characters. At most, one attribute on any element can be of type ID. An ID attribute value must be unique in the XML document.

  • IDREF— The value of the attribute must match the value of an ID attribute elsewhere in the same XML document.

  • IDREFS— An IDREF, except that it may match more than one ID attribute value elsewhere in an XML document.

  • NMTOKEN— The attribute value may contain only letters, numeric characters, and colons. No whitespace is allowed.

  • NMTOKENS— An NMTOKEN, except that multiple values that do not contain whitespace are separated by whitespace characters.

The defaultDeclaration indicates whether a value is required, is optional, is fixed, or has a default value:

  • #FIXED "someValueInQuotes"— The value of the attribute is fixed to the value given inside the quotation marks.

  • #IMPLIED— A value for the attribute is optional.

  • #REQUIRED— A value for the attribute is required.

  • "someValueInQuotes"— A value for the attribute is optional. If no value is specified for the attribute in the XML document, the value "someValueInQuotes " is applied as a default.

For example, suppose that you have a very short XML document, as follows:

<order date="2002/11/30" orderNumber="NOV123" 
customerID="DB998"> 
<items> 
<!-- And so on --> 
</items> 
<order> 

This example shows three attributes of the order element; for business purposes, each of these is essential. These are declared as a list of attributes associated with the order element. The ATTLIST keyword implies an attribute list, but you can define as few as one attribute.

<!ELEMENT order (items)> 
<!-- An element declaration for the items element would go 
   here --> 
<!ATTLIST order 
customerID CDATA #REQUIRED 
date CDATA #REQUIRED 
orderNumber CDATA #REQUIRED 
> 

Types of Attributes

XML 1.0 DTDs provide very limited typing of attribute values, as discussed in the preceding section. For data-centric XML documents, this might be insufficient. This is one of the reasons behind the development of W3C XML Schema, described in Chapter 19.

Specifying Default Attribute Values

You can specify default values for attributes when no value is given. For example, consider a business document structured as follows:

<Report> 
<Paragraph status="public">Some text.</Paragraph> 
<Paragraph status="confidential">Some confidential 
   text</Paragraph> 
<Paragraph>Some text.</Paragraph> 
</Report> 

To preserve confidentiality, you can make it essential that a human actually decide to make any information public by having the following attribute declaration:

<!ATTLIST Paragraph status "confidential" #REQUIRED> 

If an author specifies status="public", the default value is not applied. However, if the author overlooks the need to assign a status attribute to a Paragraph element, the Paragraph element’s content is confidential until a human author overrides the default.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset