XSLT Conditional Logic: if, choose, when and otherwise
XSLT Sorting with xsl:sort
Sorting is a very common requirement in XSLT transformations. XSLT gives us the xsl:sort instruction for this. It works inside xsl:for-each and inside xsl:apply-templates. While working on integration scenarios, I have used almost every option of xsl:sort. I thought of writing a blog post so others can easily find examples as well.
Basic Sorting
The xsl:sort instruction must be the first child of xsl:for-each. The select attribute defines the sort key.
Sort by name:
XSLT Date and Time Manipulation
In XSLT (Extensible Stylesheet Language Transformations) 2.0 or later, we can perform several date and time manipulation processes. I thought of writing a blog post so others can easily find examples as well.
Basic Date & Time XSLT Functions:
current-date() - Returns the current date of the server that XSLT runs
current-time() - Returns the current time of the server that XSLT runs
current-dateTime() - Returns the current date and time of the server that XSLT runs
implicit-timezone() - Returns the implicit time zone of the server that XSLT runs
Compare Two XML Elements and Filter using WSO2 MI
Think about an integration scenario, where you are getting a list of data from a service endpoint(Data 1) and you want to exclude or match this response against the data returned by another service endpoint(Data 2).
In such a situation, first, you need to have a unique attribute in both data set to exclude or match elements. So in my use case, list of data and matching content is as below
<CodeLists> <CodeList> <Id>O</Id> <Name>Open</Name> </CodeList> <CodeList> <Id>C</Id> <Name>Cancelled</Name> </CodeList> <CodeList> <Id>X</Id> <Name>Denied</Name> </CodeList> <CodeList> <Id>P</Id> <Name>Pending</Name> </CodeList> <CodeList> <Id>D</Id> <Name>Duplicate</Name> </CodeList> </CodeLists>
<CodeLists> <CodeList> <Id>O</Id> <Name>Open</Name> </CodeList> <CodeList> <Id>P</Id> <Name>Pending</Name> </CodeList> </CodeLists>
<property name="elementsToBeHide" expression="string-join(//CodeLists/CodeList/Id, ',')" scope="default" type="STRING"/>
<xslt key="conf:/xslts/xslt_FilterResults.xslt"> <property name="elementsToBeHide" expression="$ctx:elementsToBeHide"/> </xslt>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sy="http://ws.apache.org/ns/synapse" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <xsl:strip-space elements="*" /> <xsl:param name="elementsToBeHide" /> <xsl:template match="/CodeLists"> <CodeLists> <CodeList> <xsl:for-each select="CodeList"> <xsl:choose> <xsl:when test="matches($elementsToBeHide,Id)"> <!-- match senarios --> </xsl:when> <xsl:otherwise> <!-- not matching --> <Code> <Name><xsl:value-of select="Name"/></Name> <Id><xsl:value-of select="Id"/></Id> </Code> </xsl:otherwise> </xsl:choose> </xsl:for-each> </CodeList> </CodeLists> </xsl:template> </xsl:stylesheet>
The output of the XSLT transformation is as below and it only includes elements which are not matching to the filtering data set.
<CodeLists> <CodeList> <Code> <Id>C</Id> <Name>Cancelled</Name> </Code> <Code> <Id>X</Id> <Name>Denied</Name> </Code> <Code> <Id>D</Id> <Name>Duplicate</Name> </Code> </CodeList> </CodeLists>
XSLT - Modify Date and DateTime value
xs:yearMonthDuration
For this function input value is year month duration and it should be passed in ISO_8601 format
P - The duration designator(for period).
xs:dayTimeDuration
For this function input value is day time duration and it also needs to be passed in ISO_8601 format
n- the [n] is replaced by the value for each of the date and time elements
P - The duration designator(for period).
Example XSLT :
Remove duplicate XML elements using XSLT
My XML input:
XSLT:
XML Output :