Enterprise Integration Zone is brought to you in partnership with:

Madhuka is Software Engineer and currently works on the Jaggery, UES, WSO2 Gadget Server team. He holds a B.Sc (Hons) degree in Information Technology from the Faculty of Information Madhuka is a DZone MVB and is not an employee of DZone and has posted 52 posts at DZone. You can read more from them at their website. View Full User Profile

Some Basics on XSLT Transformation

05.29.2013
| 1719 views |
  • submit to reddit

This post will elucidate some basic concerns for development of XSLT scripts. While I was writing some script for a wso2 ESB XSLT transformation I decided that this process would be a useful one to share.

Firstly I will point out some XSLT Elements

template  Rules to apply when a specified node is matched and it is a top-level element. But I like to used as function and call it and reuse it. Such like java or JavaScript or C# or ruby we can pass some parameters for that method also and use them in there.

<xsl:template name="get_age">
<xsl:param name="age" select="'%str'" />
<age><xsl:value-of select="$age"/></age>
</xsl:template>

you can call this get_age from XSLT

<xsl:call-template name="get_unique_value"> 
<xsl:with-param name="age"  select="//students/james/age/node()" />

Commenting also input after xslt generate it output. In there you like to see some comments

  Here I am writing a comment for my out transformation.
</xsl:comment>

If Input XML if we have age for student name (james) write it for output. Only the age file not the parent tags.

<xsl:if test="//students/james/age!='' ">
<age><xsl:copy-of select="//students/james/age/node()"/></age>
</xsl:if>

for-each Student will have age and we need each age for each student. Element loops through each node in a specified node set.

<xsl:for-each select="//students”>
<student>
<age><xsl:copy-of select="//students/james/age/node()"/></age>
<student>
</xsl:for-each>

choose school for student age

<xsl:choose>
  <xsl:when test="age>'10'">
  <school><xsl:text>ordinary</xsl:text><school>
  </xsl:when>
  <xsl:otherwise>
  <school><xsl:text>primary</xsl:text><school>
  </xsl:otherwise>
</xsl:choose>
Next post I will write some XSLT Functions 
Published at DZone with permission of Madhuka Udantha, author and DZone MVB. (source)

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