Declaring Attributes in DTDs

Declaring attributes and their types is very useful in XML. If you want your document to be valid, you must declare any attributes that you use before using them. You can give attributes default values and even require XML authors who use your DTD to assign values to attributes.

As we saw at the beginning of this chapter, you declare a list of attributes for an element with the <!ATTLIST> element:

 <!ATTLIST ELEMENT_NAME  ATTRIBUTE_NAME TYPE DEFAULT_VALUE   ATTRIBUTE_NAME TYPE DEFAULT_VALUE   ATTRIBUTE_NAME TYPE DEFAULT_VALUE  .     .     .  ATTRIBUTE_NAME TYPE DEFAULT_VALUE  > 

In this case, ELEMENT_NAME is the name of the element that you're declaring attributes for, ATTRIBUTE_NAME is the name of an attribute you're declaring, TYPE is the attribute's type, and DEFAULT_VALUE represents its default value.

Here are the possible TYPE values you can use:

  • CDATA Simple character data (that is, text that does not include any markup).

  • ENTITIES Multiple entity names (which must be declared in the DTD), separated by whitespace.

  • ENTITY Names an entity (which must be declared in the DTD).

  • Enumerated Represents a list of values. Any one item from the list is an appropriate attribute value.

  • ID A proper XML name that must be unique (that is, not shared by any other attribute of the ID type).

  • IDREF Holds the value of an ID attribute of some element, usually another element that the current element is related to.

  • IDREFS Multiple IDs of elements separated by whitespace.

  • NMTOKEN A name token, made up of one or more letters , digits, hyphens, underscores, colons, and periods.

  • NMTOKENS Multiple NMTOKENs in a list, separated by whitespace.

  • NOTATION A notation name (which must be declared in the DTD).

I'll take a look at all these possibilities in this chapter.

Here are the possible DEFAULT_VALUE settings you can use:

  • VALUE A simple text value, enclosed in quotes.

  • #IMPLIED Indicates that there is no default value for this attribute and that this attribute need not be used.

  • #REQUIRED Indicates that there is no default value but that a value must be assigned to this attribute. If a required attribute is missing, the document is not valid.

  • #FIXED VALUE In this case, VALUE is the attribute's value, and the attribute must always have this value.

Here's a simple example in which I declare a TYPE attribute of the CDATA type for the <CUSTOMER> element and indicate that this attribute can be used or not, as the author prefers:

Listing ch04_10.xml
 <?xml version = "1.0" standalone="yes"?> <!DOCTYPE DOCUMENT [ <!ELEMENT DOCUMENT (CUSTOMER)*> <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (LAST_NAME,FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)> <!ELEMENT PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)>  <!ATTLIST CUSTOMER   TYPE CDATA #IMPLIED>   ]>  <DOCUMENT>  <CUSTOMER TYPE = "excellent">  <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Oranges</PRODUCT>                 <NUMBER>24</NUMBER>                 <PRICE>.98</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>  <CUSTOMER TYPE = "lousy">  .         .         .     </CUSTOMER> </DOCUMENT> 

This example shows how to declare a single attribute. As its name implies, you can use <!ATTLIST> to declare an entire list of attributes for an element. Here's an example in which I declare the attributes OWES , LAYAWAY , and DEFAULTS for the <CUSTOMER> element all at once:

Listing ch04_11.xml
 <?xml version = "1.0" standalone="yes"?> <!DOCTYPE DOCUMENT [ <!ELEMENT DOCUMENT (CUSTOMER)*> <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (LAST_NAME,FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)> <!ELEMENT PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)>  <!ATTLIST CUSTOMER   OWES CDATA "0"   LAYAWAY CDATA "0"   DEFAULTS CDATA "0">   ]>  <DOCUMENT>  <CUSTOMER OWES=".13" LAYAWAY=" 
 <?xml version = "1.0" standalone="yes"?> <!DOCTYPE DOCUMENT [ <!ELEMENT DOCUMENT (CUSTOMER)*> <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (LAST_NAME,FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)> <!ELEMENT PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)>  <!ATTLIST CUSTOMER   OWES CDATA "0"   LAYAWAY CDATA "0"   DEFAULTS CDATA "0">   ]>  <DOCUMENT>  <CUSTOMER OWES="$12.13" LAYAWAY="$0" DEFAULTS="0">  <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>$1.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Oranges</PRODUCT>                 <NUMBER>24</NUMBER>                 <PRICE>$4.98</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>  <CUSTOMER OWES="$132.69" LAYAWAY="$44.99" DEFAULTS="0">  .         .         .     </CUSTOMER> </DOCUMENT> 
" DEFAULTS="0">
<NAME> <LAST_NAME>Smith</LAST_NAME> <FIRST_NAME>Sam</FIRST_NAME> </NAME> <DATE>October 15, 2003</DATE> <ORDERS> <ITEM> <PRODUCT>Tomatoes</PRODUCT> <NUMBER>8</NUMBER> <PRICE>.25</PRICE> </ITEM> <ITEM> <PRODUCT>Oranges</PRODUCT> <NUMBER>24</NUMBER> <PRICE>.98</PRICE> </ITEM> </ORDERS> </CUSTOMER> <CUSTOMER OWES="2.69" LAYAWAY=".99" DEFAULTS="0"> . . . </CUSTOMER> </DOCUMENT>

Now that I've declared these attributes, the document is valid.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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