Answers to the Two Exercises in the Chapter


Here are the answers to the two exercises I gave you in this chapter.

Attribute List

The first exercise was to write an element line given an attribute declaration. Attribute declaration:

 <!ATTLIST PERSON PERSONID CDATA> 

Element line:

 <PERSON PERSONID="176"> 

The value of the attribute can be any type of character data. Attribute declaration:

 <!ATTLIST EMPLOYEE MALE (truefalse "true"> 

Element line:

 <EMPLOYEE MALE="false"> 

The MALE attribute can be either true or false . If it isn't explicitly declared, it defaults to true .

Attribute declaration:

 <!ATTLIST EMPLOYEE STATUS (singlemarrieddivorcedwidowed) #REQUIRED> 

Element line:

 <EMPLOYEE STATUS="divorced"> 

The STATUS attribute can be one of four possible values, as specified in the attribute list. The attribute must be present and specified.

Invalid XML Document

The second exercise was to identify the problems with the XML document shown in Listing 1.22.

Listing 1.22 Incorrect XML Document
 <?xml version="1.0 standalone="no"?>  <!DOCTYPE VENDORS [ <!ELEMENT VENDORS (VENDOR)?>  <!ELEMENT (NAME, LOCATION, BUSINESS, DIVISION+)>  <!ATTLIST DIVISION      NAME   CDATA #REQUIRED      BUDGET CDATA #IMPLIED  >  <!ELEMENT NAME (#PCDATA)>  <!ELEMENT LOCATION (#PCDATA)>  <!ELEMENT BUSINESS (#PCDATA)>  <!ELEMENT DIVISION (#PCDATA)>  <!ELEMENT LOCATION (STREET, CITY, STATE, ZIP)>  <!ATTLIST ZIP CDATA #REQUIRED>  <!ELEMENT STREET (#PCDATA)>  <!ELEMENT CITY (#PCDATA)>  <!ELEMENT STATE (#PCDATA)>  <!ELEMENT ZIP (#PCDATA)>  ]  <VENDORS xmlns:vend='http://www.myorg.com/companytags'>    <VENDOR>        <NAME>Iomega</NAME>        <LOCATION>           <STREET>1821 W.Iomega</STREET>           <comment -- this is a test file-->           <CITY>Roy</CITY>           <STATE>UT</STATE>           <ZIP SUB="8441">84067        </LOCATION>        <BUSINESS>Manufacturing</BUSINESS>        <DIVISION NAME="Sales" BUDGET="350000">        </DIVISION>        <DIVISION NAME="IT" BUDGET="650000">        </DIVISION>        <DIVISION NAME="HR" BUDGET="650000">        </DIVISION>    </VENDOR>     <VENDOR>        <NAME>Dell</NAME>        <LOCATION>           <STREET>1000 W. Addison</STREET>           <CITY>Dallas</CITY>           <STATE>TX</STATE>           <ZIP SUB="3456">40078        </LOCATION>        <BUSINESS>Computer Manufacturing</BUSINESS>        <DIVISION NAME="Sales" BUDGET="650000">        </DIVISION>        <DIVISION NAME="IT" BUDGET="750000">        </DIVISION>        <DIVISION NAME="HR" BUDGET="1650000">        </DIVISION>    </VENDOR>  </VENDORS> 

Here is the list of corrections needed to make this a valid, well- formed XML document and schema.

  • The document defines an xml namespace.

     xmlns:vend='http://www.myorg.com/companytags' 

    The entire document isn't using the prefix vend to define the tags. The document should be a continuation of the following:

     <vend:VENDOR>     <vendNAME>Iomega</NAME> 
  • The question mark ( ? , 0 or 1 elements present) helping to define the VENDORS element should be a plus sign ( + , 1 or more elements present).

     <!ELEMENT VENDORS (VENDOR)?> 
  • The ZIP element's attribute list is missing the name of the attribute.

     <!ATTLIST ZIP CDATA #REQUIRED> 

    It should instead be as follows :

     <!ATTLIST ZIP SUB CDATA #REQUIRED> 
  • There is no comment element defined in the DTD, so the line

     <comment -- this is a test file--> 

    must be an actual comment, in which case it is written incorrectly. It should be as follows:

     <!-- comment -- this is a test file--> 
  • The ZIP element is missing its closing tag in both cases.

  • Bonus points: Did you remember that there is no attribute declared for the VENDOR element that defines the xmlns attribute?



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net