Download 12_XSL_worksheet

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Asynchronous I/O wikipedia , lookup

Clusterpoint wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Metadata wikipedia , lookup

Web analytics wikipedia , lookup

Operational transformation wikipedia , lookup

Search engine indexing wikipedia , lookup

Semantic Web wikipedia , lookup

XML wikipedia , lookup

Transcript
Class 12 - Using Metadata to create information
services
Instructions:
Work individually or in groups to complete the worksheet. When you get to a section that requires
you to select a resource to explore – pick one resource (please don’t always choose the first one!).
When asked to ‘discuss as a group’, consider your response and continue completing the worksheet.
We’re going to work with computer coding today and here’s an important note as you follow
the exercises. Computer code is shown on numbered lines and are enclosed in boxes. The
numbered lines are simply to help as a reference during instruction and should not be copied into
your program. For example a line that reads 56. p { visibility:hidden; } should simply be typed in as
p { visibility:hidden; }
Exercise Overview
So far this semester we have explored how metadata and structured information is used as a core
component of digital documents (e.g. HTML, RDF) and how representations of print and digital
documents can be described using multiple metadata schemas (e.g. MARC, Dublin Core and
Qualified Dublin Core) and encoded using multiple encoding platforms (e.g. HTML, XML and MARC).
In order to better understand the information systems needs behind the creation and representation
of digital documents we are going to learn about the role of metadata and structured information in
creating information services.
For our purposes an information service is defined as “an operation or task that fills a specific
information need.” We experimented with information services earlier in the semester when we used
Cascading Style Sheets (CSS) to create custom views of an HTML document and when we used
JavaScript to harvest and re-purpose document metadata.. We have also seen how a suite of
Metadata Standards and Web Services
Erik Mitchell
Page 1
information services can be designed together to help librarians accomplish the cataloging tasks (e.g.
MARCEdit).
In this exercise we are going to explore a technology called eXtensible Stylesheet Language (XSL).
XSL is similar to the other metadata schemas that we have explored (HTML, DC, MARC) in that it
has a defined set of elements and attributes. It is also similar to HTML in that it is designed to be
processed by a specialized program also known as an interpreter. While HTML documents are
designed to be processed by web-browsers, XSL documents are designed to be processed by an
XSL processing engine. HTML Browsers have pretty good XSL processors in them and we will see
that in action but there are also specialized applications such as Exchanger that allow you to apply an
XSL style sheet to an XML file.
In this set of exercises, we will create XSL programs (also called XSL templates) and become familiar
with how to apply them to XML files. In our first exercise, we will create a common ‘first program’ for
many people, the hello world program. In our second and third exercises, we will use an RSS/DC xml
file and see how XSL interacts with that file as we create HTML documents.. Our development
platform will be Exchanger. We will use a web-browser as our transformation engine. Our goal for
this exercise is to be familiar enough with the transformation process to create an XSL document that
publishes a more user-friendly version of XML files.
Review of programming concepts - XML & XSL
In this exercise, we will create an XML file that contains the words “hello world” and use XSL to
retrieve data from that file. In doing so we will revisit some of the concepts we introduced during our
JavaScript work.
Model – View – Controller: You should recall the MVC development model that we discussed in our
first few weeks of this course. In this exercise our XML document is our Model (or data) and our XSL
document is our Controller (the engine that determines how data is processed and displayed). We
will use XSL to transform our XML document into an HTML document (remember our metadata
service ‘conversion’?).
Metadata Standards and Web Services
Erik Mitchell
Page 2
Metadata rich web services: XSL is one of the primary technologies used to create metadata
services that convert or transform XML documents. As we will see in the next few weeks, the ability
to transform metadata both into new standards (e.g. EAD >> DC, MARC >> DC) and new document
models (EAD >> PDF, EAD >> HTML) is an important function of information organization.
Functions: A function is a set of programming code designed to be executed as a block. In
JavaScript, we called functions methods because the function was actually a method that belonged
to a particular object. Unlike JavaScript, XSL does not use the same object notion but it does
employ the concept of hierarchy in referring to content values (more on this in a moment).
Variables and Values: You will recall in JavaScript that we were able to extract values from the
objects. For example we extracted the URL from our Window object by referring to the value of
window.location.host. In XSL we focus on accessing values from our document instead of the
window object but we still access items according to the rules of the Document Object Model.
Control structures: In programming there are a number of control structures that help your program
make decisions about how and when to process data. The most popular control structures in
programming include if/then/else and for-each:
1. If, then, else: An If, then, else statement routes the program through a decision making
process (e.g. If variable a is equal to variable b then do set of instructions y. Else, do set of
instructions x). If, then, else statements are excellent for limiting what gets output in a
transformation or for testing whether or not a variable has a specific value
2. for each: For each statements tells the program to repeat a set of instructions for every
instance of data that is included in the definition (e.g. for each metadata element title in this
XML document do set of instructions y). For-each statements are particularly useful in XSL
templates when dealing with an XML document that contains repeating XML records (e.g.
an RSS feed).
Architecture of an XSL transformation
When we transformed our HTML page using CSS and extracted data from our Window object we
used the Web browser as our interpreter. The process of loading the document into a web browser
Metadata Standards and Web Services
Erik Mitchell
Page 3
triggered the interpreter to run our code (e.g. the JavaScript or CSS) and display a new document in
our web browser. Figure 2 demonstrates the transformation process for HTML, CSS and JavaScript
documents
Figure 1: Anatomy of a web browser transformation
Input Documents
Interpreter
Output
•HTML
•CSS
•JavaScript
•Web browser
•Formatted HTML
•Redirected webpage
XSL uses a similar technique but in addition to a web browser, it can be interpreted by an XSL
engine. These transformation engines are included in programming languages (e.g. Java, Python) in
web browsers and in XML editors (e.g. Exchanger). In this exercise we will see how this
transformation process works. Figure 2 modifies the transformation model to fit the XSL process
specifically.
Figure 2: Anatomy of an XSL transformation
Input Documents
ITransformation engine
Output
•XML
•XSL
•Exchanger
•Web browser
•Programming language
•Formatted HTML
•PDF file
•A new XML document
Metadata Standards and Web Services
Erik Mitchell
Page 4
XSL is also commonly implemented in web servers and serve as a display template platform.
Templating is when It is popular in some notable digital library applications including Dspace and
Fedora (http://duraspace.org). It is also a part of a metadata publishing and harvesting standard
known as OAI/PMH (Open Archives Initiative Protocol for Metadata Harvesting). While this is
important to know, In this exercise we will focus on XML editor and web browser transformations to
learn more about these and other concepts but before we do lets get started with a simple
transformation.
Creating our first XSL/XML document transformation
Step 1:
Create a new XML file in your XML editor. This file follows a simple made up standard
using root as a main element and message as :
Code example 1: Sample XML document
1. <?xml version="1.0" encoding="UTF-8"?>
2. <root>
3.
<message>Hello World</message>
4.
<author> Erik Mitchell </author>
5. </root>
a. Save the XML file, giving it a name of your choice
Key Questions
In the above XML file take a moment to identify the core elements:
Question 1. The root element of the document is:
Question 2. The value of the element <message> is:
Metadata Standards and Web Services
Erik Mitchell
Page 5
Take a moment to diagram the hierarchical structure of our xml document. Refer to Figure 3 for an
idea on how to represent elements and relationships
Figure 3: Diagram of an HTML document
Recall for a moment the work we did with the Document Object Model (DOM). The DOM defines a
method that we can use to access elements and attributes in our document and help us work with
documents to either control display, manipulate content or, in this case, run a program that turns that
content into a new document.
XSL can move through the elements of the XML file (or can traverse the DOM) using a method called
XPath. XPath is a querying language similar in many ways to structured query language (SQL) used
in relational database management systems. Just like SQL, XPath allows you to get specific data
from your XML document. XPath statements are pretty easy to create:
In JavaScript we used a dot (.) notation to descend the hierarchy our objects (e.g.
window.location.url). XPath uses a similar concept (the ability to traverse a hierarchical document
structure) but uses forward slashes (/) instead of dots (.). For example, an XPath statement to get the
value of the <message> element in our sample XML document would be:
6. /root/message
Metadata Standards and Web Services
Erik Mitchell
Page 6
Let’s spend a moment with this example:
/:
The initial / indicates that we want to start at the beginning of our document tree
root:
This is the first element in our XML file
/:
We use / to separate elements
message:
This is the second element in our XML file
XPath in essence looks a lot like a hierarchical file system structure because it basically is a
hierarchical structure. XPath supports absolute references (e.g. /root/message) and contextual
references (e.g. ./message). A contextual reference is usually used within the context of another
element (more about this later!).
XSL functions
Like HTML, XSL is a metadata standard whose elements serve specific purposes. For example, in
HTML the element <p> denotes a paragraph and unless you use CSS or JavaScript to override its
default behavior your web browser will output the <p> element with specific formatting. XSL is similar
but instead of being a document markup language like HTML it is a rudimentary programming
language. This means that the elements and attributes of XSL are interpreted by a web browser or
XSL interpreter to perform specific tasks. These tasks include a number of functions, each of which
are documented in Table 1.
Table 1 Chart of common XSL elements, attributes and their function
XSL element and attribute
Function
combination
<xsl:value-of select=””/>
The XSL element “value-of” is used to retrieve the value from
an XML document that is being processed. The attribute
select is used to pass a valid XPath statement (e.g.
/root/message) to the interpreter so that it knows where to pull
Metadata Standards and Web Services
Erik Mitchell
Page 7
the information from
<xsl:template
The XSL element template defines a block of code to be
match=””>…</xsl:template>
executed over a given set of XML data. The template element
will contain a series of instructions that apply to all of the XML
data that is matched using the XPath statement contained in
the match attribute. (for an example see the code under step
2)
<xsl:stylesheet>…</xsl:stylesheet>
This element is the root element of every XSL document. Just
like the XML declarations in XML documents this element
defines namespaces and XSL versions
<xsl:for-each select=””></xsl:for-
The XSL element for-each defines a control structure that is
each>
used to loop over a repeating set of XML data defined in the
select attribute. All of the code contained within a for-each
statement is executed for every instance of XML data
matching the XPath statement.
While there are a number of approaches to creating an XSL document we will follow a method that
depends on the template function to control program flow. When an XSL interpreter processes and
XML and XSL document set it begins at the beginning of the XSL document and process instructions
in sequence. Take a quick look at the code below in order to acquaint yourself with the general
structure of an XSL document
Code example 2: Example XSL document
1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2. <xsl:template match="/">
3.
<html>
4.
<body>
Metadata Standards and Web Services
Erik Mitchell
Page 8
5.
<h1>
6.
<xsl:value-of select="/root/message"/>
7.
</h1>
8.
</body>
9.
</html>
10. </xsl:template>
11. </xsl:stylesheet>
1. You notice that an XSL document will begin with the root xsl:stylesheet element. This tells the
processor that it is dealing with the XSL standard
2. On line 2 we open our xsl:template element. The template element is used to define a block of
xsl code contained on lines 3 through 9. This code is executed for any XML document
elements matching the XPath statement passed in using the match element. In this case,
because we passed in the XPath statement “/”, the XSL processor retrieves the entire XML
document and moves to the next instruction set.
a. A small note on creating complex XSL documents. In creating a large XSL
transformation you would likely create multiple template elements (or functions) each of
which would apply to different sections or “slices” of an XML document. In our case we
are using a single template to keep things simple
3. Because the goal of our XSL is to output an HTML document lines 3,4 5,7,8 and 9 all contain
HTML code. Because these elements are not part of the xsl namespace they are simply
passed through the interpreter without any transformation.
4. On line 6 we tell our XSL interpreter to get a value from our XML document corresponding to
the “/root/message” XPath statement. This is accomplished through the use of the value-of
element with the select attribute. The XSL processer retrieves this value from the XML
document and outputs it to our HTML document.
Key Questions
Take a moment to examine the code here and answer the following questions
Metadata Standards and Web Services
Erik Mitchell
Page 9
Question 3. What is the namespace of the value-of element?
Question 4. What attribute does the value-of element have?
Question 5. What is the value of the attribute of the value-of element?
Question 6. When the XSL processor executes the value-of element what will it output (use the
sample XML document on page 2 as your data source)
Question 7. What is the meaning of the last “/” in the <xsl:value-of…> element?
Question 8. Which lines in this document contain elements from the XSL schema?
Question 9. What is the role of the xsl element template?
There are multiple ways to process and XML and XSL document set. The first way that we ill explore
involves Exchanger. However, all modern web browsers also support XML/XSL processing and it is
possible to call an XSL document from within an XML document using a special element similar in
function to the XHTML code that we used to reference a CSS style sheet in our XHTML document.
Step 2:
Now that we have a basic understanding of how XSL and XPath work Let’s create an XSL
file that we will use to transform our XML file into an HTML file.
a. Create an XSL file in your XML editor:
b. Click on New >> select XML file. If prompted, choose “1.0”
Notice the declaration of the XSL namespace in the stylesheet element. This is just like our work with
Dublin Core in XML. You have to define the namespaces that you are going to use in your document.
Metadata Standards and Web Services
Erik Mitchell
Page 10
The powerful aspect here is that defining a namespace brings in a suite of functions that we will use
to process our XML document! Notice also the use of HTML elements in the program. XSL
documents are primarily designed for transformation and output – they access values in a source
XML document and echo those values along with formatting to the screen or to a new document.
Key Questions
Take a moment to look at the program below and answer the following questions:
12. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
13. <xsl:template match="/">
14.
<html>
15.
<body>
16.
<h1>
17.
<xsl:value-of select="/root/message"/>
18.
</h1>
19.
</body>
20.
</html>
21. </xsl:template>
22. </xsl:stylesheet>
Question 10.
Look at the element <xsl:template> Notice that this element has an attribute
(match) with the XPath value (/). Assume that we called this stylesheet from our other XML
document. Where in this source XML document does this XPath expression match?
Question 11.
Consider what you have learned so far about XSL. If we used this XSL file to
transform our XML document what type of document would be output?
Metadata Standards and Web Services
Erik Mitchell
Page 11
Step 3:
Create a new xml file and type in the XML document from Code example 1. Make sure that
the document is well-formed. Under the author element substitute your name. It will not
validate as it does not have a metadata schema referenced in it. Save the XML document
with a file name of your choice.
Step 4:
Create a second new document and type the program from Code example 2 into it. Make
sure your XSL stylesheet is well formed and validates. This document validates because
we reference the XSL metadata schema. Save the document with a file name of your
choice. (hint – it helps to save it in the same directory as your XML document)
Step 5:
With your two documents created we are ready to XML document using your XSL
stylesheet using our web browser as an interpreter. In order to do this we need to assign
your XSL file to your XML file
a. To accomplish this, add the following line of code in the top of your XML document right
under the XML element declaration and before your <root> element (e.g line 2 of your
XML document)
23. <?xml-stylesheet type="text/xsl" href="YOURXSLFILENAME.xsl"?>
This command links our XSL stylesheet to our XML document. The two key attributes here
are type (defines the type of document) and href (defines the location of the document).
Step 6:
From within a web-browser, Open your XML file. What do you see?
XSL is also used as a means by which you can transform a document to fit another metadata schema
or encoding standard (e.g. in the case above we transform from Dublin Core XML to HTML). When
your goal is to transform metadata for later use you will want to use the XSL transformation function
in Exchanger. Lets experiment with this type of transformation before moving on to a more
complicated approach.
Step 7:
Alternate method using exchanger to transform your document
a. Have both documents open in exchanger
b. Go to the XML document
c. Click on Transform >> Execute Simple XSLT
d. Make sure “Current Document” is selected, click ok
Metadata Standards and Web Services
Erik Mitchell
Page 12
e. Make sure that open document is selected for XSLT, use the dropdown to choose your
XSLT document, click ok
f. Select Saxon XSLT 2.0 from the Processor dialog box, click ok
g. Once the processing window says “Transformation Complete” click hide
h. find the new document that was created in Exchanger (it should be open in a new tab)
Step 8:
Key Questions
Consider the following schematic representing a webpage built with XML, XSL and CSS.
Question 12.
In this model which elements are the model, the view and the controller?
If you wanted to include your name from the XML document what code would you add to your
XSL document? (Curious to see this happen? modify your XSL document and reload it in your web
browser).
Metadata Standards and Web Services
Erik Mitchell
Page 13
So far in this exercise we have learned that XSL can selectively display data from an XML file
and can output the data in a number of different formats (e.g., HTML, PDF, XML) Can you think
of any web services that make use of this technology already?
Advantages
Disadvantages
Working with multiple records in an XML file
In our first exercise we found that XSL can transform an XML document into an HTML document. We
became familiar with common XSL elements including template and value-of and learned how to
process our XML and XSL documents using both a web browser and XML editor. In this exercise we
will expand on this knowledge by learning to work with more complex XML document (i.e. those that
contain more than one metadata record). In order to do this we will learn how to use the control
structure element for-each. While this may seem complicated it is in fact just the beginning of what
XSL can do. XSL has so many functions that it ca be used to create rich dynamic web services. In
this exercise, we will get started with this by working with an RSS file encoded in XML. XSL is
commonly used to transform RSS feeds. This makes the RSS feed more user friendly and creates
an interactive web service.
A brief introduction to RSS
RSS stands for Rich Site Summary (or Real Simple Syndication or RDF Site Summary) and is a
metadata standard designed to support dissemination of metadata over the internet. RSS metadata
Metadata Standards and Web Services
Erik Mitchell
Page 14
features a document header that defines the source, author and time-stamp of the included metadata
records and a repeating series of brief metadata records that point to recently published stories,
HTML documents or other web resources. RSS is commonly used as a “push” service in which
metadata about updated stories are pushed to users to view in an RSS Reader. Popular RSS
readers include Google Reader, RSS Feed reader and smart phone apps like Feeddler.
In order to understand how to transform RSS document using XSL we first need to understand a bit
more about the RSS structure. Lets begin by obtaining an RSS document and comparing it with the
RSS 2.0 specification (http://cyber.law.harvard.edu/rss/rss.html)
Step 9:
Obtain an RSS feed from the New York Times:
a. Visit http://nytimes.com/rss in yoru web browser.
b. Select an RSS feed and download it to your computer
i. Right click on the feed url and select “Save link as”
ii. Save it with the file extension .xml
Step 10: Explore the RSS feed in Exchanger
a. Open exchanger and open the RSS feed that you downloaded. If you have difficulty
downloading the document copy the link URL from your web browser and use (Open
Remote) to open the URL in exchanger.
b. Check to see if the document is well-formed. Note – your RSS feed will likely not
validate as NyTimes does not include a link to the RSS schema!
c. Using the RSS 2.0 specification and your XML document answer the following
questions
Key Questions
Question 13.
What namespaces are defined in the rss element?
Question 14.
What RSS element is used to contain the header information?
Metadata Standards and Web Services
Erik Mitchell
Page 15
Question 15.
What RSS element is used to repeat information about each RSS entry? Look
for the parent element, not all of the children of that element.
Question 16.
Does your RSS feed items include any categorization information? What
categorization schema has been applied?
The structure of an RSS feed
RSS feeds are at the same time simple and very complex. The general structure of a feed is
documented in code example 3.
Code example 3: Basic structure of an RSS document
7. <rss>
8. <channel>
9. <title></tile>
10. <pubDate></pubDate>
11. <item>
12.
<title></title>
13.
<link></link>
14. </item>
15. <item></item>
16. </channel>
17. </rss>
RSS documents introduce the concept of a multi-level hierarchy that we have not yet seen in XML
documents. Notice that the root element of an RSS document is RSS but that it has only one child
element (channel). The channel element contains both header information (e.g. title, pubDate…) and
repeating item elements which contain the actual metadata about the contents of the RSS feed. In
order to access these elements, the same principles of XPath apply (e.g. start at the beginning and
Metadata Standards and Web Services
Erik Mitchell
Page 16
use / to create an XPath to your desired metadata element. For example, an absolute XPath
statement to the pubDate elment would be (/rss/channel/pubDate).
Before moving on, be sure you can answer the following questions.
Key Questions
Question 17.
What is an absolute XPath expression to the <item> element?
Question 18.
What is an absolute XPath expression to the <title> element that is a direct
descendant of the channel element of the RSS feed?
Question 19.
What are the relative (e.g. from <item>) XPath expressions for the item, item
title and link?
a. item XPath:
b. item title XPath:
c. Link XPath:
Before we move too far along with this process we need to review one more XSL element
<xsl:for-each>: This element creates a looping structure that repeats for every instance of an
element. So in our XML file we have multiple <item> elements. This means that we will use an
<xsl:for-each> loop to get to every <item> element.
Metadata Standards and Web Services
Erik Mitchell
Page 17
Absolute vs. Relative XPaths
There is one more concept we need to introduce before getting to our code – the concept of relative
XPath statements. We are familiar in general with the concept of absolute (e.g. /root/element/) and
relative (./element) paths. We use absolute paths when we want to refer to an element beginning at
the very start of our XML document and we use relative paths when we want to refer to an element
from a current context or set of elements.
The notion of relative paths come into play in our <xsl:for-each> loop because we want to be sure
that we leave no ambiguity for our XSL processor. Imagine if we used absolute XPath expressions
for the output statements that are children of our <xsl:for-each> loop. When the XSL processor got to
those elements it would go to the beginning of our XML document and output the first instance of that
elment (e.g. the title) that it found. This means that each time through the loop our XSL document
would output the same value.
By using a relative XPath expression for all elements that are children of our <xsl:for-each> loop we
are telling our XSL Processor to remember the current XPath expression that it is in and just look
further down the XML document tree. Using this method, our XSL processor will never get confused
with which node of our XML document it is in.
Pay attention to the use of absolute (e.g. starts with /) and relative (e.g. starts with .) in the following
code examples – the difference is significant!
With this information in hand (XPath expressions for <title>, <item> and relative expressions for book
title and link) and our understanding of <xsl:for-each> lets examine our XSL program:
Code example 4: XSL stylesheet with for-each element
24. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms">
25. <xsl:template match="/">
26. <html>
27.
<body>
28.
<h1>
Metadata Standards and Web Services
Erik Mitchell
Page 18
29.
<xsl:value-of select="/rss/channel/title"/>
30.
</h1>
31.
<ul>
32.
<xsl:for-each select="/rss/channel/item">
33.
<li>
34.
<xsl:value-of select="./title"/>
35.
</li>
36.
</xsl:for-each>
37.
</ul>
38.
</body>
39. </html>
40. </xsl:template>
41. </xsl:stylesheet>
Key Questions
Review the code above and answer the following questions. Lets pay attention to a specific section
of code (look below). This code executes the <xsl:for-each> process which loops through each
instance of the <item> element in our RSS file.
1. <xsl:for-each select="/rss/channel/item">
42.
<li>
43.
<xsl:value-of select="./dc:title"/>
44.
</li>
45.
</xsl:for-each>
Question 20.
Note how within the <xsl:for-each> element we define a generic process (e.g.
output an li element, output a link to a feed item, and close the li element). This will be
Metadata Standards and Web Services
Erik Mitchell
Page 19
repeated for every <item> element in the file. Take a moment to write out the HTML for the first
and second instance of <item> in your XML file based on the instructions in this set of code
Step 11: Let’s continue creating our XSL file. Create a new XSL file in your XML editor (see above
instructions)
Step 12: Code your XSL file to:
a. Output the name of the feed in an <h1> element
b. Go into each item element and output the title of the item element
c. Save the file
d. Hint: This is exactly what code example 4 does.
Step 13: Associate the XSL file with the xml document you downloaded by adding an xml-stylesheet
element into your XML file (<?xml-stylesheet type="text/xsl"
href="YOURXSLFILENAME.xsl"?>)
Step 14: Load the downloaded xml document in your webbrowser. What happened?
a. If you have issues getting the document to display in your web browser, use the
transformation process in Exchanger to transform the document and see any error
messages.
Advanced XSL processing – creating a useable web page
In this worksheet we have learned how to create and use XSL documents and have learned how to
use those documents to retrieve data from XML documents. Before we wrap up we will learn two
more skills in relation to XSL - advanced output methods and built-in functions. XSL is capable of
very advanced output including metadata transformation (e.g. changing text to uppercase and finding
and returning parts of text just to name a few examples). In this exercise we will learn how to create
html elements that require attributes (e.g. an <a> element). In the second part of our exercise we will
learn more about built-in functions (XSL has many) by learning to work with the xsl:sort function. With
these two features we will be able to create an XSL document that shows us the sorted titles from our
RSS feed along with a clickable link that we can use to connect to the full story. The code below is
Metadata Standards and Web Services
Erik Mitchell
Page 20
substantially the same as our above example with the exception of the addition of the <a> element
(see bolded code)
Advanced HTML output – clickable URLS
In order to create a clickable link from our RSS feed we need to combine both the title and the link
from our RSS item element stanza. Quickly refer back to your RSS feed to find these elements (they
are children of the item element).
Clickable links in HTML are created using the anchor or <a> element. An example is:
<a href=”http://ischool.umd.edu>Maryland’s iSchool</a>.
Lets spend a moment parsing this statement
<a:
Open the a element
href:
An attribute that stores the link that will be followed
“http://isc…” The value of the href attribute
Maryland’s… The value of the a element and also the text that will be displayed as the link text. Using
what we know so far about XSL we might try to create an XML document using the following
approach:
<a href=”<xsl:value-of select=”./link”/”><xsl:value-of select=”./title”/></a>
Key Questions
Question 21.
Try adding this code into your XSL document and check for well-formedness.
What error does Exchanger return?
Question 22.
What do you think this error means?
Metadata Standards and Web Services
Erik Mitchell
Page 21
In order to create these links dynamically from our RSS feed we need to familiarize ourselves with a
new xsl element called attribute (I know. . .stay with me  ).
The xsl element called attribute (<xsl:attribute>) is useful because it allows us to define attributes
while respecting the XML document encoding rules. Lets explore the following code in more detail.
1. <a><xsl:attribute name="href"><xsl:value-of select="./link"/></xsl:attribute><xsl:value-of
select="./title"/></a>
<a>
Open the anchor element
<xsl:attribute name=”href”>
Create an attribute that is a child of the <a> element with the
name href
<xsl:value-of select…/>
Assign the value of the <link> element from the XML
document to this attribute
</xsl:attribute>
Close the attribute
<xsl:value-of select=”./title”…>
Output the value of the title element as the value of the <a>
element.
</a>
Close the a element.
This can seem pretty confusing at first pass but as you can see, this syntax allows us to respect
element hierarchy and not create any confusion with our required double quotes.
Step 15: With that understanding under out belts, lets modify our XSL document and replace the line
that outputs title (e.g. <xsl:value-of select=”./title”/> with our new line (see line 12 in the
code below).
a. Save your XSL document and use it to transform your XML document. Did it work? If
not use Exchanger to debug the errors and keep going until it works!
Code example 5: Enhanced XSL file
2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms">
Metadata Standards and Web Services
Erik Mitchell
Page 22
2.
<xsl:template match="/">
3.
<html>
4.
<body>
5.
<h1>
6.
<xsl:value-of select="/rss/channel/title"/>
7.
</h1>
8.
<ul>
9.
<xsl:for-each select="/rss/channel/item">
10.
<xsl:sort select="./title"/>
11.
<li>
12.
<a><xsl:attribute name="href"><xsl:value-of select="./link"/></xsl:attribute><xsl:value-of
select="./title"/></a>
13.
</li>
14.
</xsl:for-each>
15.
</ul>
16.
</body>
17. </html>
18. </xsl:template>
19. </xsl:stylesheet>
In our code example above we also introduced the <xsl:sort> element. this element takes a single
attribute (select) which allows you to re-sort the contents of our XML document following whichever
element we specify. If we were to pass this element a date/time it would sort in date/time order. In
our example we pass the element in using the title as our XPath. This sorts our output in title order.
Step 16: Add the sort element to your XSL file. Notice that it is placed just below the <xsl:for-each>
control structure. Because of this we can use a relative path (e.g. ./) to our element.
Step 17: Save and transform your XML/XSL document. did sorting change?
Metadata Standards and Web Services
Erik Mitchell
Page 23
A final attribute for formatting
If you continue working with your XSL document and decide to output the description you may notice
some funky characters in the output. This is because the description of many RSS feeds contain
escaped HTML code (e.g. code that has been modified so that XML does not recognize it). NYTimes
and other RSS providers do this so that they can deliver nicely formatted HTML to RSS readers
rather than relying on the reader itself. In order to have your XSL processor re-transform this code to
useable HTML you need to tell the processor to “disable output escaping.” This is accomplished by
adding an attribute called disable-output-escaping to your xsl:value-of elements. You also want to
assign the value of disable-output-escaping to yes (e.g. disable-output-escaping=”yes”).
To try this out add the following line of code to your XSL document just below the title/link you created
<xsl:value-of select=”./description” disable-output-escaping=”yes”/>
Try toggling between the values yes and no in this attribute, what happens in your transformation?
Key Questions
Question 23.
What is the difference between relative and absolute XPath expressions?
Question 24.
What xsl element have we covered that serves as looping function?
Question 25.
Why do we need to use the <xsl:attribute> element to assign attributes to our
output HTML elements>?
Question 26.
What is the function of the <xsl:sort> element?
Metadata Standards and Web Services
Erik Mitchell
Page 24
Summary
This exercise has been our second foray into programming and software design. We became
conversant in some of the key functions of XSL but there is much more to learn. If you enjoyed this
exercise I recommend spending some time with the XSL tutorial at W3schools.com. It will introduce
more about programming concepts and XSL. In the coming weeks we will be exploring other
metadata services that utilize this functionality including our digital library application and metadata
harvesting services. Keep in mind how the metadata we will work with in those services is being
processed behind the scenes.
Metadata Standards and Web Services
Erik Mitchell
Page 25