vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

XSLT and Eclipse - Tutorial

Lars Vogel

Version 0.3

12.08.2010

Revision History
Revision 0.107.12.2009Lars Vogel
Created
Revision 0.206.07.2010Lars Vogel
Updated to Eclipse 3.6 Helios
Revision 0.312.08.2010Lars Vogel
bugfixes and enhancements

XSLT with Eclipse

This articles explains how to do a simple XSLT transformation using the Eclipse XSL project. In this article Eclipse 3.6 (Helios) is used.


Table of Contents

1. XSLT
2. Eclipse XSLT installation
3. Your first transformation - copy
4. Your first transformation
5. Thank you
6. Questions and Discussion
7. Links and Literature

1. XSLT

XSLT allows to convert XML to other format. XSLT stands for XSL Transformations. The Eclipse XSL Project allows to edit XSLT files and perform interactive XSL transformations in Eclipse. For an introduction to XML please see Java and XML Tutorial .

2. Eclipse XSLT installation

Install the "Eclipse XSL Developer Tools" via the Eclipse update manager .

3. Your first transformation - copy

We start first with a simplest transformation possible; no transformation at all. Create a new Java project "de.vogella.xslt.first". Create a folder "files".

Create the following XML file "source.xml" in the folder "files".

			
<?xml version="1.0"?>
	<!-- This is a comment -->
<people>
	<address type="personal">
		<name>Lars </name>
		<street> Test </street>
		<telephon number="0123" />
	</address>
	<address type="personal">
		<name>Joe </name>
		<street> Test2 </street>
		<telephon number="1234" />
	</address>
	<address type="business">
		<name>Jim</name>
		<street> Test3 </street>
		<telephon number="2345" />
	</address>
</people>
		

Create the following XLS file "transform.xsl" in the folder "files"

			
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">

	<xsl:output method="xml" />

	<xsl:template match="*">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:apply-templates />
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

		

Select your xsl file and run it as XSL application.

Select "Open Files..." and select "source.xml". Press ok to run it.

Review the result "source.out.xml"

4. Your first transformation

We want to do a real transformation. Create the following "transform2.xsl" and run it with the same input file

			
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">

	<xsl:output method="xml" />

	<!-- Copy everything -->
	<xsl:template match="*">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:apply-templates />
		</xsl:copy>
	</xsl:template>

	<!-- Do some adjustments for the address -->
	<xsl:template match="address">
		<xsl:element name="place-where-person-live">
			<xsl:apply-templates />
		</xsl:element>
	</xsl:template>

	<!-- Put the name in a <hubba> tag -->
	<xsl:template match="name">
		<xsl:element name="name">
			<hubba>
				<xsl:apply-templates />
			</hubba>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>

		

The result should look like the following.

			
<?xml version="1.0" encoding="UTF-8"?><people>
	<place-where-person-live>
		<name><hubba>Lars </hubba></name>
		<street> Test </street>
		<telephon number="0123"/>
	</place-where-person-live>
	<place-where-person-live>
		<name><hubba>Joe </hubba></name>
		<street> Test2 </street>
		<telephon number="1234"/>
	</place-where-person-live>
	<place-where-person-live>
		<name><hubba>Jim</hubba></name>
		<street> Test3 </street>
		<telephon number="2345"/>
	</place-where-person-live>
</people>
		

5. Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.

Flattr this

6. Questions and Discussion

Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .

7. Links and Literature

http://wiki.eclipse.org/XSLT_Project Eclipse XSL Wiki

http://www.dpawson.co.uk/xsl/sect2/sect21.html XSLT FAQ

http://www.zvon.org/xxl/XSLTutorial/Books/Book1/index.html XSLT Tutorial