Digital humanities

Maintained by: David J. Birnbaum ( [Creative Commons BY-NC-SA 3.0 Unported License] Last modified: 2017-03-17T02:06:02+0000

Test #5: XSLT

Using Bad Hamlet, write an XSLT stylesheet that will produce an HTML5 table with a header row followed by five data rows, one for each act, and three columns. The leftmost column will give the title of the act (from the <head> element within the act itself). The middle column will give the number of scenes in that act. The rightmost column will contain a comma-separated list of all speakers in that act, without duplicates. Our output is:

Act Number of Scenes Characters
Act 1 5 Bernardo, Francisco, Horatio, Marcellus, King, Cornelius and Voltimand, Laertes, Polonius, Hamlet, Gertrude, Marcellus and Bernardo, All, Ophelia, Ghost, Marcellus and Horatio
Act 2 2 Polonius, Reynaldo, Ophelia, King, Gertrude, Rosencrantz, Guildenstern, Voltimand, Hamlet, Rosencrantz and Guildenstern, First Player
Act 3 4 King, Rosencrantz, Guildenstern, Gertrude, Polonius, Ophelia, Hamlet, First Player, Rosencrantz and Guildenstern, Horatio, Prologue, Player King, Player Queen, Lucianus, All, Ghost
Act 4 7 King, Gertrude, Hamlet, Rosencrantz and Guildenstern, Rosencrantz, Guildenstern, Fortinbras, Captain, Gentleman, Horatio, Ophelia, Laertes, Danes, Servant, Sailor, Messenger
Act 5 2 First Clown, Second Clown, Hamlet, Horatio, Laertes, Priest, Gertrude, King, All, Osric, Lord, Fortinbras, First Ambassador

The characters can be in any order, and you don’t have to worry about the fact that in some acts people speak both separately and together. For example, in our solution, under Act 3 we list separately Rosencrantz, Guildenstern, and Rosencrantz and Guildenstern. In Real Life we’d fix that peculiarity, but for test purposes we’ll ignore it.

A valid HTML5 file must have a properly configured <xsl:output> element and it must specify the HTML namespace. You can remind yourself about how to do that at our XSLT basics page. You should also validate your output HTML in <oXygen/>. You can do that by saving the output to disk and then opening it in <oXygen/> (as we demonstrated in class last week, but as a reminder: use the File icon next to the box labeled Output at the top of the screen to specify an output filename and location), or you can just copy the contents of the output panel in the <oXygen/> debugger view, create a new XML document, and paste the output into it.

We used the XPath count() function to generate the number of scenes in each act, and we used the XPath string-join() function to create our comma-separated lists. If you utilize these functions, you have to use <xsl:value-of> to output their results. You can’t apply templates in these situations because the functions do not return a node, and you can only apply templates to nodes. You can remind yourself about <xsl:value-of> in the section titled <xsl:value-of>at our XSLT, part 2: Advanced features page.