Sunteți pe pagina 1din 10

Best Open Source

XSL & EXSLT


Editor-Eclipse

Knowledge Level: Beginner-Intermediate

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.

XSL Tutorial: http://www.w3schools.com/xsl/

XPath Tutorial: http://www.w3schools.com/XPath/default.asp

EXSLT How To: http://www.exslt.org/howto.html

It may also require some knowledge of XSLT in Eclipse.


Start the eclipse, and in your workspace, just create a general project:

In the new project wizard select General -> Project and click “Next” and provide a project name as
“XSLT” or any other valid name.

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 ->

Other … -> Select “XML”.

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.

To start using Eclipse of XSLT development, let’s


create a XSLT file in eclipse.

As shown right click on the XSLT Project and select


XSL.

Enter “WebBooks.xsl” as file name in the XSL


stylesheet wizard and click finish. It will use “basic
template” by default.
The resulting XSL will look as shown below:

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<!-- TODO: Auto-generated template -->
</xsl:template>
</xsl:stylesheet>

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.

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<!-- TODO: Auto-generated template -->
<xsl:copy-of select="/"></xsl:copy-of>
</xsl:template>
</xsl:stylesheet>

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.

Step 1: build basic XSL with expected html structure.

Step 2: Display information

Step 3: Display information as expected!


Step 1:
To do that let’s start with building the base html with a table as shown below:

<?xml version="1.0" encoding="UTF-8"?>

<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:

<?xml version="1.0" encoding="UTF-8"?>


<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>
<xsl:for-each select="//book">
<tr>
<td>
<xsl:value-of select="title"></xsl:value-of>
</td>
<td>
<xsl:value-of select="author"></xsl:value-of>
</td>
</tr>
</xsl:for-each>

</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 “,”.

The complete XSL will look as shown below:

<?xml version="1.0" encoding="UTF-8"?>


<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 bgcolor="#00cccc">
<th>Title</th>
<th>Author(s)</th>
</tr>
<xsl:for-each select="//book">
<tr>
<td>
<xsl:value-of select="title"></xsl:value-of>
</td>
<td>
<xsl:for-each select="./author">
<xsl:value-of select="."></xsl:value-of>
<xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>

</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The final result will look as shown below

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:

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://exslt.org/functions"
xmlns:my="soa2world.blogspot.com" xmlns:math="http://exslt.org/math"
extension-element-prefixes="math func my">
<func:function name="my:generateRandomNumber">
<xsl:variable name="randomNum" select="math:random() * 100 " />
<func:result select="substring-before($randomNum,'.')" />
</func:function>
<xsl:template match="/">
<out>
<xsl:value-of select="my:generateRandomNumber()" />
</out>
</xsl:template>
</xsl:stylesheet>
When you execute the XSL file you may get the following output (84 is the random number):

<?xml version="1.0" encoding="UTF-8"?><out>84</out>

In the above XSL file we are using the following extensions

EXSLT Functions documented at http://www.exslt.org/func/index.html

EXSLT Math documented at http://www.exslt.org/math/index.html

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.

Author: Siva Prasanna Kumar.P

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).

S-ar putea să vă placă și