Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer

Part 7 -- Developing XSL Stylesheets using the XSL Editor and XSL Debugger

Christina Lau (clau@ca.ibm.com), Senior Technical Staff Member, IBM
Christina Lau is a Senior Technical Staff Member at IBM. She currently leads the XML and Data Tools effort for WebSphere Studio Application Developer. She can be reached at clau@ca.ibm.com.

Summary:  This is Part 7 of a series that focuses on the XML tools provided with WebSphere Studio Application Developer. Part 7 demonstrates how to use some of the new XSL tools available in Application Developer Version 5.0 to develop XSL stylesheets.

Date:  16 Oct 2002
Level:  Introductory

Activity:  2120 views
Comments:  

Introduction

IBM ® WebSphere® Studio Application Developer is an application development environment that supports the building of a large spectrum of applications using different technologies, such as JSPs, servlets, HTML, XML, Web services, databases, and EJBs. In particular, Application Developer provides tight integration between XML and relational data. Application Developer supports all of the databases that WebSphere Application Server supports, including DB2®, Oracle Sybase and Microsoft® SQL ServerTM.

This is Part 7 of a series of articles focusing on the XML tools provided with WebSphere Studio Application Developer. Part 7 demonstrates how to use some of the new XSL tools available in WebSphere Studio Application Developer Version 5.0 to develop your XSL stylesheets.

It is recommended that you read the other articles in this series:

  • Part 1 Learn how to use Application Developer to develop XML Schema.
  • Part 2 Learn how to create an SQL query using Application Developer's SQL Builder.
  • Part 3 Learn about the Application Developer features available to incorporate data access and XML in your application.
  • Part 4 Learn how to use the XML Editor, a visual tool for creating and editing XML documents.
  • Part 5 Learn how to use the RDB to XML Mapping Editor to create DAD files for use with DB2 XML Extender.
  • Part 6 Learn how to use the XML Schema Editor and the XML Editor together to develop XML applications that make use of XML namespaces.

Create a simple stylesheet

We will create a new XSL stylesheet from scratch. Before you start, download the xslExample1.zip file provided below. Switch to the XML perspective. Create a new project called Simple, and import the videos.xml file from the xslExample1.zip file into the Simple project. Launch the New XSL file wizard as follows:

  1. Select File => New => XSL to launch the New XSL wizard.
  2. In the File name field, enter videos.xsl. Click Next.
  3. In the Select XML file page, select the videos.xml file that you have imported. This associates the videos.xml file with the videos.xsl file.
  4. Click Finish. The videos.xsl file is created and the XSL Editor is automatically opened for you.

Create the stylesheet

The XSL Editor provides a number of wizards to help you create the content in your stylesheet. It also provides one-click transformation to let you quickly see the results of a transform. In this section, we will show how you can create a simple XSL stylesheet to format your videos.xml data into an HTML table in three simple steps:

Step 1: Add an HTML template header

To create an HTML document, we will add a template to generate HTML header information as well as to define the output method for the document.

  1. Position the cursor on an empty line after the <xsl:stylesheet> element in the videos.xsl file.
  2. From the menu, select XSL => HTML Template. This will create an <xsl:output> element that will output the result in HTML, and a template that will emit an HTML header with an <xsl:apply-templates> rule to process all of the immediate children in the videos.xml file.

Step 2: Create an HTML table

Next, we will create a template that will produce an HTML table.

  1. Position the cursor on an empty line after the </xsl:template> element that was generated in Step 1 above.
  2. From the menu, select XSL => HTML Table. This launches the XSL Table Wizard as shown in Figure 1 below.
  3. Select the VIDEO element as the context node for building the HTML table. This implies that all of the children of the VIDEO element will be added as columns in the table.
  4. Select the Wrap table in a template check box to indicate that we want to wrap this table in a new template.
  5. Select the Include Header check box to indicate that we want to include a header in this table.
  6. Optionally, click Next to go to the next page to add a table border and background color for the table.
  7. Click Finish. This will create a new VIDEOS template in your videos.xsl file that will produce an HTML table.

Figure 1. XSL Table Wizard
Screen capture of the XSL Table Wizard

Step 3: Save and transform

  1. Select File => Save to save the videos.xsl file. XSL Validation will be run on the videos.xsl file. The completed videos.xsl file is included in the xslExample1.zip file.
  2. From the menu, select Debug => Transform. This will invoke the Xalan processor to transform the XML file (videos.xml) that is associated with this XSL file. The resulting HTML file is shown in Figure 2 below.

Figure 2. Simple HTML table for videos.xml
Screen capture of a simple HTML table for videos.xml

Associate a stylesheet with multiple XML documents

When we create the videos.xsl file, we associate it with the videos.xml file. You can change this association in the Associations dialog. Select the videos.xsl file, and choose Properties. This launches the Properties page for the videos.xsl file. Click the Associations tab as shown in Figure 3 to launch the Associations dialog. You can see that the videos.xml file is currently associated.


Figure 3. Associating XML documents with an XSL stylesheet
Screen capture of the Properties dialog for videos.xsl

Using the Associations dialog, you can add or remove associations, as well as set the default association. If an XML file is set as the default association of an XSL file, any processing of an XSL file that involves the knowledge of an instance file (for example, transformation, debug, or XPath wizard invocation) will automatically use the default association for the processing.

It is quite common to have one XSL stylesheet that is applied to a number of XML documents with the same structure but different content. You can use the Associations dialog to set up all of the associations. When you make a change to the XSL stylesheet, you can select the Transform all XMLs action to transform all of the associated XML files with only one single mouse click.


Figure 4. Transform all XMLs
Screen capture of the drop-down menu displaying the Transform all XMLs commands

Improve the design of the video store

So far, I have demonstrated how you can use the wizards in the XSL Editor to quickly create a stylesheet from an XML document. However, for a real video store, the HTML table does not look very attractive. For example, would you rather see the real image of the video? A more appealing design might look like Figure 5 below. When you visit the video store, a list of the most popular movies is presented. You can then select a particular movie by clicking on the link; an image, together with a detail description, will be displayed as shown in Figure 6 below.


Figure 5. List of movies
Screen capture of the Web browser displaying a list of the Most Popular Movies

Figure 6. A selected movie
Screen capture of the Web browser displaying a selected movie

Such a Web site can use the same videos.xml as its data source. However, to create this Web site using XSL stylesheets requires the following:

  • Create stylesheets that can handle the hyperlinks (hrefs).
  • Design and deploy the Web application that will transform the XSLs into HTML at run time.

In the remaining sections of this article, we will concentrate on the first task. We will examine the XSL Debugger that can be used for debugging your XSL stylesheets. In the next part of this series, we will discuss how to build your Web application that will apply these stylesheets to the input data to transform them into the HTML output in Figure 5 and Figure 6 above.


Debug XSL stylesheets

Create a new project called Scenario, and import all of the files in the Scenario directory from xslExample1.zip into this project. You will notice that in the xsl folder, there are two XSL stylesheets: movieList.xsl and movieDetail.xsl. The movieList.xsl stylesheet is used to present the list of movies. The moveDetail.xsl stylesheet is used to show the details of a particular movie.

To figure out how movieList.xsl works, we will examine it using the XSL Debugger. Select both the movieList.xsl and the videos.xml files. Select Apply XSL =>As HTML. This launches the XSL Debugger in the XSL Debug perspective, as shown in Figure 7 below:


Figure 7. XSL Debugger
Screen capture of the XSL Debugger

The XSL Debugger has the following views:

  • Sessions View Shows the list of debug sessions. You can use the toolbar buttons from this view to step forwardThe Step Forward icon in the Sessions view of the XSL Debugger or backwardThe Step Backward icon in the Sessions view of the XSL Debugger on the current transformation, run to a breakpointThe Run to a Breakpoint icon in the Sessions view of the XSL Debugger if breakpoints are set on the XSL source, or open the browserThe Open the Browser icon in the Sessions view of the XSL Debugger on the result document.

  • Current XSL Element View Shows the details of an element in the XSL stylesheet as you are stepping through the transform. The Actual Value column displays the actual value of the XSL element as it is being evaluated.

  • Breakpoints View Shows the list of breakpoints that are set on the XSL stylesheet. You can use this view to remove breakpoints.

  • Template Call Stack View Shows the list of templates that are in the call stack. As a template is entered, an entry will be added. When a template completes its execution, it is removed from the stack.

The XSL Debugger also opens the input XML and XSL in a tile editor so that the XML and XSL can be positioned side by side for easy visualization when you step through the transform. However, if your XSL stylesheet includes other stylesheets, or if your XSL stylesheet transforms multiple documents, a tile editor could be hard to read and you might want to simply stack the editors instead. You can change this by selecting Windows => Preference => XML => XSL Debugging, and deselecting the check box for showing all debugging files in a tile editor, as shown in Figure 8 below:


Figure 8. XSL Debugging Preference
Screen capture of the XSL Debugging Preference dialog

Examine the movieList.xsl file with the XSL Debugger

In the Session View, click on the The Step Forward icon in the Sessions view of the XSL Debugger button to start stepping through the transform. The XSL rule that is being executed is highlighted in the XSL source editor. Continue stepping forward until you reach this line that defines the variable, id.

<xsl:variable name="id" select="VID_ID"/>

The XSL Debugger evaluates a variable when an end tag is encountered. In this simple case, the variable ends in the same line, but for more complicated variable declarations, the value of a variable can involve calling another template that can span multiple lines. Examine the Current XSL Element View to see the evaluated result when the </xsl:variable> tag is displayed. You will see the context node is evaluated to be 100, which is the value of the VID_ID element for the current iteration.

Continue to step forward until you reach the line that defines the href link attribute as follows:

<a href="/VideoWeb/main/detail?videoId={$id}">

This href attribute defines the link to the page when the link is clicked. So, what is in this href? The first part of this href defines a URL /VideoWeb/main/detail that has meaning when running in a Web application. We will revisit this part in the next article in this series. The second part of this href defines a parameter videoId whose value is computed from the value of the {$id} variable. To find out the exact value of this href when a transformation is applied against the videos.xml file, examine it in the Current XSL Element View as shown in Figure 9 below. Note that you can use the ... button to bring up a dialog if the actual value is very long.


Figure 9. Evaluating the href attribute
Screen capture showing details of the href attribute

You can either single step through the <xsl:for-each> loop to examine each XSL element as they are being executed, or you can set a breakpoint if you are only interested in examining certain values. Let's set a breakpoint on the <a href> element by double-clicking on that line. You will notice a breakpoint (a green circle) being added as shown in Figure 7 on line 28.

Now click on the The Run to a Breakpoint icon in the Sessions view of the XSL Debugger button to run to the breakpoint. Notice that at each click, the Current XSL Element View shows a new href attribute value corresponding to the VID_ID element that is in the videos.xml file.

Examine the movieDetail.xsl file with the XSL Debugger

Next, we will review the movieDetail.xsl file using the XSL Debugger. Select both the movieDetail.xsl and the videos.xml files, and select Apply XSL => As HTML to start a new debug session.

Recall that the movieDetail.xsl file is designed to show the details of a selected movie. An identifier for the movie, videoId, must be passed to this stylesheet. In the next article in this series, we will show you how to dynamically pass in such a parameter from a servlet when a URL is requested. As you will see, for the purposes of designing this stylesheet, you can use a scaffolding value instead.

In the XSL source editor, set a breakpoint on the following line which tests the VID_ID value against the variable $videoId.

<xsl:if test="VID_ID=$videoId">

In the Session View, click on the The Run to a Breakpoint icon in the Sessions view of the XSL Debugger button to run to the breakpoint. In the Current XSL Element View, notice that <xsl:if test> is evaluated to be true. This is because this variable is set to the value 100 in this line, and it matches the first element value from the vidoes.xml file.

<xsl:variable name="videoId" select="100"/>

If you click on the The Run to a Breakpoint icon in the Sessions view of the XSL Debugger button again, the <xsl:if test> element will return false since the next VID_ID value does not match the current value of 100.

You can make changes to the XSL or XML source and relaunch the XSL Debugger to see the immediate result. Let's change the videoId value to 103 as follows, and save the file.

<xsl:variable name="videoId" select="103"></xsl:variable>

In the Session View, select the current session and launch the pop-up menu (see Figure 10). Select Relaunch to start a new transform. Click on the The Open the Browser icon in the Sessions view of the XSL Debugger button to open the browser on the new transform. You will see a different movie description.


Figure 10. Relaunch a new debug session
Screen capture showing the Relaunch option selected in the pop-up menu

Conclusion

This article described how to use some of the new XSL tools in WebSphere Studio Application Developer Version 5.0. These tools can help beginners get started quickly with XSL, and help enhance the productivity of expert XSL programmers.

Other XSL-related tools in WebSphere Studio Application Developer 5.0 that are not described here include:

  • An XSL from XHTML wizard. It generates XSL stylesheets from an XHTML file. Optionally, it can also extract data into XML format if you want to migrate existing HTML files into an XSL and XML infrastructure.
  • A Java bean XML/XSL Web client wizard. It takes a Java bean and generates a DOM, the associated XSL stylesheets that can be applied to the DOM, and a servlet that applies the XSL stylesheets to the DOM so that you can quickly create the front-end of a Web application using XML and XSL.
  • Compiled XSL stylesheets support using XSLTC.
  • An XPath wizard that helps you build and test XPath expressions.

In the next article in this series, we will look at developing and deploying a Web application that will make use of the XSL stylesheets that we created in this article.



Download

NameSizeDownload method
xslExample1.zip46 KBFTP|HTTP

Information about download methods


About the author

Christina Lau

Christina Lau is a Senior Technical Staff Member at IBM. She currently leads the XML and Data Tools effort for WebSphere Studio Application Developer. She can be reached at clau@ca.ibm.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=14390
ArticleTitle=IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer
publish-date=10162002
author1-email=clau@ca.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers