Name: _________________________________
Time allowed: one hour and 20 minutes. Total score: 100%.
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 (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 file, which is used in homework #4 to represent the difference of two XML files:
<?xml version="1.0" encoding="UTF-8"?>
<yuecomp:yuecomp yuecomp:document1="test1_1.xml" yuecomp:document2="test1_2.xml"
xmlns:yuecomp="http://dcm.uhcl.edu/yue/yuecomp">
<root yuecomp:diff="changed" k="1">
<a yuecomp:diff="same">
<b p="1" q="2" />
<c>hello</c>
</a>
<old yuecomp:diff="deleted">
<b p="3" q="4" />
</old>
<new yuecomp:diff="added">
<a><b>this</b>
<c>is<d x="1" y="2" z="3">fun<e
w="4" /></d></c>
</a>
</new>
<f yuecomp:diff="changed">
<yuecomp:sameAttribute k="1" />
<yuecomp:sameAttribute l="2" />
<yuecomp:deletedAttribute j="3" />
<yuecomp:deletedAttribute i="2" />
<yuecomp:addedAttribute j="5" />
<yuecomp:addedAttribute m="2" />
</f>
<g yuecomp:diff="changed">
<yuecomp:sameAttribute x="1" />
<yuecomp:deletedAttribute y="3" />
<h yuecomp:diff="changed">
<yuecomp:oldText>Hello</yuecomp:oldText>
<yuecomp:newText>Goodbye</yuecomp:newText>
</h>
<k yuecomp:diff="same">My friend</k>
<m yuecomp:diff="deleted" d="10" />
<n yuecomp:diff="added" e="11">new n</n>
</g>
</root>
</yuecomp:yuecomp>
Give the XPath expressions to select the followings.
(a) All third level element nodes (i.e. all grandchildren
of the root element "yuecomp:yuecomp")
(b) All attribute nodes that use the namespace
"yuecomp" (e.g. yuecomp:document1, yuecomp:diff, etc).
(c) All elements with exactly two attributes.
(d) All elements that contain the attribute yuecomp:diff with a value of 'same'.
(e) All elements that are the same in the two input files (four elements in
this case: <a>, <b>, <c> and <k> in <a yuecomp:diff="same"><b
p="1" q="2" /> <c>hello</c></a> and <k yuecomp:diff="same">My
friend</k>).
(f) The number of attributes with values '1' (5 in this case: there are one
'p', two 'k' and two 'x' attributes with values '1').
(2) Write a simple XSLT program, removeDummy.xslt, to accept an XML document and output another XML document with the same XML tree structure, except that all subtrees of the element "dummy" are removed. The exception is when the dummy element has an "c" attribute with value "1".
For example, if the input XML document is:
<?xml version="1.0" ?>
<root a="1" b="2" c="3" d="4">
<dummy a="2"><p /><q a="1">hello</q></dummy>
<p><dummy /><q /></p>
<dummy c="1" />
Goodbye.
</root>
The output XML should be (with the possible exception of indentation)
<?xml version="1.0" encoding="UTF-8"?>
<root a="1" b="2" c="3" d="4">
<p>
<q/>
</p>
<dummy c="1"/>
Goodbye.
</Root>
The skeleton of the XSL program is:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" version="1.0" indent="yes" />
<!-- Your XSL code here -->
</xsl:stylesheet>
You only need to write your XSLT templates.
(3) Write a Java program, NumAttributes.java, to use JDOM (DOM or SAX will not be accepted), to output the element hierarchy with the elements and their numbers of attributes of the input XML file.
For example, if the input file numAttributesTest.java is:
<?xml version="1.0" encoding="UTF-8"?>
<root a="1" b="2" c="3">
<!-- Some comments -->
<?somePi ?>
<p x="1">
Hello
<q />
<r />
<s x="1" y="2"><u x="1">ok</u></s>
Good bye
</p>
<p x="1" y="2" z="3" w="4" />
</root>
Running the program:
java NumAttributes numAttributesTest.xml
should print exactly the following to the standard output:
<?xml version="1.0" ?>
<root numAttributes="3">
<p numAttributes="1">
<q numAttributes="0">
</q>
<r numAttributes="0">
</r>
<s numAttributes="2">
<u numAttributes="1">
</u>
</s>
</p>
<p numAttributes="4">
</p>
</root>
Note that only elements in the original XML document are printed. Attributes, text, PI or comments are removed. For every element, an attribute numAttributes is added to show the number of attributes of the element in the input XML document. Proper indentation must be inserted.
The following skeleton is provided for you, so it is only necessary for you to provide the code for the method printOutXml (and possibly other helping methods).
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
public class NumAttributes {
public static void main(String argv[]) {
if(argv.length == 1) {
Document document = readDocument(argv[0]);
if (document != null) {
System.out.println("<?xml
version=\"1.0\" ?>");
printOutXml(document.getRootElement(),
0);
}
}
} // main
private static Document readDocument(String filename) {
try {
SAXBuilder builder = new SAXBuilder();
Document result = builder.build(new
File(filename));
return result;
}
catch(JDOMException e) {
e.printStackTrace();
}
catch(NullPointerException e) {
e.printStackTrace();
}
return null;
} // RreadDcoument
private static void printOutXml(Element root, int level) {
// Body of printOutXml and other code here.
}
} // numAttributes