Spicing Up Your Web Services with XSLT

In the
first thirteen
parts of this series, I’ve introduced you to some of the many features
available within the IBM Data Studio integrated
development environment (IDE) that’s available for use with the IBM data servers. Specifically, I’ve shown you how to set
up and use database connection objects, how to generate an overview diagram of
your database architecture, how to build OLE DB functions that can be used to
easily integrate data from external data sources that have an OLE DB provider,
how to create an SQL statement using either the SQL Builder or the SQL Editor
in IBM Data Studio, and how to take an
SQL statement and quickly turn it into a stored procedure. I’ve also shown you
how to wrap both an SQL statement and a stored procedure as a Web service. Most
recently, I showed you how to test your Web service using the Web Services
Explorer that’s integrated into IBM
Data Studio or through a Web browser using the Web Services Explorer.

In this
article, I’m going to show you how to apply Extensible Stylesheet Language
Transformations (XSLT) to your Web services. The output of the Web service you
created earlier in this series was XML. For example, when executing the FEMALEPERSONNEL
Web service from a representational state transfer (REST) interface in a Web
browser, the output looked like this:

As you
can see, business logic was executed through a URL-based invocation of a Web
service, but the output is just plain XML. XML isn’t a presentation language;
rather it’s a data semantic language. Using XSLT, you can transform XML into
all sorts of different output formats. For example, perhaps you want the output
format of this data to be a Really Simple Syndication (RSS) feed, hypertext
markup language (HTML)Braille text, or practically anything. In this
article, we’re going to create a new Web service and transform the output such
that it displays as a nicely formatted HTML Web page using XSLT.

Getting ready for this article

I assume in this article that you completed all the steps in
12: Testing your Web Service using the Web Services Explorer”
. From there,
all you need to do in order to follow the steps in this article is ensure that
the application server you defined and deployed your Web services to in Part
is started and running such that the Servers tab looks like

the Servers tab

I also assume
that your Data Project Explorer view looks similar to this:

Data Project Explorer view

I assume you have access to the Mozilla Firefox Internet browser.

What is Extensible Stylesheet Language Transformation?

Stylesheet Language Transformation (XSLT) is an XML-based technology used to
transform the structure of XML documents. In fact, the term stylesheet is
part of the XSLT name because of its ability to change the content of the XML
document – think cascading stylesheets (CSS)
with HTML and you get the point.

designed for use as part of the Extensible Stylesheet Language (XSL). XML Path
Language (XPath) is an XSL sub-language that is designed to be used with XSLT.
It is used for identifying or addressing parts of a source XML document. Every
node within an XML document can be uniquely identified and located using the
syntax defined for XPath.

One of
the coolest things about XSLT is the fact that you can shape your XML into
almost any output you want. For example, perhaps an XML response envelope from
your Web service returns some XML data. Using XSLT, you could transform this
data for output on a Web page in HTML. Another example of the use of XSLT is to
screen-scrape an XHTML page and distribute that content to an RSS or Atom
Syndication Format (ASF) feed.

When you
configure XSL transformations for your Web services using a REST GET invocation, the message flow for a Web service
operation follows these steps:

A client
application sends an HTTP GET (TEXT/XML) message that accesses an operation in
a Web service. The message is tagged according to a custom XML schema.

The message is
transformed so that it is tagged according to the default XML schema.

The Web
service receives the message and passes to the database the SQL statement or
stored procedure call that is in the operation.

The Web
service receives the response from the database and packages the response in an
XML message that is tagged according to the default XML schema for the

The message is
transformed so that it is either in XML (and is tagged according to the custom
XML schema), or in a non-XML format, such as HTML or plain text.

The Web
service sends the response to the client application.

process is shown in the following figure:

configure XSL transformations for your Web services using a REST GET invocation

As you
can see in the previous figure, IBM
Data Web Services gives you options to apply XSLT to incoming XML messages and
outgoing XML messages. (In this article, the example shows you how to apply an
XSLT transformation to the outbound XML message.)

In this
series, I’m illustrating how to build Web services using a bottom-up approach
where the business logic (in this case, an SQL statement or a stored procedure)
has been built and the intent is to expose this logic as a Web service. In a
top-down approach, you start with a service specification (for example, a WSDL
file) and then implement the underlying code to match that specification. By
applying XSLT to input service messages, in many cases, you can map the service
format of the bottom-up style messages to the top-down design. This alleviates
some of the top-down service format requirements while allowing you to develop
bottom-up Web services; of course, this is all done with XSLT, only in this
case on an input message.

on how you build your Web service (for example, a SOAP service), this process varies
somewhat; refer to the IBM Data Studio Information Center or integrated help for more

Here are
some excerpts of the XSLT document that you will be using in this example:

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet xmlns_xsl=”http://www.w3.org/1999/XSL/Transform”

<xsl:output method=”html” version=”1″ encoding=”UTF-8″ omit-xml-declaration=”yes” standalone=”yes” indent=”yes” />

As you
can see, the excerpts of this XSLT document show you that XSLT documents are indeed
written in XML. You can also see that this XSLT document will transform the XML
into HTML, as indicated by the xsl:output element where the method attribute is set to "html". Perhaps you are transforming an
XML document into an RSS feed for which you would define an output clause
similar to this: <xsl:output
method="xml" encoding="UTF-8"

<xsl:template match=”/”>
<h2>Employee Record</h2>
<xsl:element name=”form”>
<xsl:attribute name=”action”>http://localhost:8080/DemoMyWebService/rest/Demo/updateEmp</xsl:attribute>
<xsl:attribute name=”method”>POST</xsl:attribute>
<table border=”1″>
<th rowspan=”12″>
<xsl:attribute name=”src”>data:image/gif;base64,<xsl:value-of select=”//row/PICTURE/text()”/></xsl:attribute>

As you
further inspect the sample XSLT document used in this article, you can see how
the previous code shapes the input XML data from our Web service into an HTML
output. The following code tells an XSLT processor how to take the <EMPNO> XML element and transform it into

<xsl:value-of select=”//row/EMPNO/text()”/>
<xsl:element name=”input”>
<xsl:attribute name=”value”>
<xsl:value-of select=”//row/EMPNO/text()”/></xsl:attribute>
<xsl:attribute name=”type”>hidden</xsl:attribute>
<xsl:attribute name=”name”>empno</xsl:attribute>

You can
see how XML technologies are used with XSLT by noticing the XPath notation of
the textual value of the <EMPNO>: //row/EMPNO/text().

course, all of the transformed data will be displayed in table format because
all of the transformation logic resides within the following tags:

<table border="1">…</table>

outside the scope of this article to delve into the depths of transformations
with XML data; however, the W3 Schools Web site has some terrific resources on
this (and many other) XML-related technologies. Check out http://www.w3schools.com/xsl/ for more

Paul Zikopoulos
Paul Zikopoulos
Paul C. Zikopoulos, BA, MBA is the Program Director for the DB2 Evangelist team at IBM. He is an award-winning writer and speaker with more than 14 years of experience with DB2. Paul has written more than 230 magazine articles and 11 books on DB2 including, Information on Demand: Introduction to DB2 9.5 New Features, DB2 9 Database Administration Certification Guide and Reference (6th Edition), DB2 9: New Features, Information on Demand: Introduction to DB2 9 New Features, Off to the Races with Apache Derby, DB2 Version 8: The Official Guide, DB2: The Complete Reference, DB2 Fundamentals Certification for Dummies, DB2 for Dummies, and A DBA's Guide to Databases on Linux. Paul is a DB2 Certified Advanced Technical Expert (DRDA and Clusters) and a DB2 Certified Solutions Expert (BI and DBA). In his spare time, he enjoys all sorts of sporting activities, including running with his dog Chachi, avoiding punches in his MMA training, and trying to figure out the world according to Chloë - his daughter.

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles