XML and DTDs

Team-Fly    

 
XML, Web Services, and the Data Revolution
By Frank  P.  Coyle
Table of Contents
Appendix  A.   XML Language Basics


A DTD defines the structure of an XML document with a list of legal elements. It can be declared either within your XML document or as an external reference. If an XML document conforms to the rules set out by a DTD, the XML is said to be valid with respect to that DTD.

For example, consider the following XML document that contains an internal DTD that begins with " <!DOCTYPE " and ends with " ]> ".

 <?xml version="1.0"?> <!DOCTYPE note [   <!ELEMENT note (to,from,heading,body)>   <!ELEMENT to      (#PCDATA)>   <!ELEMENT from    (#PCDATA)>   <!ELEMENT heading (#PCDATA)>   <!ELEMENT body    (#PCDATA)> ]> <note>   <to>Bob   </to>   <from>Marilyn</from>   <heading>Reminder</heading>   <body>Don't forget to bring the cheese</body> </note> 

The !DOCTYPE syntax in the second line defines this as a document of type note . The third line defines the note element as having four elements: to , from , heading , and body . The fourth line defines the to element as type #PCDATA, which means it may contain text. All the other elements are similarly defined.

A DTD may also be external to an XML source document. If so, the following syntax is used.

 <!DOCTYPE root-element SYSTEM "filename"> 

Defining Attributes in DTDs

DTDs may also be used to specify attributes using the following form.

 <!ATTLIST element-name attribute-name   attribute-type default-value> 

For example, the DTD

 <!ATTLIST payment type CDATA "check"> 

establishes the validity of the XML

 <payment type="check"/> 

The attribute-type can have the values given in Table A-1.

As an example, consider the following attribute declaration as part of a DTD:

 <!ELEMENT square EMPTY>    <!ATTLIST square width CDATA "0"> 
Table A-1. Possible Values of the Attribute-Type in an XML DTD
Value Explanation
CDATA The value is character data
(en1en2..) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is the name of a notation
xml: The value is a predefined XML value

This maps to the following XML:

 <square width="100"></square> 

The element square is defined as an empty element with a width attribute of type CDATA . If no width attribute is given, the attribute has a default value of 0.

Default Attribute Value

The syntax for default attribute values is as follows :

 <!ATTLIST element-name attribute-name attribute-type "default-value"> 

The following DTD example uses default values:

 <!ATTLIST payment type CDATA "check"> 

Here is an example of XML that is conformant with this DTD:

 <payment type="check"/> 

Specifying a default value for an attribute means that the attribute will be assigned a value even if the author of the XML document does not supply one. Table A-2 shows possible default attribute values.

Table A-2. Default Attribute Values in an XML DTD
Value Explanation
value The attribute's default value
#DEFAULT The attribute's default value
#REQUIRED The attribute value must be included in the element
# IMPLIED The attribute does not have to be included
#FIXED The attribute value is fixed

Implied Attribute

The syntax for implied attribute values is as follows:

 <!ATTLIST element-name attribute-name attribute-type #IMPLIED> 

This is a DTD example using implied values:

 <!ATTLIST contact fax CDATA #IMPLIED> 

And the following XML conforms to this DTD:

 <contact fax="214-555-6677"/> 

Use an implied attribute in a DTD when you don't want to force the author to include an attribute and you don't have a default value.


Team-Fly    
Top


XML, Web Services, and the Data Revolution
XML, Web Services, and the Data Revolution
ISBN: 0201776413
EAN: 2147483647
Year: 2002
Pages: 106
Authors: Frank Coyle

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net