Presents your
XML E-NEWSLETTER for August 21, 2002
<------------------------------------------->
CONTROL XSL TEMPLATES WITH PARAMETERIZED SORTS
XSL templates are often used to drive the translation from XML data
sources to HTML-based Web browsers. When the information is displayed
in HTML
tables, many applications provide users with the ability to sort on
each
column. Our approach to this problem is to control the sort using a
couple of XSL parameters. Let's examine the details of this problem and
our
solution.
STARTING WITH THE DATA
It's easier to understand the problem if we look at a sample XML
document that contains data that we want to display as an HTML page.
Listing 1
shows a simple order (which contains a handful of items) as an XML
document.
Listing 1: order8876.xml
9900234
-
1234
5.95
100
595.00
Super Widget Clamp
-
6234
22.00
10
220.00
Mighty Foobar Flange
-
9982
2.50
1000
2500.00
Deluxe Doohickie
-
3256
389.00
1
389.00
Muckalucket Bucket
1111
3704.00
07/07/2002
8876
What we want to end up with is an HTML document that looks like Listing
2. More than the HTML, we want to be able to specify how to sort the
items based on different columns. In other words, we want to specify
whether
to sort by SKU or Quantity, and to determine whether the sort is
ascending or descending.
Listing 2: order.html
Order View
Order View
Order Information
Order Number: 8876
Account Number: 9900234
Order Date: 07/07/2002
| SKU | Description | Price | Quantity |
Subtotal |
| 1234 | Super Widget
Clamp | 5.95 | 100 | 595.00 |
| 6234 | Mighty Foobar
Flange | 22.00 | 10 | 220.00 |
| 9982 | Deluxe
Doohickie | 2.50 | 1000 | 2500.00 |
| 3256 | Muckalucket
Bucket | 389.00 | 1 | 389.00 |
Number of Items: 1111
Order total: 3704.00
SORTING WITH PARAMETERS
The basic XSL template to get from our XML data to our HTML page is
reasonably straightforward if you understand the basic concepts of XSL
templates. There is simply a template for the root document, one for
the Order
element, and one for the Item elements.
To add a sort for our items, we'll apply the element to the
element used to display the Item elements. The
basic sort command looks like this:
This command will sort the items by their SKU values in the default
ascending order. But providing a parameter to this command is a little
tricky. The above code snippet would be used within the template for
the Order
element. Passing a parameter to the Order template from the root
template
is fairly trivial. You simply define the parameter in the root element,
pass it to the Order template, and the order template defines the
parameter.
SKU
descending
As you can see, we have passed the parameter to our Order template, but
have yet to pass it to our sort command. The first instinct is that we
can just specify the parameter value within the sort's select
attribute,
like this:
Unfortunately, this doesn't yield the expected result. The reason has
to
do with the way the parser works; specifically, with how it handles the
scoping of XSL parameters and variables within select attributes. To
make
this work as we want, we'll have to use a slight workaround that looks
like this:
In this case, we have explicitly selected the parameter with the name
sortcolumn. Now, the problem becomes a little stranger when we decide
to
augment this snippet with our sortorder parameter. Again, our instinct
would be to try this:
However, in this case, we're wrong again because the same issue that
caused the failure earlier actually works in reverse for the order
attribute. Therefore, the solution to this problem is actually a
combination such
as this:
THE COMPLETE SOLUTION
Now, if we add together all of what we've learned so far, we can create
a complete solution. We'll need to start by specifying our parameters
in
the root template. The parameters will describe the element we want to
sort by and the order of the sort. Then we'll pass these parameters to
the
Order template, which will define the parameters and use them in
specifying the sort attributes for the Item template. Listing 3 shows
the
complete solution, which yields the expected results.
Listing 3: order.xsl
Order View
Order View
Order Information
SKU
descending
Order Number:
Account Number:
Order Date:
| SKU |
Description |
Price |
Quantity |
Subtotal |
Number of Items:
Order total:
|
|
|
|
|
Brian Schaffner is a senior consultant for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Telcom360
group.
----------------------------------------