Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Sunday, February 13, 2011

Comparing Dates in XSLT

Dates manipulation is a very daunting task in XSLT, considering the very limited date-time functions that XSLT 1.0 offers. XSLT 2.0 is enhanced as far this case is concerned. I had to compare dates in XSLT 1.0 and I was contemplating of using a java embedding node, not until I found how to compare the dates in XSLT as below:

1. Convert the date into the format YYYY-MM-DD, either using a function or using existing string functions
2. Do a translate on the date available, translate($Date, '-', '')
3. By doing a translate, the date is converted to ISO date format
4. Do this to both the dates
5. The dates in the ISO format can be directly used for comparison like ISODate1 > ISODate2 etc


Cheers,
-AR

Thursday, January 13, 2011

XSLT for indentifying line feed character

A line feed character is nothing but a new line character. I had a requirement to break a text in one of fields in input xml received by a bpel process. I tried using substring-before function with checking if the value contains '&xa'. However, the soa server does not seem to recognize this.

I understood Substring wouldn't work for line feed characters. The xslt chunk in this post will remove the line feed character and extract the text. Consider the input xml:
<Emp_No>100</Emp_No>
<Emp_Name>SS</Emp_Name>
<Details>First Employee
Second Employee
Third Employee</Details>

This xml has to be transformed to:
<Details1>First Employee</Details1>
<Details2>Second Employee</Details2>
<Details3>Third Employee</Details3>

Use the XSLT below to do the transformation:
<xsl:variable name="Temp">
<xsl:value-of select="translate(/InputXml/Details, '&#xA;', '|')"/>
</xsl:variable>
<Details1>
<xsl:value-of select="substring-before($Temp,'|')"/>
</Details1>
<Details2>
<xsl:value-of select="substring-before(substring-after($Temp,'|'),'|')"/>
</Details2>
<Details3>
<xsl:value-of select="substring-after(substring-after($Temp,'|'),'|')"/>
</Details3>


Cheers,
-AR


Saturday, January 8, 2011

XSLT transformation and Line Breaks

This post details how to add and remove Line Breaks in a XSLT.

Removing Line Breaks in XSLT:

Consider the XML below. Need to remove the line breaks in the input payload:
<test>Line1
Line2</test>


Use the XSLT chunk below to remove the line break from the XML:
<xsl:variable name="Temp">
<xsl:value-of select="translate(/InputXml/test, '&#xA;', '|')"/>
</xsl:variable>
<Line1>
<xsl:value-of select="substring-before($Temp,'|')"/>
</Line1>
<Line2>
<xsl:value-of select="substring-after($Temp,'|')"/>
</Line2>


And then do a concat($Line1,',',$Line2) or as required!

Adding Line Breaks in XSLT:

Adding a line break should not be an issue once the removal of it has been figured out. Lets consider the same XML without line break. We will add line breaks to generate the xml:
<test>Line1
Line2</test>


 Use the XSLT chunk below to add the line break from the XML:
<xsl:stylesheet version="1.0">
<xsl:template match="/">
<OutputXml>
<Details>
<xsl:value-of select="concat(/InputXml/Line1,'&#xA;',/InputXml/Line2)"/>
</Details>
</OutputXml>
</xsl:template>
</xsl:stylesheet>
 

Cheers,
-AR
 

Thursday, July 22, 2010

Remove duplicates from an input XML using XSLT

It is often the case that an input XML may contain duplicate data. It might be necessary to filter the duplicate data using a unique identifier, also from the input and send only the non-repeating, unique data to the output. In these cases, this transformation can be used to filter the data in the input.
This can be better understood by mapping this case to a real time scenario. Let us assume an organization having employees who can work in more than one department. If the input is going to contain the list of employees based on the department, there will be some employees whose data can repeat, as they are part of more than one department. If we need to filter the result based on the employee id to get a set of non-repeating unique employees, this logic can be used.

The implementation of this logic has been done using the function “following::”. The transformation logic is as below:


<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper
  <!-- SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY. -->
  <mapSources>
    <source type="XSD">
      <schema location="http://localhost:7778/Schemas/Sample.xsd"/>
      <rootElement name="SampleXML" namespace="http://xmlns.oracle.com/
SampleXML"/>
    </source>
  </mapSources>
  <mapTargets>
    <target type="XSD">
      <schema location="
http://localhost:7778/Schemas/Sample.xsd"/>
      <rootElement name="
SampleXML" namespace="http://xmlns.oracle.com/SampleXML"/>
    </target>
  </mapTargets>
  <!-- GENERATED BY ORACLE XSL MAPPER 10.1.3.4.0(build 080718.0645) AT [THU SEP 24 15:16:11 EEST 2009]. -->
?>
<xsl:stylesheet version="1.0"
                xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="xsl xsd bpws hwf">
    <xsl:template match="/">
        <EmployeeListSorted>
            <xsl:for-each select="/EmployeeList/Employee[not(EmpId=following::EmpId)]">
                <xsl:sort select="./EmpId" order="ascending"/>
                <Employee>
                    <xsl:copy-of select="./Name"/>
                    <xsl:copy-of select="./EmpId"/>
                </Employee>
            </xsl:for-each>
        </EmployeeListSorted>
    </xsl:template>   
</xsl:stylesheet>



The preceding-sibling grouping technique did not work as because your nodes are not siblings of each other and because it only works where the grouping key is the string-value of the node, not where it is some other function of the node (here, its name).

Peace !

Cheers,
- AR
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License