Here we’re going to show you how to define XML attributes and their properties. In our previous tutorial Creating an XML DTD, we showed you how to create an attribute for a given XML element like so, <element attribute=”attribute-value” />. The important thing to consider here is that the attribute value is not really an equivalent of the attribute being defined, but instead is actually content that is being placed within the attribute itself. You can think of this as being comparable to a variable that also holds some sort of data as in the case of programming languages. For instance, Take a look at the XHTML element used for displaying images in a webpage, <img src=”binary-data.jpg” />. The “img” portion defines the element being used and the use of an image, the “src” attribute defines we are pointing to an external source for some sort of data, and the “binary-data.jpg” value is the binary data we are pointing too, although we could just as well be pointing to textual data also. In XML all the same rules and principals apply. We can define naming conventions for XML elements and attributes however we like, just stay clear of special symbols and use letters or numbers only.
Now we need to learn how to declare attributes and their properties in our DTD (Document Type Definition). Attributes must be declared before being used in your XML documents, otherwise your XML markup is not considered valid.
First start by defining your element within your DTD:
If you wanted your element to describe the use of textual data, you could create an element that is descriptive of this:
<!ELEMENT car (#PCDATA)>
Notice we defined our element as consisting of parsed character data, since it will contain only text. Now we need to declare the attribute we plan to use with our element “car”:
<!ATTLIST car type CDATA #IMPLIED>
The “CDATA” declaration specifies that our attribute value can contain a combination of characters that the user chooses. For instance, <car type=”Honda” />. We could just as easily have used the name “Ford” instead. We then used the “#IMPLIED” property to specify that our attribute has no default value, and does not require one. Now our “car” elements attribute, “type”, can contain any mix of letters or numbers as an attribute value.
We could also set a default value for our attribute value by typing a default name like so:
We now stated that the attribute value has a default of “Honda”, but can contain any other value we choose as long as it’s textual. likewise, We can force the use of the default value only if we wanted too by replacing “#IMPLIED” with “#FIXED” so:
The “#FIXED” property implies we want to use only the default value specified and no other value.
We can also state that our attribute value must contain some sort of non predefined data with the use of the “#REQUIRED” property:
<!ATTLIST car type CDATA #REQUIRED>
We can also specify a set of predefined choices to choose from:
<!ATTLIST car type ( Honda | Toyota ) #REQUIRED>
Here we specified the choice for our attribute value as either “Honda” or “Toyota”, but no other value may be used since we incorporated the “#REQUIRED” property in our declaration. Likewise, the vertical bar states that we can only use one of these two values at a time. Also, we could use as many vertical bars as we want in our declarations, in order to create a variable amount of fixed choices to select a value from. If you define a default or list of default attributes, the xml parser will automatically use the first default value it encounters in your DTD declarations for that given elements attribute value when using the “#REQUIRED” property.
NOTE: Attribute properties are case sensitive and must be upper case! Typing “#Implied” would return errors from the XML parser for example. Also, you cannot use a default value with “#IMPLIED”.