The element Element
As shown in the code in "A DTD for Schemas," the simplified DTD declaration for an element element is as follows:
<!ELEMENT element ((annotation)?, (complexType | simpleType)?,
(unique | key | keyref)*)>
<!ATTLIST element type CDATA #IMPLIED
name CDATA #IMPLIED
ref CDATA #IMPLIED
minOccurs (1 | 0 ) #IMPLIED
maxOccurs CDATA #IMPLIED
id ID #IMPLIED
nullable (true | false ) 'false'
default CDATA #IMPLIED
fixed CDATA #IMPLIED >
|
The name attribute is the name of the element. The name attribute must follow all the rules defined for DTD element names. You can define your element using a complexType element, a simpleType element, or a type attribute. The type attribute and either the simpleType element or the complexType element are mutually exclusive. If you are declaring a data type, then one and only one of these must be used for the datatype declaration to be valid.
The type attribute
The type attribute associates either a simple or complex data type with an element. As we've seen, simple data types are either the predefined simple data types or simple data types you define based on these predefined simple data types. Complex data types can be used to associate attributes, elements, or a combination of both to an element. For example, you can declare the simple data type String24 and associate it with the customerName element, as shown here:
<simpleType name="String24" base="string">
<maxLength= "24"/ >
<minLength = "0"/>
</simpleType>
<element name = "customerName" type = "String24"/>
|
In this case, you have created a data type named String24 that has a length between 0 and 24 characters. This data type is then used in the element declaration, which means that the customerName element will be a string that is between 0 and 24 characters.
The customerName declaration uses document scope, meaning that all elements in the document can see the String24 data type. The type attribute can be used to assign either a complex or a simple data type with document scope to an element.
The minOccurs and maxOccurs attributes
Notice that the minOccurs and maxOccurs attributes are also used in the DTD declaration for an element element to specify the number of occurrences of an element. When working with DTDs, we used the markers *, ?, and + to indicate the number of times a particular child element could be used as content for an element. For attributes, we used #IMPLIED for optional attributes, #REQUIRED for required attributes, #FIXED for attributes that had a fixed default value, and a default value when the attribute was optional. In schemas, both elements and attributes use the minOccurs and maxOccurs attributes. The minOccurs and the maxOccurs attributes are also used with the group element; the choice, sequence, and all elements that are contained within the group element; and the any element.
When used with elements, the minOccurs and maxOccurs attributes specify the number of occurrences of the element. For example, if an element has a minOccurs value of 0, the element is optional. You can also declare an element to occur one or more times by setting a maxOccurs attribute to 1 or * respectively. The default value for minOccurs is 1, and maxOccurs has no default value.
When used with attributes, minOccurs and maxOccurs indicate whether the attribute is required. The maxOccurs attribute defaults to 1 unless it is specified or minOccurs is greater than 1. If minOccurs is set to 0 for an attribute and the default maxOccurs is equal to 1, you can have between 0 and 1 occurrences of this attribute. Thus, an attribute with minOccurs set to 0 is optional. If minOccurs is set to 1, the attribute is required. The default for minOccurs is 0, but it's better to specify a value for it in your schema. The minOccurs and maxOccurs attributes can be set only to 0 or 1. For example, the following declaration makes the target attribute required:
<attribute name = "target" minOccurs = "1" maxOccurs = "1"
type = "string"/>
|
Notice that the attributes we have discussed in this section can also be used to define other elements such as the attribute element.