NewtFire logo: a mosaic rendering of a firebelly newt
newtFire {dh}
Creative Commons License Last modified: Monday, 04-Nov-2024 13:29:11 UTC. Maintained by: Elisa E. Beshero-Bondar (eeb4 at psu.edu). Powered by firebellies.

Our very first XSLT assignment is an Identity Transformation, a kind of transformation we have to do frequently in our projects when we need to make specific changes to our encoding. We will work with a file originally prepared by a Fall 2020 DIGIT 110 project team working with a collection of poems by Langston Hughes, Montage of a Dream Deferred. We will write XSLT to make some changes to the way this document is coded.

Source files

Download these files to work with for this assignment. (You may also pull them in from our class GitHub repo.) Save them with your homework files:

  1. montageDreamDeferred2.xml
  2. IDTransform-starter.xsl

Open the files in oXygen, and switch to the XSLT debugger view (as shown in class) so you can see the XML file on the left and the XSLT file in the middle. The output you create with XSLT will be shown on the right.

Writing an Identity Transformation

An identity transformation in XSLT is designed to make minor changes to a document but preserve most of it intact. There is a special command at the top of our starter XSLT file to make it so, if we don’t write any rules at all, we will simply copy each node, step by step, from parent to child, through the source XML tree. In an identity transformation, we only need to write rules that make changes to particular nodes we want to change on the XML tree.

We are going to use a clear and simple command for this identity transformation in version XSLT 3.0, so that is why we have set version="3.0" in our stylesheet template. (In general, XSLT 3.0 and XPath 3.1 are better to work with than the earlier 2.0 versions, and you will want to set these by default in your oXygen XML Editor.) Our identity transformation command (in your starter XSLT) sets a mode, or a way of handling elements when we don't write any rules in the XSLT to match them:

         <xsl:mode on-no-match="shallow-copy"/>

This XSLT mode on-no-match="shallow-copy" basically says, Hi there! If I do not write a template rule to match an element, attribute, or comment node, really of any part of the document that I do not mention in a template match rule, then simply make a copy of that element and output it just exactly the way it is. Test this by running the XSLT (as shown in class and in the Introduction to XSLT tutorial). Look at your output: it will look exactly identical to the current XML document. This makes it easy for us to write an identity transformation that leaves most of the document alone, and where any template rules we write will make changes.

Writing XSLT Template Rules to Change the Nodes

We write XSLT template rules to make changes to nodes in our source document and output them in a new way, in a new document. Your task in this assignment is to remove and change some elements, and add a new attribute. To do these things, you will need to:

You will write xsl:template rules that @match on nodes in the source document and output their contents in a new way. Here are your tasks:

  1. Write a template rule to match on the <poemTitle> elements and change them to <title> elements. Make sure you preserve their contents by running <xsl:apply-templates/> inside your template rule.
  2. Look at how each poem is coded, with a <body> wrapping around the its contents. Try to write a template rule that matches on the body element and simply removes it, while still outputting its children and descendants.
  3. The next tasks involve changing some attributes, working with Attribute Value Templates (or AVTs). An AVT offers a special way to extract or calculate information from our input XML to output inside an attribute value. You need to look at some examples of AVTs in order to write one yourself, so study the examples in Obdurodon’s Attribute Value Templates (AVT) tutorial.
  4. Now that you have looked at some examples of AVTs, try writing a template rule to change the <line> into self-closing elements that also supply the line number value in an attribute, like this: <lb n="2"/>:
  5. Each <poem> should have a distinct identifier, usually called an @xml:id attribute. Write an attribute value template to add an @xml:id attribute to each poem that includes a string of text, "p-" followed by its count in the sequence of poem elements, which you will calculate with XPath. This portion will be similar to the code for the line-numbering. (See examples in the Obdurodon AVT tutorial.)
  6. You will be looking at your results in the Output window as you write and test your template rules each time you press the blue Run-to-End button. Beware! Eye-balling those results is not good enough because the Output window does not check for well-formedness or validation against a schema. What to do now:

When you are finished, save your XSLT file and your XML output, following our usual homework file naming conventions, and upload these to the appropriate place in Canvas.