CSCI 5733.1
XML Application Development
Summer 2003
Suggested Solution to Mid-Term Examination

(1) For example,

   //  Data members.
   private static int currentMaxDepth = -1;
   private static int maxDepth = -1;
   private static String currentMaxElementName = "";
   private static int currentMaxAttributeCounts = 0;

   //   Handler methods
   public void startElement(String namespaceURI,
         String lName,
         String qName,
         Attributes attrs)
      throws SAXException
   {   String eName = lName; // element name
      if ("".equals(eName)) eName = qName;
      currentMaxDepth++;
      if (currentMaxDepth > maxDepth)
      {   maxDepth = currentMaxDepth;
         currentMaxElementName = eName;
         currentMaxAttributeCounts = attrs.getLength();
      }
   }   //   startElement

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

   //   Helping method
   private static void printResult()
   {   System.out.println("The first deepest element is <" +
         currentMaxElementName + "> and it has " +
         currentMaxAttributeCounts + " attriubues.");
   }

(2) (a) For example,

  <xsd:simpleType name="mySimpleType">
    <xsd:restriction base="xsd:positiveInteger">
<!-- leading zeroes ok:
      <xsd:pattern value="4|8|(\d*([02468][048]|[13579][26]))"/>
-->
      <xsd:pattern value="4|8|([2468][048]|[13579][26])|([1-9]\d*([02468][048]|[13579][26]))"/>
    </xsd:restriction>
  </xsd:simpleType>

(b) For example:

<!ELEMENT collection (categories, books) >

<!ELEMENT categories (category*)>
<!ELEMENT category (categoryName, categoryDescription?)>
<!ELEMENT categoryName (#PCDATA)>
<!ELEMENT categoryDescription (#PCDATA)>

<!ATTLIST category categoryId ID #REQUIRED>

<!ELEMENT books (book*)>
<!ELEMENT book (author+)>
<!ELEMENT author (#PCDATA)>

<!ATTLIST book callNumber ID #REQUIRED
               title NMTOKEN #REQUIRED
               categories IDREFS #REQUIRED >

(3) (a)

(i) False
(ii) True
(iii) True
(iv) True
(v) False

(b) For example,

use LWP::Simple;
use strict;

my $url = "http://dcm.uhcl.edu/yue/courses/xml/Summer2003/hw/h1dat3.xml";
my $urlContents = get($url);

my $quotes = "";
while ($urlContents =~ /<quote.*?>.*?<author.*?>(.*?)\/author>.*?<content.*?>(.*?)<\/content>/gsi) {
   $quotes .= "$1: $2\n";
}
$quotes = xmldecode($quotes);

print <<__RESULT;
Content-type: text/plain

$quotes
__RESULT
exit 0;

sub xmldecode {
   my $result = shift;
   $result =~ s/&lt;/</gsi;
   $result =~ s/&gt;/>/gsi;
   $result =~ s/&quot;/"/gsi;
   $result =~ s/&apos;/'/gsi;
   $result =~ s/&amp;/&/gsi;
   $result;
}   #   xmldecode