Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

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.

Share ThisShare This

Informit Network