In this article we will explain most common used XSL elements within XSL transformation (XSLT). We will try to keep high-level and if you need any specific information please write us a line in comment box.
Table of Contents
- xsl:stylesheet or xsl:transform
- xsl:output
- xsl:templates
- xsl:apply-templates
- xsl:call-template
- xsl:for-each
- xsl:value-of
- xsl:if
- xsl:choose
- xsl:when
- xsl:otherwise
- xsl:sort
- xsl:with-param
- xsl:param
- xsl:variable
<xsl:stylesheet> or <xsl:transform>
The <xsl:stylesheet> and the <xsl:transform> both are identical. you can use anyone in your XSLT. It is a required element in the XSL transformation. In this element, you can define all the namespace that you would like to use within the XSLT transformation.
Syntax
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<!-- Write here your transformation logic -->
</xsl:transform>
Attribute
- xmlns:xsl , xmlns:wd(required): Define your all namespace within <xsl:stylesheet> or <xsl:template> element. xmlns: followe by link. Elements that are not part of the namespace will file without being modified.
- version(required): Specify the XSL version that you would like to use. possible value 1.0 or 2.0 or 3.0
- exclude-result-prefixes(optional): Specify namespace that needs to be excluded from your output file. you can specify all namespace separated by space.
If your output file format is xml and you would like to appear namespace in file then just skip those namespace in list. If you all the namespace to not appear in output file then just use as exclude-result-prefixes=”#all”
<xsl:output>
XSLT <xsl:output> element helps to define property of output document produce by XSL transformation. This element is optional within and if it’s not defined then by default output format will be XML.
Syntax
<xsl:output method="xml|html|text" encoding="UTF-8|ISO 8859-1" indent="yes|no" omit-xml-declaration = "yes|no"/>
Attribute
- method(optional): Specify the type of output file. Possible value is “xml” or “text” or “html”. if the method attribute is not defined then the default value is “xml”.
- encoding(optional): Specify the encoding type of output document. e.g.: “UTF-8” . Reference: Common Character
- Encodings Type indent(optional): This attribute can be specified only when the output document is in XML. Specify “yes” for pretty xml format. If the indent attribute is not defined then the default value is “no”.
You should rarely use indent=”yes” whenever required because this will increase the size of the output document up to 30% which can also impact the scalability and performance of the integration.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration = "no"/>
<xsl:template match="/">
<!-- in case of report xml match attribute can be match="/wd:Report_Data" -->
<!-- Define root node -->
<workers>
<worker>Data specific to worker </worker>
</workers>
</xsl:template>
</xsl:stylesheet>
<xsl:templates>
XSLT <xsl:templates> element helps to define property of output document produce by XSL transformation. This element is optional within and if it’s not defined then by default output format will be XML.
Syntax
<xsl:template match="pattern">
<!-- Write here your template rules -->
<!-- You can write transaformation rule here or you can call another template to apply specifc rules to matching pattern -->
</xsl:template>
Attribute
- match(required) : Specify pattern(XPATH) of node that you want to processed by template.
<xsl:templates> must have a name or match attribute (or both).
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<!-- in case of report xml match attribute can be match="/wd:Report_Data" -->
<!-- Define root node -->
<workers>
<worker>Data specific to worker </worker>
</workers>
</xsl:template>
</xsl:stylesheet>
<xsl:call-template>
XSLT <xsl:call-template> element allows you to call specific template.
Syntax
<xsl:call-template name="template name">
<xsl:with-param name="parameter_name" select = "XPATH or Constant"/> <!-- Optional element-->
</xsl:call-template>
Attribute
- name(optional) : Specify template name.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:call-template name="header">
<xsl:with-param name="rowcount" select = "count(wd:Report_Entry)" /> <!-- Optional element-->
</xsl:call-template>
<xsl:apply-templates select="wd:Report_Entry"/>
</workers>
</xsl:template>
<xsl:template name="header">
<xsl:param name = "rowcount" />
<header><xsl:text>Row Count: </xsl:text><xsl:value-of select="$rowcount"/></header>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee><xsl:text>This is example node for an employee : </xsl:text><xsl:value-of select="wd:Legal_Name"/></employee>
</xsl:template>
</xsl:stylesheet>
<xsl:xsl:apply-templates>
XSLT <xsl:apply-templates> element allows you to apply specific template rules for the selected node.
Syntax
<xsl:apply-templates select="pattern">
<xsl:sort> <!-- optional -->
<xsl:with-param> <!-- optional -->
</xsl:apply-templates>
Attribute
- select(optional) : Specify pattern(XPATH) of node that you want to processed by template.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:wd="urn:com.workday.report/INHCM0001-Outbound"
exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="/wd:Report_Data"> <!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry" />
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee>
<xsl:text>This is example node for an employee : </xsl:text>
<xsl:value-of select="wd:Legal_Name"/>
</employee>
</xsl:template>
</xsl:stylesheet>
<xsl:for-each>
XSLT <xsl:for-each> element allows to loop through selected node-set.
Syntax
<xsl:for-each select="pattern">
<xsl:sort> <!-- optional -->
</xsl:for-each>
Attribute
- select(required) : Specify pattern(XPATH) of node that you want to loop through.
<xsl:for-each> can have <xsl:sort> element. if <xsl:sort> element is specified then node will get sorted before transformation rules get applied otherwise node will get loop through in order that they occur.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:for-each select="wd:Report_Entry">
<employee><xsl:text>Executing loop for : </xsl:text><xsl:value-of select="wd:Legal_Name"/></employee>
</xsl:for-each>
</workers>
</xsl:template>
</xsl:stylesheet>
<xsl:value-of>
XSLT <xsl:value-of> element allows to print/write data to an output document. In XSLT <xsl:value-of> element can occur more than once but <xsl:value-of> element can only have one select attribute.
Syntax
<xsl:value-of select="pattern" disable-output-escaping="yes|no" />
Attribute
- select(required): Specify the pattern(XPATH or expression) that you want to print in the output document.
- disable-output-escaping(optional) : Specify if special character should display as-is in output document. Default is no.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry">
<xsl:sort select="wd:Legal_Name"/>
<xsl:sort select="wd:Employee_ID" data-type="number" order="descending"/>
</xsl:apply-templates>
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee>
<legalname><xsl:value-of select="wd:Legal_Name"/></legalname>
<emlployeeid><xsl:value-of select="wd:Employee_ID"/></emlployeeid>
</employee>
</xsl:template>
</xsl:stylesheet>
<xsl:if>
XSLT <xsl:if> element allows evaluating condition which returns a boolean result to determine if template rule should be applied or not.
<xsl:if> does not supports to construct else condition or else-if as other programming language does, in case of this use <xsl:choose>.
Syntax
<xsl:if test="condition">
<!-- Write template rule that should be executed after evaluation condition is true -->
</xsl:if>
Attribute
- test(required) : Specify expression/condition that should get evaluated. you can construct condition expression using different comparison( = | != | < | > | <= | > = ) and logical operators ( or | and ).
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry">
<xsl:sort select="wd:Legal_Name"/>
<xsl:sort select="wd:Employee_ID" data-type="number" order="descending"/>
</xsl:apply-templates>
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee>
<legalname><xsl:value-of select="wd:Legal_Name"/></legalname>
<xsl:if test="wd:Country='Canada'"><countryisocode>CAN</countryisocode></xsl:if>
<emlployeeid><xsl:value-of select="wd:Employee_ID"/></emlployeeid>
</employee>
</xsl:template>
</xsl:stylesheet>
<xsl:choose> , <xsl:when> , <xsl:otherwise>
XSLT <xsl:choose> element allows to evaluate multiple condition apply different template rule depending on condition met. <xsl:choose> element must have one or more <xsl:when> and contain have only one optional <xsl:otherwise> element.
<xsl:when> element allows evaluating condition which returns a boolean result to determine if template/transformation rule should be applied or not. <xsl:when> element should appear within <xsl:choose> in order that needs to be evaluated, if more than one <xsl:when> condition satisfied then first occurrence template rules will apply.
<xsl:otherwise> element allow to apply specific template rules if none of the test conditions in any of xsl:when element is satisfied. If there is no <xsl:otherwise> element in <xsl:choose> then the <xsl:choose> element is exited.
Syntax
<xsl:choose>
<xsl:when test="condition">
<!-- Write template rule that should be executed after evaluation condition is true -->
</xsl:when>
<xsl:when test="condition">
<!-- Write template rule that should be executed after evaluation condition is true -->
</xsl:when>
<xsl:otherwise>
<!-- Write template rule that should be executed after evaluation condition is true -->
</xsl:otherwise>
</xsl:choose>
Attribute
- none
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry">
<xsl:sort select="wd:Legal_Name"/>
<xsl:sort select="wd:Employee_ID" data-type="number" order="descending"/>
</xsl:apply-templates>
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee>
<legalname><xsl:value-of select="wd:Legal_Name"/></legalname>
<xsl:choose>
<xsl:when test="wd:Country = 'Canada'">
<!-- Write template rule that should be executed after evaluation condition is true -->
<countryisocode>CAN</countryisocode>
</xsl:when>
<xsl:when test="wd:Country = 'Australia'">
<!-- Write template rule that should be executed after evaluation condition is true -->
<countryisocode>AUS</countryisocode>
</xsl:when>
<xsl:otherwise>
<!-- Write default template rule that should be executed if none of above condition is met -->
<countryisocode><xsl:value-of select="wd:Country"/></countryisocode>
</xsl:otherwise>
</xsl:choose>
<emlployeeid><xsl:value-of select="wd:Employee_ID"/></emlployeeid>
</employee>
</xsl:template>
</xsl:stylesheet>
<xsl:sort>
XSLT <xsl:sort> element allows to loop through selected node-set.
Syntax
<xsl:sort select="pattern" data-type="number|qname|text" order="ascending|descending" case-order="upper-first|lower-first" lang="language-code"/>
Attribute
- select(optional): Specify the pattern(XPATH) of node that you want to loop through. You can also refer to this as the sort key.
- data-type(optional): Specify data type of sort key e.g. number or text or qname.
- order(optional): Specify sorting order whether node should get processed in ascending or descending order.
- case-order(optional): if a sort key data type is text then you can further define sort logic based on text case e.g. upper-first or lower-first.
- lang(optional): Specify language alphabet to determine the sort order.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry">
<xsl:sort select="wd:Legal_Name"/>
<xsl:sort select="wd:Employee_ID" data-type="number" order="descending"/>
</xsl:apply-templates>
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee>
<legalname><xsl:value-of select="wd:Legal_Name"/></legalname>
<emlployeeid><xsl:value-of select="wd:Employee_ID"/></emlployeeid>
</employee>
</xsl:template>
</xsl:stylesheet>
<xsl:with-param>
XSLT <xsl:with-param> element allows set parameter to be passed to template. <xsl:with-param> can appear within an <xsl:apply-templates> or an <xsl:call-template> element.
Syntax
<xsl:with-param name="parameter_name" select = "XPATH or pattern"/>
Attribute
- name(required): Specify parameter name.
- select(optional): Specify the pattern(XPATH) of the node that you want to pass to the parameter.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:call-template name="header">
<xsl:with-param name="rowcount" select = "count(wd:Report_Entry)" /> <!-- Optional element-->
</xsl:call-template>
<xsl:apply-templates select="wd:Report_Entry"/>
</workers>
</xsl:template>
<xsl:template name="header">
<xsl:param name = "rowcount" />
<header><xsl:text>Row Count: </xsl:text><xsl:value-of select="$rowcount"/></header>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee><xsl:text>This is example node for an employee : </xsl:text><xsl:value-of select="wd:Legal_Name"/></employee>
</xsl:template>
</xsl:stylesheet>
<xsl:param>
XSLT <xsl:param> element allows to declare a local or global parameter and define parameter name and default value. The default value will be used only if no other value is provided when the template is called.
Syntax
<xsl:param name="parameter_name" select = "XPATH or pattern or constant"/>
Attribute
- name(required): Specify parameter name.
- select(optional): Specify default value that should get used if none is specified.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound" exclude-result-prefixes="wd xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:call-template name="header">
<xsl:with-param name="rowcount" select = "count(wd:Report_Entry)" /> <!-- Optional element-->
</xsl:call-template>
<xsl:apply-templates select="wd:Report_Entry"/>
</workers>
</xsl:template>
<xsl:template name="header">
<xsl:param name = "rowcount" select="parameter value not specified"/>
<header><xsl:text>Row Count: </xsl:text><xsl:value-of select="$rowcount"/></header>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<employee><xsl:text>This is example node for an employee : </xsl:text><xsl:value-of select="wd:Legal_Name"/></employee>
</xsl:template>
</xsl:stylesheet>
<xsl:variable>
XSLT <xsl:variable> element allows to declare a local or global variable and define variable name and value. If the variable is declared at a top-level element to the stylesheet then the variable scope is global and it can be accessed throughout the document. if a variable is defined within a template or for each then variable scope is local and only accessible only within declaring template or for each.
Variable can be accessed using $variablename.
Syntax
<xsl:variable name="variable_name" select = "XPATH or pattern or constant"/>
or
<xsl:variable name="variable_name">
<!-- write transformation rule to set variable value -->
</xsl:variable>
Attribute
- name(required): Specify variable name.
- select(optional): Specify XPATH or expression to set variable value.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INHCM0001-Outbound">
<xsl:output method="xml" encoding="UTF-8"/>
<!-- Defining Global Variable -->
<xsl:variable name="globalvariable" select="'My Global Variable'"/>
<xsl:template match="/wd:Report_Data">
<!-- Define root node -->
<workers>
<xsl:apply-templates select="wd:Report_Entry">
<xsl:sort select="wd:Legal_Name"/>
<xsl:sort select="wd:Employee_ID" data-type="number" order="descending"/>
</xsl:apply-templates>
</workers>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<!-- Defining Global Variable -->
<xsl:variable name="linenumber">
<xsl:value-of select="position()"/>
</xsl:variable>
<employee>
<linenumber><xsl:value-of select="$linenumber"/></linenumber>
<legalname><xsl:value-of select="wd:Legal_Name"/></legalname>
<xsl:choose>
<xsl:when test="wd:Country = 'Canada'">
<!-- Write template rule that should be executed after evaluation condition is true -->
<countryisocode>CAN</countryisocode>
</xsl:when>
<xsl:when test="wd:Country = 'Australia'">
<!-- Write template rule that should be executed after evaluation condition is true -->
<countryisocode>AUS</countryisocode>
</xsl:when>
<xsl:otherwise>
<!-- Write default template rule that should be executed if none of above condition is met -->
<countryisocode><xsl:value-of select="wd:Country"/></countryisocode>
</xsl:otherwise>
</xsl:choose>
<emlployeeid><xsl:value-of select="wd:Employee_ID"/></emlployeeid>
<globalvariable><xsl:value-of select="$globalvariable"/></globalvariable>
</employee>
</xsl:template>
</xsl:stylesheet>