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, 2003</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 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 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, including the DTD, looks like:

 <?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>             .             .             .             <ITEM>                 <PRODUCT>Asparagus</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Lettuce</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.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 entitiesfirst general entities and then parameter entities.



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