Browse DevX
Sign up for e-mail newsletters from DevX


XML DTDs : Page 6

Valid XML documents follow a set of rules defined in a associated DTD. This Document Type Definition defines elements, attributes, and relationships between elements.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Attribute Lists
Elements can have attributes, which describe the element in more detail. When you create an element in your DTD, you can also an create an attribute list for the element.

Attribute lists define the name, data type, and default value (if any) of each attribute associated with an element.

In this very simple example, we're adding some attributes to the title element from our book list. We want to be able to specify the edition date and whether the book is paperback or hardcover. Click on any of the attribute list code to see what it does.

<!--This defines a listing of books-->
<!DOCTYPE books [
<!ELEMENT booklist (title, author)>
<!ELEMENT title (#PCDATA)>

<!ATTLIST title
   edition (CDATA) #REQUIRED
   type (paper|cloth|hard) "paper">
<!ELEMENT author (#PCDATA)>

Here's how you'd use these attributes in an XML file. Notice the use of the edition attributes in each title tag. Notice how one title tag also uses the type attribute to indicate that this book is a hardcover title.

Attribute Types
Attributes can have one of seven different types of data, but the two most common are:

CDATA. Character data. This allows the attribute value to be textual data. You use it like this:

<!ATTLIST edition date (CDATA)>

Pre-defined values. You can list a string of specific values that the attribute can have. The value set is enclosed in parenthesis and each value is separated with a vertical bar, like this:

<!ATTLIST edition type (paper|hard|cloth)>

Default Values
You can specify a default value for the attribute, or make the attribute required or optional. The default value has a strong effect on how the attribute is used and what values it might have if you don't use it in the XML tag.

#REQUIRED: the attribute must have a value every time the element is listed. You specify that an attribute is required like this:

<!ATTLIST edition date (CDATA) #REQUIRED>

#IMPLIED: the procesor ignores this attribute unless it used as part of the element. It does not assume any default value.

#FIXEDvalue: an attribute is not required for the element, but if it occurs, it must have the specified value. For example, if the new attribute is used, it must have the value of "yes":

<!ATTLIST edition new #FIXED "yes">

VALUE defaultvalue provides a default value for that attribute. If the attribute in not included in the element, the processing program assumes that this is the attribute's value. For example, this gives the type attribute a default value of "hard":

<!ATTLIST edition type (paper|cloth|hard) "hard">

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date