Sams Teach Yourself XML in 21 Days
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- Day 2. Creating XML Documents
- Day 3. Creating Well-Formed XML Documents
- Day 4. Creating Valid XML Documents: DTDs
- Declaring Attributes in DTDs
- Day 6. Creating Valid XML Documents: XML Schemas
- Day 7. Creating Types in XML Schemas
- Part I. In Review
- Day 8. Formatting XML by Using Cascading Style Sheets
- Day 9. Formatting XML by Using XSLT
- Day 10. Working with XSL Formatting Objects
- Part II. In Review
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Day 12. Putting XHTML to Work
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Day 14. Handling XLinks, XPointers, and XForms
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Day 16. Using Java and .NET: DOM
- Day 17. Using Java and .NET: SAX
- Day 18. Working with SOAP and RDF
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Day 20. Working with XML and Databases
- Day 21. Handling XML in .NET
- Part V. In Review
- Appendix A. Quiz Answers
Specifying Default Values
The most common attribute type, as you've seen in the examples so far in this book, is CDATA, which is just character data. Before working through all the types of attributes you can use, you'll use CDATA attributes for a few more pages as you take a look at what kinds of default values you can specify for attributes. The first, and most common, type of default values are immediate values, and you'll begin with them.
Immediate Values
You can specify a default value for an attribute simply by listing that value, in quotes, in the attribute's declaration in the <!ATTLIST> element, making it an immediate value. If you give an attribute a default value and then don't use that attribute in an element, the attribute is automatically given the default value. The following example specifies a default value of "no" for the supervisor attribute, "plastics" for the division attribute, and "yes" for the fullTime attribute:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE document [
<!ELEMENT document (employee)*>
<!ELEMENT employee (name, hiredate, projects)>
<!ELEMENT name (lastname, firstname)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT hiredate (#PCDATA)>
<!ELEMENT projects (project)*>
<!ELEMENT project (product, id, price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST employee
supervisor CDATA "no"
division CDATA "plastics"
fullTime CDATA "yes"
>
.
.
.
Now each of these three attributes has a default value that will be assigned to it if you don't specifically assign another value.
The #REQUIRED Default Value
You can specify a default value of #REQUIRED to indicate that an attribute is required. For example, if you want to specify that all <employee> elements needed to have supervisor attributes, you can do so like this:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE document [
<!ELEMENT document (employee)*>
<!ELEMENT employee (name, hiredate, projects)>
<!ELEMENT name (lastname, firstname)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT hiredate (#PCDATA)>
<!ELEMENT projects (project)*>
<!ELEMENT project (product, id, price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST employee supervisor CDATA #REQUIRED>
]>
<document>
<employee supervisor="no">
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
.
.
.
</employee>
</document>
#REQUIRED is useful, for example, when you have an attribute whose data is essential, such as when an XML author needs to supply the URI of an image in an attribute named uri.
The #IMPLIED Default Value
Attributes declared with #IMPLIED are optional. For example, you can use the #IMPLIED default value if you want to allow the document author to include an attribute, but you don't want to require it. Here's an example that declares the supervisor attribute with #IMPLIED:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE document [
<!ELEMENT document (employee)*>
<!ELEMENT employee (name, hiredate, projects)>
<!ELEMENT name (lastname, firstname)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT hiredate (#PCDATA)>
<!ELEMENT projects (project)*>
<!ELEMENT project (product, id, price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST employee supervisor CDATA #IMPLIED>
]>
<document>
<employee supervisor="no">
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
.
.
.
</employee>
<employee>
<name>
<lastname>Grant</lastname>
<firstname>Cary</firstname>
</name>
.
.
.
</employee>
</DOCUMENT>
Using #IMPLIED this way means that an attribute can appear in elements or not, as the document author prefers.
The #FIXED Default Value
The final default value is #FIXED, which you use when you want to assign a fixed value to an attribute—a value that the attribute will always have. For instance, the following example ensures that an attribute named language will always have the value "en", to specify that a document is in English:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE document [
<!ELEMENT document (employee)*>
<!ELEMENT employee (name, hiredate, projects)>
<!ELEMENT name (lastname, firstname)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT hiredate (#PCDATA)>
<!ELEMENT projects (project)*>
<!ELEMENT project (product, id, price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST employee language CDATA #FIXED "en">
]>
<document>
<employee>
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
<id>111</id>
<price>$111.00</price>
</project>
<project>
<product>Laptop</product>
<id>222</id>
<price>$989.00</price>
</project>
</projects>
</employee>
</document>
When you make an attribute fixed, you don't even have to give it a value, as in this example. In this case, even though you haven't used the language attribute in the <employee> element, an XML processor will report a language attribute with the value "en" for the <employee> element.
Specifying Attribute Types | Next Section

Account Sign In
View your cart