CSCI 5733.1
XML Application Development
Spring 2005
Mid-Term ExaminationName: _________________________________
Time allowed: one hour and 20 minutes. Total score: 30 points.
Open: lecture notes, every file I wrote and posted in my Web page and your project assignments, but no book.
Answer all questions. Turn in both question and answer sheets. Plan your time well.
Academic honesty policy will be followed strictly. Cheating will be pursued vigorously and will result in a failing grade of D or below, a permanent academic record and possibly other more serious penalty!
(1) [8 points] Write a Java program, AverageNumberOfAttributes.java, using JAXP/SAX (other techniques not acceptable) to read an XML file and output the average number of attributes per element.
For example, running the program
java AverageNumberOfAttributes h1q1a.xml
where h1q1a.xml is:
<?xml version="1.0" ?>
<root a="1">
<p b="2" c="3" d="4">
<q />
<r />
<s e="5" f="6" g="7" h="8">hell0</s>
</p>
</root>output:
The average number of attributes of the XML file is 1.6.
This is because there are five elements in h1q1a.xml: <root>, <p>, <q>, <r> and <s>, and there are eight attributes, a to h.
(2) [8 points] Design a reasonable DTD for capturing the data in the following highly simplified application. A small art gallery wants to catalog its collection. An art collection contains many art artifacts. Every artifact has a title, a date of acquisition, an acquisition price and a sale price. An artifact has exactly one primary artist and possibly zero to many secondary artists. Each artist has a last name, a first name and a social security number (which is required for tax purpose, when an artifact was sold). You may make additional reasonable assumptions but the basic requirements listed above must be satisfied as much as possible.
(3) [5 points] True or false. No justification is necessary. Just answer 'T' or 'F' in the question paper.
(a) In DOM, Node is a superclass of Element.
(b) In XML Schema, a simpleType cannot have simpleContent as its content.
(c) In XML Schema, a simpleType can be used as the data type of an element.
(d) DTD validation and XML Schema validation cannot be performed for an XML document in one parsing.
(e) W3C's DOM does not apply to HTML.(4) Short questions
(a) [3 points] The Java's variable e has been declared as org.w3c.dom.Element. Assume that e is currently referring to an element <A>, which is declared in the DTD as:
<!ELEMENT A (#PCDATA)>
Write the Java code to print out the textual content of e. For an example, if e is referring to the Element node:
<A>Hello</A>
you code should print out:
Hello
Note that <A> may be empty. There is no need to include error handling code.
(b) [3 points] Provide the definition of the type positiveIntegerWithComma in XML Schema, which will accept only strings representing positive integers, with the possible inclusion of commas before every three digits. For examples, it should accept "1234567", "1,234,567", "12", etc, but not "023", "12,34", "1,234567", etc.
(c) [3 points] Consider the following CGI-Perl program that serves the time of the server in XML:
use strict;
my $time = localtime;
print <<__END;
<?xml version="1.0" ?>
<servertime>
Date and time: <$time>
</servertime>
__END
exit 0;
The output is supposed to look like this in a browser (Mozilla here):
There are one or more errors in the program. Correct them in the question paper.