CSCI 5733.1
XML Application Development
Spring 2003
Mid-Term ExaminationName: _________________________________
Time allowed: one hour and 20 minutes. Total score: 30 points.
Open: text book, lecture notes, every file I wrote and posted in my Web page and your project assignments. No other materials are allowed.
Answer all questions. Turn in both question and answer sheets. Plan your time well.
Academic honesty policy will be followed strictly. Cheating will result in a failing grade of D or below and a permanent academic record!
(1) [7 points] Write a method private static int numberOfPI(Element element) that accepts a Node and return the number of processing instruction in the node element.
For example, if the element root representing the following xml code is passed to the method, the method will return 5 (since there are five PIs.)
<root>
<?pi_1 ?>
<a>
hello<?pi_2 ?><?pi_3 ?>
<b><?pi_4 ?></b>
</a>
<?pi_5 ?>
</root>(2) [Total: 9 points]
(a) [6 points] Consider the following DTD declaration:
<!ELEMENT a (b, c*)>
<!ATTLIST a
num NMTOKEN #IMPLIED>
<!ELEMENT b (#PCDATA)>
<!ELEMENT c (#PCDATA)>It is known that the values of the elements b and c, and the attribute num should all be positive integers divisible by 10. Write the corresponding XML Schema declaration based on the DTD and the specified type of <b>, <c> and the attribute num.
(b) [3 points] In an XML application, an element <a> may contain the following contents:(i) two <b> child elements followed by any number of <c> and <d> child elements, or
(ii) an <e> child element followed by a text string.The <a> element may also contain an optional attribute "w" with values that are strings of digits and letters.
Give the DTD declaration of <a> that is as restrictive as possible.
(3) [Total: 14 points]
(a) [6 points] Write a Java program, AverageNumberOfAttributes.java using SAX (DOM or JDOM not acceptable) to print out the average number of attributes.
For example, if AverageNumberOfAttributesTest1.xml is
<?xml version="1.0" ?>
<a b="1" c="2">
<b />
<c />
<d b="1" d="3" e="4" />
<e />
<f b="1"><g/></f>
</a>then running
java AverageNumberOfAttributes AverageNumberOfAttributesTest1.xml
displays:
The average number of attributes of the tree is 0.8571428571428571.
Study the following skeleton carefully and then give the definition of any SAX handlers and/or data members required to complete the program. We must not write your own version of the program.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class AverageNumberOfAttributes extends DefaultHandler
{
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java AverageNumberOfAttributes xmlfilename");
System.exit(1);
}
DefaultHandler handler = new AverageNumberOfAttributes();
SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(argv [0]), handler);
// Print out average depth of the XML tree.
System.out.println("The average number of attributes of the tree is "
+ averageNumAttributes + ".");
} catch (Throwable t) {
t.printStackTrace();
}
} // main
// Data members:
private static double averageNumAttributes = 0.0;
// Defined any additional data members here.
// Define required SAX Handler methods
} // AverageNumberOfAttributes.(b) [2 points] Consider the following XML file:
<?xml version="1.0" ?>
<!-- A simple XML file -->
<a>
<b x="1" y="2">
</a>A SAX parser that redefines only handlers in the interface ContentHandler is used to parse the file. Will the event handler ignorableWhitespace ever been called?
(c) [6 points] Write a simple CGI-Perl program to accept an XML document stored in the HTTP parameter "input" (this may be input from a textarea and submitted through a form.) The input XML document has the following DTD declaration:
<!ELEMENT root (a, b*)>
<!ELEMENT a (b*)>
<!ELEMENT b (#PCDATA)>The output should be a list of all <b> elements.
For example, if the input XML document stored in the HTTP parameter "input" is:
<?xml version="1.0">
<root>
<a>
<b>5 < 7</b>
<b>"hello"</b>
</a>
<b>x & y</b>
</root>Your CGI program output should look exactly like the following screen in IE:
![]()
There is no need to handle errors.