Documente Academic
Documente Profesional
Documente Cultură
I am currently working on SOA Appliance named “IBM Data Power”, which is a very powerful and
standards based appliance for developing service oriented integrations without compromising on
performance and standards.
Being a hardware enterprise bus, it is meant to process XML at very high speeds. For XML processing it
uses XSLT (Extensible Stylesheet Language Transformations), also support EXSLT. EXSLT is an extension
to standard XSLT with few additional and interesting capabilities. There are lot many commercial editors
available to support XSLT, whereas AFAIK only Stylus Studio XML editor supports EXSLT. In this article we
are going to use Eclipse Galileo capabilities for developing and executing XSLT and EXSLT, along with
some examples.
To start with you need to go through the following tutorial of basic XSLT and XPath.
In the new project wizard select General -> Project and click “Next” and provide a project name as
“XSLT” or any other valid name.
Change your eclipse perspective to XML using the files tool bar -> Window -> Open Perspective ->
The below screen shot shows the exact XML perspective with various windows highlighted:
XML Outline
XPath Evaluator
XSL Templates
Let’s use the Books.xml specified at W3C examples and to do that we need to create a new XML
document in our XSLT project of eclipse. In this article we will be doing a set of simple XSLT
transformation on this XML.
To do this right click on the project -> New -> XML and type filename as books.xml and click finish.
Eclipse displays the opened XML in two views namely design and source view, switch to source view and
copy paste the content of books.xml specified at W3C examples.
XSLT stands for Extensible Stylesheet Language Transformations, which is a standard language for
transforming XML documents into other XML documents from W3C.
Now let’s try applying this XSL transformation on “books.xml”, in order to do that right click on the XSL
file and select Run As-> XSL Transformation
Select the input file by selecting “workspace” and select XSLT followed by “books.xml”, click ok.
We see a new file getting generated named “books.out.xml”, which is the resultant xml got by applying
“books.xsl” transfomation on “books.xml”, as you can see this output xml only has one line(xml
processing intruction), as we have not written how exactly the books.xml needs to be processed.
Now let’s do a simple XSL transformation, let’s just copy the input to the output as is, in order to do that
all we need is to just add a single line 9<xsl:copy-of select="/"></xsl:copy-of>) to the
existing XSL.
Save the updated XSL file and apply it on the “books.xml” as we did earlier:
The resultant XML will be complete copy of the input as shown below.
We can format the “books.out.xml” by right clicking on the file and selecting source->format or a
keyboard short cut using “ctrl+shift+F”.
Now let’s make some useful transformations, we will build an html page to display all the books title and
author(s) information from the books.xml.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Books</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author(s)</th>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Save the updated WebBooks.xsl and run the transformation you will find the result as shown below, if
you open this in a web browser after renaming the file you will see the screen as shown below:
<html>
<body>
<h2>My Books</h2>
<table border="1">
<tr>
<th>Title</th><th>Author(s)</th>
</tr>
</table>
</body>
</html>
Step 2:
Now let’s display the information by parsing the xml source, to do that all we need to do is:
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Save the updated WebBooks.xsl and run the transformation, you open this result in a web browser after
renaming the file you will see the screen as shown below:
It’s almost close to what we expected but if you observe carefully you will find that row 3 “XQuery Kick
Start” book has 4 authors where as our generated html is only showing one of them, in order to fix this
lets go to step 3.
Step 3:
Now let’s display the expected result:
As there are multiple authors we need loop through all the authors for a given book and lets append a
comma to each author, this needs an additional check to make sure that we are not adding “,” when
there is an single element or when it’s the last author in the list. In order to do this we need add
additional loop and check for the last elements position before appending the “,”.
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Now that we have seen how to use XSLT in eclipse, let’s see how eclipse supports EXSLT, all this time
when we ran the XSL Transformation on the default JRE Instance XSL Processor. This processor doesn’t
support the EXSLT capabilities in order use EXSLT; we need to set the processor to Xalan, which is an
open source XSLT Processor supporting EXSLT.
To do that click “Window” in eclipse file menu, and select “preferences” under XML->XSL->Java
Processors, you need to uncheck the default JRE Instance XSL Processor or check the Xalan Processor as
shown below and click ok.
Now we are ready to execute any XSL with EXSLT in it. Let’s do a simple EXSLT example as part of this
article but we will surely do an advanced EXSLT XSl in the next article which will be about masking the
sensitive data in incoming messages.
Lot many extensions to XSLT are defined as part of EXSLT, as part of this article lets see how to write a
function which generates a random number with in 100 using EXSLT.
Create a New XSL file named “genRandom.xsl” with the following code:
This is just a basic EXSLT example I have demonstrated, I needed the above tools for developing my Data
Power requirements, will explain few of the exact requirements which were solved using EXSLT in the
next article.
EXSLT supports recursive functions, isn’t that interesting? Take a look at the following example at
EXSLT.org: http://www.exslt.org/func/elements/function/func.function.4.xsl
I conclude this article stating that Eclipse is one (only?) of the best IDE for XML technologies
development with features like code complete for EXSLT also.
The XSLT files used for this article are available for download @ My Ghost
Drive.
Blog: http://soa2world.blogspot.com/
Disclaimer: Author is not responsible for any unintended use of the above article or damages caused by
using the technologies mentioned in the article (if any).