Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XSL gives your XML some style

Use XSL and servlets to style your XML data

  • Print
  • Feedback

Page 3 of 7

<xsl:for-each select="employee[title='Manager']">
...
</xsl:for-each>


Variables

The variable command provides a way to set a variable and access it later. The extension mechanism uses variables to store the values retrieved from extensions:

<xsl:variable name="count">assign value to count 
here</xsl:variable>


The variable count can be accessed by using $count later in the stylesheet:

<xsl:value-of select="$count"/>


Parameters

You can pass parameters to your stylesheet, using the param tag. You can also specify a default value in a select statement. The default is a string, so it must be in single quotes:

<xsl:param name="param1" select="'default value'"/>


You can set the parameters you are passing to your stylesheet on your XSLProcessor object:

processor.setStylesheetParam("param1", processor.createXString("value"));

Extensions

Extensions add functionality to a stylesheet. XSL comes with some basic functions:

  • sum() -- Sum the values in designated nodes
  • count() -- Count the nodes
  • position() -- Returns the position of the current node in a loop
  • last() -- Test whether this is the last node; this function returns a boolean value


If you want additional functionality, you need to use extensions. Extensions can be called anywhere a value can be selected. Extensions to a stylesheet can be written in Java or JavaScript, among other languages. We'll concentrate on Java extensions in this article.

In order to call extensions in Java, the java namespace must be specified in your stylesheet declaration:

xmlns:java="http://xml.apache.org/xslt/java"


Any calls to Java extensions would be prefaced with java:. (Note: You don't have to call your namespace java; you can call it whatever you want.)

You can do three things with Java extensions: create instances of classes, call methods on those classes, or just call static methods on classes. Table 2 shows syntax that can be used to reference Java objects.

Instantiate a class: prefix:class.new (args) Example: variable myVector select"java:java.util.Vector.new()"

Call a static method: prefix:class.methodName (args) Example: variable myString select="java:java.lang.String.valueOf(@quantity))"

Call a method on an object: prefix:methodName (object, args) Example: variable myAdd select="java:addElement($myVector, string(@id))" Table 2. Three ways to use Java objects

(For more on XSL, see Elliotte Harold's The XML Bible in Resources.)

The XSL Processor API

For our example, we will use Lotus' implementation of Apache's XSL processor Xalan (see Resources). We'll use the following classes in our servlet example:

Class com.lotus.xsl.XSLProcessor

com.lotus.xsl.XSLProcessor is the processor that implements the functionality defined in org.apache.xalan.xslt.XSLTProcessor. The default constructor can be used and processing can take place using the process() method, as seen below:

void process(XSLTInputSource inputSource, XSLTInputSource stylesheetSource, XSLTResultTarget outputTarget)


The process() method transforms the source tree to the output in the given result tree target.

  • Print
  • Feedback

Resources