CSCI 5733.1
XML Application Development
Summer 2004
Final Examination

Name:  _________________________________

Time allowed: one hour and 30 minutes.  Total score: 30.

Open: lecture notes, every file I wrote and posted in my Web page and your project assignments.  No books or other materials are allowed.

Answer all questions.  Turn in both question and answer sheets (with the question sheets on top).  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) Consider the following xml document, ft2.xml:

<?xml version="1.0"?>
<!-- head comments -->
<familytree>
    <!-- second level comments -->
   <meta>Becker</meta>
    <person ssn="s123456789" gender="M" luckynumber="7">
       <!-- third level comments -->
        <first>Boris</first>
        <last>Becker</last>  
        <email>boris@becker.com</email>
        <email>boris@hotmail.com</email>
        <link spouse="s111222333" children="s123123123 s222333444" father="s555987323" mother="s887667545" />
    </person>
   <?br ?>
   <?nl ?>
    <person ssn="s111222333" gender="F" luckynumber="6">   
        <first>Valerie</first>
        <last>Becker</last>
        <email>valerie@hotmail.com</email>
        <link spouse="s123456789" children="s123123123 s222333444"></link>  
    </person>
    <person ssn="s123123123" gender="M" luckynumber="4">
        <first>Chris</first>
        <last>Becker</last>
        <email>chris@becker.com</email>
        <link father="s123456789" mother="s111222333" luckynumber="10" />
    </person>
    <person ssn="s222333444" gender="F">   
        <first>Julie</first>
        <last>Becker</last>
        <email>julie@hotmail.com</email>
        <link father="s123456789" mother="s111222333" />
    </person>
    <person ssn="s555987323" gender="M">
        <first>John</first>
        <last>Becker</last>
        <email>john@becker.com</email>
        <link spouse="s887667545" children="s123456789"/>
    </person>
    <person ssn="s887667545" gender="F">   
   <first>Mary</first><last>Becker</last> 
   <email>mary@hotmail.com</email>
    <link spouse="s555987323" children="s123456789"  />
    </person>
</familytree>

Write an XQuery program to output the following HTML code for listing the email link of every person in the following format. If a person has more than one email address, the first one should be used. For ft2.xml, your xQuery should output the following exactly:

<html>
   <body bgcolor="#ccccff">
      <p>Persons and their email links:</p>
      <ul>
         <li><a href="mailto:boris@becker.com">Boris Becker</a></li>
         <li><a href="mailto:valerie@hotmail.com">Valerie Becker</a></li>
         <li><a href="mailto:chris@becker.com">Chris Becker</a></li>
         <li><a href="mailto:julie@hotmail.com">Julie Becker</a></li>
         <li><a href="mailto:john@becker.com">John Becker</a></li>
         <li><a href="mailto:mary@hotmail.com">Mary Becker</a></li>
      </ul>
   </body>
</html>

(2) Using ft2.xml, give the XPath expressions for the following queries.

(a) The number of persons married.
(b) The number of persons who have siblings.
(c) The fourth email element that appears in the XML document.
(d) The average value of all luckynumber.
      
(3) Write an XSLT program to preserve the elements, attributes and text of the input XML document. In addition, for every element e, add a child element, <numAttributesInSubtree>, as its first child to show the number of attributes of all elements in the subtree with element e as the root.

For example, for the input XML document:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root a="1" b="y" y="3">
   <p y="y">
      <q />
   </p>
   <r c="1" />
   Hello
   <s y="1">
   Bye
   </s>
</root>

your program should output:

<?xml version="1.0" encoding="UTF-8"?>
<root a="1" b="y" y="3">
<numAttributesInSubtree>6</numAttributesInSubtree>
   <p y="y">
<numAttributesInSubtree>1</numAttributesInSubtree>
      <q>
<numAttributesInSubtree>0</numAttributesInSubtree>
</q>
   </p>
   <r c="1">
<numAttributesInSubtree>1</numAttributesInSubtree>
</r>
   Hello
   <s y="1">
<numAttributesInSubtree>1</numAttributesInSubtree>
   Bye
   </s>
</root>

(4) True or false.

(a) A WML variable must be declared before using.
(b) It is not possible to change a variable in XSLT 1.0.