MCCC CIS 177 - Markup Languages

Create a Valid XML Documen

Declare Element Attributes

To enforce attribution properties of elements, you must add an attribute-list declaration to the DTD. The attribute-list declaration lists the names of all attributes; specifies the data type of all attributes; indicates whether the attribute is required or optional; and provides a default value if necessary. The syntax is as follows:

<!ATTLIST element attribute1 type1 default1
   attribute2 type2 default2
   attributet type 3 default3...>

OR

<!ATTLIST ELEMENT attribute1 type1 default1>
<!ATTLIST ELEMENT attribute2 type2 default2>
<!ATTLIST ELEMENT attribute3 type3 default3>

Attribute Types

Type
Syntax
String attribute CDATA
Enumerated (list of possible values) attribute (value1 | value2 | value3)
Tokenized (valid XML name)

attribute token

ID - unique identifier for the attribute
IDREF - references id of another element
IDREFS - a list of id references separated by spaces
NMTOKEN - name token whose value is restricted to a valid XML name
ENTITY - a reference to an external file (i.e. non-XML data)
ENTITIES - a list of entity references separated by spaces

Default Values

There are four possible defaults:

Attribute Default Syntax
#REQUIRED <!ATTLIST Customer CustID ID #REQUIRED>
#IMPLIED <!ATTLIST Customer CustType (home | business) #IMPLIED>
"default"

Optional value. Syntax to have parser assume a value:

<!ATTLIST Item ItemQty CDATA "1">

#FIXED "default"

Optional value. If an attribute value is specified, it must match the default value.

 

 

 

Create and Apply Attribute Declarations

Edit Orders.xml and add the following code:

<!-- document type declaration follows -->

<!DOCTYPE Customers
[
   <!ELEMENT Customers (Customer+)>
   <!ELEMENT Customer (Name, Address, Phone, E-mail?, Orders)>
   
<!ATTLIST Customer CustID ID #REQUIRED>
   <!ATTLIST Customer CustType (home | business) #IMPLIED>
   <!ELEMENT Name (#PCDATA)>
   
<!ATTLIST Name Title (Mr. | Mrs. | Ms.) #IMPLIED>
   <!ELEMENT Address (#PCDATA)>
   <!ELEMENT Phone (#PCDATA)>
   <!ELEMENT E-mail (#PCDATA)>
   <!ELEMENT Orders (Order+)>
   <!ELEMENT Order (Order_date, Items)>
   
<!ATTLIST Order OrderID ID #REQUIRED>
   <!ATTLIST Order OrderBy IDREF #REQUIRED>
   <!ELEMENT Items (Item+)>
   <!ELEMENT Order_date (#PCDATA)>
   <!ELEMENT Item (#PCDATA)>
  
 <!ATTLIST Item ItemPrice CDATA #REQUIRED>
   <!ATTLIST Item ItemQty CDATA "1">
]>


Save the file and view in the browser.