Digital humanities

Maintained by: David J. Birnbaum ( [Creative Commons BY-NC-SA 3.0 Unported License] Last modified: 2017-04-14T21:08:49+0000

Test #7: XQuery

The text

For this test we’ll be using the version of Hamlet that is available in eXist. You can access it XQuery as doc('/db/apps/shakespeare/data/ham.xml')/tei:TEI

Remember that you'll also need a namespace declaration:

declare namespace tei="";

Don’t forget the trailing semicolon, which is required for XQuery declare statements.

The task

Your goal is to produce a valid HTML document that contains a table of characters in Hamlet, the number of times they speak, and their first line, sorted in descending order of number of speeches. In case of ties in number of speeches, we’ve subsorted our output alphabetically. You can see our output (without the first lines) at You’re welcome to do a view source, but to save you the trouble, the underlying HTML for the two chattiest characters looks like:

  <td>A little more than kin, and less than kind.</td>
  <td>Friends to this ground.</td>

You can do this with a single FLWOR expression. A few things to consider:

If you finish early

If you finish early, you’re encouraged to enhance your output. A few possibilities: