Setting Default Values for Attributes

I'm going to start the examination of declaring attributes in DTDs by seeing what kind of default values you can specify for attributes.

Immediate Values

You can supply a default value for an attribute simply by giving that value in quotes in the attribute's declaration in the <!ATTLIST> element, as we've seen:

 <?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">   ]>  .     .     . 

However, you can also use other keywords here, such as #REQUIRED .

#REQUIRED

When you use the #REQUIRED keyword as an attribute's default value, it means that you're actually not providing a default value, but you're requiring anyone using this DTD to do so. Here's an example in which I'm requiring anyone who uses this DTD to supply the <CUSTOMER> element with an OWES attribute:

Listing ch04_12.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 #REQUIRED>   ]>  <DOCUMENT>  <CUSTOMER OWES=" 
 <?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 #REQUIRED>   ]>  <DOCUMENT>  <CUSTOMER OWES="$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="$599.99">  .         .         .     </CUSTOMER> </DOCUMENT> 
">
<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="9.99"> . . . </CUSTOMER> </DOCUMENT>

Requiring a value for an attribute is useful for cases in which a document should be customized, as when you want to list the document author's name or email address. It's also useful, of course, when the element needs more information, as when you use a URI attribute for an element that displays an image or loads an applet.

# IMPLIED

You use the #IMPLIED keyword when you don't have a default value for an attribute in mind and you want to indicate that the document author doesn't even have to use this attribute at all. XML processors will know about this attribute and not be disturbed if the attribute is not used (note that some XML processors will explicitly inform the underlying software that no value is available for this attribute if no value is given). The #IMPLIED keyword is the one to use when you want to allow the document author to include this attribute but not require it.

Here's an example in which I'm making the OWES attribute of the <CUSTOMER> element implied, which means that not every element needs to use it:

Listing ch04_13.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 #IMPLIED>   ]>  <DOCUMENT>  <CUSTOMER OWES=".99">  <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>  .         .         .     </CUSTOMER> </DOCUMENT> 

It's very common to declare attributes as #IMPLIED because that means they can appear in elements or not, as the document author prefers.

#FIXED

You can even set the value of an attribute so that it must always have that value. To do that, you use the #FIXED keyword, which sets a fixed value for the attribute. Then you specify the value you want the attribute to have.

Here's an example in which I'm setting the LANGUAGE attribute of the <CUSTOMER> elements to English, EN , and specifying that that is the only valid value for the attribute. This makes the assumption that the underlying application can handle only English and so needs data provided in that language:

Listing ch04_14.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   LANGUAGE CDATA #FIXED "EN">   ]>  <DOCUMENT>  <CUSTOMER>  <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>  .         .         .     </CUSTOMER> </DOCUMENT> 

Note that I didn't even use the LANGUAGE attribute in the <CUSTOMER> elements here. The XML processor passes that attribute and its value to the underlying application anyway because I've declared them #FIXED . If you do explicitly use this attribute, you must set its value to the value you've set as the default in the DTD, or the XML processor will generate an error.

That covers the possible default value types you can specify when declaring attributes; I'll take a look at the possible attribute types next .



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