CSCI 5733
XML Application Development
Spring 2003
Suggested Solution to Final Examination
(1) For example,
<!-- Your XSL code here -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:if test="text()">
<xsl:attribute name="textCount">
<xsl:value-of
select="count(text())" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
(2) For example:
(a) //book[count(chapter) >= 2]
(b) //book[categoryRef/@id=//category[text()='Computing']/@id]/@isbn
(c) count(//book[authorRef/@id=//author[text()='Bun Yue']/@id])
(d) count(//book[not(authorRef)])
(e) count(//author[not(@id = //authorRef/@id)])
(3) For example,
private static int numDept(Document document) {
int result = 0;
HashMap departmentNames = new HashMap();
if (document != null)
{
Iterator divisions = document.getRootElement().getChildren("division").iterator();
while (divisions.hasNext()) {
Iterator faculty = ((Element)
divisions.next()).getChildren("faculty").iterator();
while (faculty.hasNext()) {
Element currentFaculty
= (Element) faculty.next();
String department
= currentFaculty.getAttribute("department").getValue();
departmentNames.put(department,
"yes");
}
}
result = departmentNames.size();
}
return result;
}