CSCI 5733.1
XML Application Development
Summer 2004
Suggested Solution to Final Examination
(1) For example,
<html>
<body bgcolor="#ccccff">
<p>Persons and their email links:</p>
<ul>
{ for $p in doc("yueex/use-cases/yue/ft2.xml")//person
return
<li>
{ if ($p/email) then
<a>
{ attribute href {concat("mailto:",$p/email[position()=1]/text())},
(: note that concat must be used. Otherwise, a space will be created. :)
$p/first/text(),
" ",
$p/last/text()
}
</a>
else ""
}
</li>
}
</ul>
</body>
</html>
(2) For example:
(a) count(//person[link/@spouse])
(b) count(//person[following-sibling::person/link/@father=link/@father or following-sibling::person/link/@mother=link/@mother
or preceding-sibling::person/link/@father=link/@father or preceding-sibling::person/link/@mother=link/@mother])
(c) //email[count(preceding::email) = 3]
(Assume that <emai>l does not have <email> as an ancestor. To be
more exact: //email[count(preceding::email) + count(ancestor::email) = 3]
(d) sum(//person/@luckynumber) div count(//person/@luckynumber)
Note that for (c), //email[4] will not be correct as position() gives the position of the context node as a child of its parent.
(3) For example,
<?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" />
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:element name="numAttributesInSubtree">
<xsl:value-of select="count(.//@*)" />
</xsl:element>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>
(4)
(a) F
(b) T