CSCI 5931A.1
XML Application Development
Spring 20012
Suggested Solution to Mid-Term Examination

(1) For example,

use strict;
# Get file
die "usage: t1q1.pl inputTextFile outputXMLFile\n" unless (@ARGV >= 2);
die "Cannot open input text file: $ARGV[0]" unless open(IN, "<$ARGV[0]");
die "Cannot open output XML file: $ARGV[1]" unless open(OUT, ">$ARGV[1]");

my $result = "";
while ($_ = <IN>) {
  chomp;
  s/\\\\/\\/g;
  s/&/&amp;/g;
  s/</&lt;/g;
  s/>/&gt;/g;
  s/"/&quot;/g;
  s/'/&apos;/g;
  my ($source, $body, $rank) = split /,/;
  $source =~ s/\\c/,/g;
  $body =~ s/\\c/,/g;
  $result .= " <quote><body>$body</body><source>$source</source></quote>\n";
}
print OUT "<quotes>\n$result</quotes>";
close OUT;
exit 0;

(2)

(a) One error: <son /> should be </son>.

(b) The error messages (not required in answers):

Parsing error at line 8 with URI file:F:/Data/Courses/XMLApplications/Notes/parsers/jaxp/examples/ba
ddtd.xml, message:
Attribute "y" is required and must be specified for element type "b".
Parsing error at line 9 with URI file:F:/Data/Courses/XMLApplications/Notes/parsers/jaxp/examples/ba
ddtd.xml, message:
The content of element type "c" must match "(a*,b?)".
Parsing error at line 9 with URI file:F:/Data/Courses/XMLApplications/Notes/parsers/jaxp/examples/ba
ddtd.xml, message:
An element with the identifier "_2" must appear in the document.

The right DTD, for example:

<!ELEMENT c (a*, b*)>
<!ELEMENT a EMPTY>
<!ATTLIST a x CDATA #IMPLIED>
<!ELEMENT b (#PCDATA)>
<!ATTLIST b y CDATA #IMPLIED>

 

(c) For example:

<!ELEMENT graph (nodes, edges)>
<!ELEMENT nodes (node*)>
<!ELEMENT edges (edge*)>

<!ELEMENT node EMPTY>
<!ATTLIST node name ID #REQUIRED
               weight CDATA #IMPLIED>

<!ELEMENT edge EMPTY>
<!ATTLIST edge name ID #IMPLIED
               from IDREF #REQUIRED
               to IDREF #REQUIRED>

 

An XML file complying to the DTD:

<?xml version="1.0"?>
<!DOCTYPE graph SYSTEM "graph.dtd">
<graph>
  <nodes>
    <node name="n1" weight="20" />
    <node name="n2" />
    <node name="n3" weight="10" />
  </nodes>
  <edges>
    <edge name="e1" from="n1" to="n2" />
    <edge from="n1" to="n3" />
    <edge name="e2" from="n3" to="n2" />
    <edge name="e3" from="n2" to="n1" />
  </edges>
</graph>

(3) For example,

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC
"-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="start">
    <p>
      <anchor title="quotations"><go href="#card1"/>Start quotations</anchor>
    </p>
  </card>
  <card id="card1" ontimer="#card2">
    <timer value="50"/>
    <p>
      <anchor title="quote 1"><go href="#start"/>Home</anchor>
    </p>
    <p>Quotation: I love XML.</p>
  </card>
  <card id="card2" ontimer="#card3">
    <timer value="50"/>
    <p>
      <anchor title="quote 2"><go href="#start"/>Home</anchor>
    </p>
    <p>Quotation: I love Visual Basic.</p>
  </card>
  <card id="card3" ontimer="#card1">
    <timer value="50"/>
    <p>
      <anchor title="quote 3"><go href="#start"/>Home</anchor>
    </p>
    <p>Quotation: I love Java.</p>
  </card>
</wml>

(4) For example,

// Data members.
private static int nodeLevel = 0; // current element level.
private static int rootChildrenCount = 0; // Number of children nodes of the root.

// Handler methods
public void startElement(String namespaceURI,
    String lName,
    String qName,
    Attributes attrs)
      throws SAXException
{
  nodeLevel++;
  if (nodeLevel == 2) rootChildrenCount++;
} // startElement

public void endElement(String namespaceURI,
String lName,
String qName)
throws SAXException
{
  nodeLevel--;
} // endElement

// utility methods.
private static void printRootChildrenCount() {
  System.out.println("Number of children elements of the document root = " + rootChildrenCount + ".");
} // printRootChildrenCount