CSCI 5733.1
XML Application Development
Spring 2005
Suggested Solution to Final Examination

(1) For example,

(a)

<result>
{   for $year in fn:distinct-values(doc("yueex/use-cases/yue/bibalt.xml")//book/@year)
   order by $year
   return
      <numBooks>
      {   attribute year {$year},
          attribute num { fn:count(doc('yueex/use-cases/yue/bibalt.xml')//book[@year = $year]) }
      }
      </numBooks>
}
</result>

(b)

  <result>
  {   doc("yueex/use-cases/yue/bibalt.xml")//book[(author/last/text() = "Stevens" and not(author/last/text() = "Suciu")) or (author/last/text() = "Suciu" and not(author/last/text() = "Stevens"))]
  }
  </result>

(2) For example:

(a) //book[author[last/text()='Buneman' and first/text()='Peter']]/author[not(last/text()='Buneman' and first/text()='Peter')] or
//book/author[last/text()='Buneman' and first/text()='Peter']/preceding-sibling::author | //book/author[last/text()='Buneman' and first/text()='Peter']/following-sibling::author

(b) //@year[not(//@year > .)]

Note that //@year[not(number(//@year) > number(.))] does not work. It will include the year 1994 incorrectly.

(c) //book[@year < '1997']

(3) For example,

<?xml version='1.0'?>
<xsl:stylesheet version='1.0'
   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method="xml" version="1.0" indent="yes" standalone="yes" />

<xsl:template match='/'>
<result>
  <xsl:for-each select="//book[not(@year = preceding::book/@year)]" >
   <xsl:sort select="@year" />
   <xsl:variable name="year" select="@year" />
   <numBooks>
      <xsl:attribute name="year"><xsl:value-of select="@year" /></xsl:attribute>
      <xsl:attribute name="num"><xsl:value-of select="count(//book[@year = $year])" /></xsl:attribute>
   </numBooks>
  </xsl:for-each>
</result>
</xsl:template>

</xsl:stylesheet>


(4)

(a) F
(b) F