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.
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:
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.
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.
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:
<poemTitle>
elements and change them to <title>
elements. Make sure you preserve their contents by running <xsl:apply-templates/>
inside your template rule.<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.<line>
into self-closing elements that also supply the line number value in an attribute, like this: <lb n="2"/>
: lb
element sits at the start of each line.@n
attribute that numbers the lines inside the poem. (See examples in the Obdurodon AVT tutorial: You can calculate this with XPath with count()
of the preceding::line
elements in the document and adding 1. Look at the difference if we change the XPath axis—counting the preceding-sibling::line
and preceding::line
elements: why would we prefer one axis over the other?)<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.)Run-to-Endbutton. 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:
Save as.
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.