Attributes

We've already discussed attributes in some detail; they're those name/value pairs that you can use in start tags and empty tags to provide additional information for an element. Here's an example; in this case, I'm adding an attribute named TYPE to the <CUSTOMER> tag to indicate what type of customer a person is:

<CUSTOMER TYPE = "excellent">
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
    .
    .
    .

You can use attributes like this one and assign them values in XML documents, but unless you also declare them, your document won't be valid. You can declare a list of attributes for an element with the <!ATTLIST> element in the DTD. Here's the general form of an <!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 that you're declaring, TYPE is the attribute's type, and DEFAULT_VALUE specifies its default value. As we'll see in this chapter, DEFAULT_VALUE can take several forms.

Here's an example in which I'll declare the TYPE attribute that we used previously. In this case, I'll use the simplest kind of declaration, making the attribute's type CDATA, which is simple character data, and using an #IMPLIED default value, which means that you can use this attribute in an element or skip it entirely. This is what the document looks like, including the DTD:

<?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, 2001</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>
            .
            .
            .
            <ITEM>
                <PRODUCT>Asparagus</PRODUCT>
                <NUMBER>12</NUMBER>
                <PRICE>$2.95</PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>Lettuce</PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

That introduces us to the idea of declaring attributes in DTDs. I'll get into the details on entities and attributes now, starting with entities—first general entities and then parameter entities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset