XSL gives your
XML some style - JavaWorld June 2000
Tutorial Details:
XSL gives your XML some style
XSL gives your XML some style
By: By Michael Ball
Use XSL and servlets to style your XML data
ow, you've come so far. A year ago you didn't know what XML was, but now it's everywhere. You're storing XML in databases and using it in middleware, and now you want to present that data to a browser. That's where XSL can help. XSL can help you turn your XML into HTML. Moreover, servlets provide a powerful medium to perform those translations as they reside on the server and have access to all the features of server-side Java.
In this article, we'll cover the basics of XSL and XSL processors. If you don't know much about XML, you may want to first read Mark Johnson's excellent XML article, " Programming XML in Java, Part 1 ."
Our example will use a servlet to turn well-formed XML into HTML. If you need to learn more about servlets, please refer to Sun's servlets tutorial (see Resources ).
XML and XSL
The process of transforming and formatting information into a rendered result is called styling. Two recommendations from the W3C come together to make styling possible: XSL Transformations (XSLT), which allows for a reorganization of information, and XSL, which specifies the formatting of the information for rendering.
With those two technologies, when you put your XML and XSL stylesheet into an XSL processor, you don't just get a prettied up version of your XML. You get a result tree that can be expanded, modified, and rearranged.
An XSL processor takes a stylesheet consisting of a set of XSL commands and transforms it, using an input XML document. Let's take a look at a simple example.
Below we see a small piece of XML, describing an employee. It includes his name and title. Let's assume that we would like to present that in HTML.
Joe Shmo
Manager
If we wanted our HTML to look like this:
Joe Shmo: Manager
Then we could use a stylesheet, such as the one below, to generate the HTML above. The stylesheet could reside in a file or database entry:
:
Common XSL stylesheet commands
Stylesheets are defined by a set of XSL commands. They make up valid XML documents. Stylesheets use pattern matching to locate elements and attributes. There are also expressions that can be used to call extensions -- either Java objects or JavaScript. Let's look at some XSL commands.
Stylesheet declaration
The stylesheet declaration consists of a version and namespace. The namespace declares the prefix for the tags that will be used in the stylesheet and where the definition of those tags are located:
version="1.0">
.
.
.
If there are any extensions referenced, the namespace must be specified. For example, if you were going to use Java, you would specify this namespace:
xmlns:java="http://xml.apache.org/xslt/java"
Pattern matching
When selecting in a stylesheet, a pattern is used to denote which element or attribute we want to access. The syntax is simple: specify the node you want, using / to separate the elements.
Notice that in the sample XML code above we matched our template on / , which is the root node. We could have, however, matched on the employee node. Then a select statement could just refer to the name node instead of employee/name .
For example, if we had the following XML:
Joe Shmo
Manager
Attributes can also be selected. The employee id could be accessed by saying employee/@id . Groups of nodes can be accessed by using employee/* . A specific employee could be located using employee/@id='03432' .
Pattern matching allows us to select specific values out of our XML document. The Accessing elements versus attributes
Command
Result
Joe Shmo
03432
Templates
Templates provide a way to match nodes in an XML document and perform operations on them. The syntax for a template is:
.
.
.
The template is matched on a node name, then all the stylesheet commands in that template are applied. We can call templates in our stylesheet by using the apply-templates command:
An example using our employee XML above would be:
We can call this template anywhere there is a name node to be referenced, using this:
Logical commands
There are a few structures available for doing ifs and loops. Let's take a look at the syntax.
Choose command
The choose command provides a structure to test different situations.
stylesheet commands
stylesheet commands
The first successful test will result in that block's stylesheet commands executing. If all the tests fail, the otherwise block is executed. You may have as many when blocks as you want. The otherwise block must always be present; if you don't want to do anything in your otherwise block, just put:
If command
The if command provides only a single test and doesn't have any kind of else structure available. If you need to have an else, use the choose command.
...
Loops (for-each command)
Unlike most languages with for and while structures, XSL offers only the for-each command. As such, you can loop on a set of nodes or you can select the nodes you want to loop on, using a pattern match:
...
For example, if you had more than one employee in your XML document, and you wanted to loop through all the managers, you could use a statement such as this:
...
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:
assign value to count
here
The variable count can be accessed by using $count later in the stylesheet:
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:
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.
The void reset() method, to be used after process() , resets the processor to its original state.
The process() method is overloaded 18 times. Each signature provides a different way to process your XML and XSL. Some return an org.w3c.dom.Document object. I have found that the above process() method is the handiest; the documentation recommends its use because of the XSLTInputSource (used to read in XML or XSL) and XSLTResultTarget (used to write out the results) classes, which we examine next in turn.
Class com.lotus.xsl.
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: XSL gives your
XML some style - JavaWorld June 2000
View Tutorial: XSL gives your
XML some style - JavaWorld June 2000
Related
Tutorials:
XSLT blooms with
Java
XSLT blooms with
Java |
Boost Struts with
Boost Struts with XSLT and XML |
XML documents on
the run, Part 1
XML documents on
the run, Part 1 |
XML documents on
the run, Part 2
XML documents on
the run, Part 2 |
Take the sting out of SAX
Take the sting out of SAX |
Yes, you can secure your Web services documents, Part 1
Yes, you can secure your Web services documents, Part 1 |
Transform data into Web applications with Cocoon
Transform data into Web applications with Cocoon |
XML glossary
XML glossary |
Maven ties together tools for better code management
Maven ties together tools for better code management |
Sun boosts
Sun boosts enterprise Java |
Publish and find UDDI tModels with JAXR and
WSDL
Publish and find UDDI tModels with JAXR and
WSDL |
Transparently cache XSL transformations with
JAXP
Transparently cache XSL transformations with
JAXP |
SAAJ: No strings attached
SAAJ: No strings attached |
FastParser 1.6.3
FastParser 1.6.9.1
XML Edition
FastParser is a Java Xml parser
High performance XML parser (benchmarks* : up to +100% faster compared to Xerces and JDK1.4 integrated parser)
SAX Level 1 and 2 compliant
DOM support
JAXP compatibility
Names |
Jython
Get to know Jython, in this first article in a new series introducing alternate languages for the Java Runtime Environment, alt.lang.jre. Jython is an implementation of the popular scripting language Python, but running on a JVM. For Python developers Jyt |
Introduction to JSP
Introduction to JSP
Introduction to JSP
Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the |
Java Resources
There are all Java freebies. Some of these are old, and not under maintenance. Download and use them at your risk. In case of queries, mail subrahmanyam_avb@technologist.com or varalakshmi_a@techie.com. |
FOP is the world's first print formatter driven by XSL formatting objects.
It is a Java application that reads a formatting object tree and then turns it into a PDF document. The formatting object tree, can be in the form of an XML document (output by an XSLT engine like XT or Xalan) or can be passed in memory as a DOM Document |
What is Web Hosting
What is Web Hosting
What is Web Hosting?
What is Web Hosting?
If you have a company and want web presence than you need a website. With the website any one from the world must be able to view your pages, images etc.
Website is actually a |
Chat Transcript: Java Web Services Developer Pack (Java WSDP) 1.5
Learn about the exciting new web services features in the recently-released Java WSDP 1.5. |
|
|
|