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 DTDsDTDs 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
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 ValueThe 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
Implied AttributeThe 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 |