Since I am writing this Bio by myself I won't writing it in 3rd Person. The motivation to post here is to get feedback of my post's, which are mostly summary of mine about software development. Other reason to write here is to share my experience with the community. That all together as résumé I would give a name like "The Philosophy of Programming" or in other words "Bullshit Bingo for Coders". Kosta has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Introduction to XSLT

11.19.2013
| 3561 views |
  • submit to reddit

Introduction to XSLT

This article is an introduction to XML transformation stylesheets, better known as XSLT [ref], with some simple samples.

Actually, I wanted to call this article "Java and XML – Part 4 (XSLT)," but since I have mentioned the Java part in one of my previous articles [ref], that doesn't seems to be nesessery here. In that article you can see how the syntax for binding the XSLT process using the JAXP parsers DOM and JDOM2 looks.

The XSL files have XML syntax, but they are used to transform one XML file into another. The XML transformation can be also done with the JAXP parsers, but that way requires much more coding.

If your favorite IDE is Eclipse, then you can use the XML perspective for testing the XSL-files. Further explanation can be found in Lars Vogel's blog [ref]. With this approach, you can concentrate on developing the transformation logic used by the XSLT without affecting other parts of your application.

Examples

I found a nice article called "10 XSLT or XML, XSL transformation Interview Questions and Answers for Java Programmers," [ref] which I will use it as use cases here. Similar examples like the following can be read on the "xmlplease" site [ref].

Given is the simple xml file:

<?xml version="1.0" encoding="UTF-8"?>
<person type="employee">
	<firstName>John</firstName>
	<lastName>Doe</lastName>
</person>

Empty File

Using the simple template ignores all nodes and attributes.

Here is the XSL file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- situation 1 -->
	<!-- output XML empty -->
	<xsl:template match="/" />
</xsl:stylesheet> 

The output looks like:

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

Identity template usage

With this kind of template all nodes and attributes are copied into the output XML file.

<xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()" />
     </xsl:copy>
</xsl:template>

Removing element and attribute

Here is the XSL file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- situation 2 -->
	<!-- ignoring nodes and atrributes -->
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="firstName" />
	<xsl:template match="@type" />
</xsl:stylesheet>
The output looks like:
<?xml version="1.0" encoding="UTF-8"?>
<person>
	<lastName>Doe</lastName>
</person>

Renaming element and attribute

Here is the XSL file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- situation 3 -->
	<!-- renaming nodes and atrributes -->
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="@type">
		<xsl:attribute name="kind">
         <xsl:value-of select="." />
    </xsl:attribute>
	</xsl:template>
	<xsl:template match="person">
		<item>
			<xsl:apply-templates select="@*|node()" />
		</item>
	</xsl:template>
</xsl:stylesheet> 
The output looks like:
<?xml version="1.0" encoding="UTF-8"?>
<item kind="employee">
	<firstName>John</firstName>
	<lastName>Doe</lastName>
</item>

Retrieving attribute value

Here is the XSL file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- situation 4 -->
	<!-- retrieving atrribute value -->
	<xsl:template match="/person">
	<personType>
		<xsl:value-of select="@type"></xsl:value-of>
	</personType>	
	</xsl:template>
</xsl:stylesheet>
The output looks like:
<?xml version="1.0" encoding="UTF-8"?>
<personType>employee</personType>

Links

Other links on this topic can be found here on DZone or on the internet.

Links to DZone articles

http://architects.dzone.com/articles/some-basics-xslt
http://architects.dzone.com/articles/obtaining-complete-xml-message
http://architects.dzone.com/articles/how-handle-heavily-nested-xml
http://java.dzone.com/articles/joox-and-xslt-xml-love-story

Links to DZone refcards

http://refcardz.dzone.com/category/tags/xslt?page=1

Links to DZone snippets

http://www.dzone.com/snippets/forloop-simple-loop-xslt
http://www.dzone.com/snippets/find-and-replace-inside-xml
http://www.dzone.com/snippets/trim-template-xslt
http://www.dzone.com/snippets/xslt-recursively-set-prefix
http://www.dzone.com/snippets/some-javascript-usefull
http://www.dzone.com/snippets/formatting-time-xslt

Links to DZone news

http://java.dzone.com/news/death-xslt-web-frameworks

Links to external tutorials

http://zvon.org/xxl/XSLTutorial/Output/elementIndex.html
http://zvon.org/comp/m/xslt.html
http://www.w3schools.com/xsl/default.asp
http://www.xmlplease.com

Links to external best practice

http://www.codedairy.com/blog/best-practices-and-coding-standards-for-using-xslt-transformations

GitHub

The sources for this example can be checked out from: https://github.com/kstojanovski/java-and-xml. Use the java-and-xml folder and start the test method of the TestJavaXml.java file from the test folder.

Published at DZone with permission of its author, Kosta Stojanovski.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)