Sunday, July 28, 2013

Compare two collections or xmls and set value in output using xsl/transformation

We come across situations wherein we need to compare some elements from two collection and generate the output
I would like to explain the solution using the example:
Input1 contains list of employees and their current salary and Input2 contains the increment percentage for the employees.
Now input2 might not contain all the employees, so for those missing employees, the increment is default which is 5%
We need to calculate the new salary for all the employees from input1
Sample inputs are as below:

Input1:


<EmployeeDetails>
      <employeeid>1</employeeid>
      <employeename>employeename10</employeename>
      <salary>1000</salary>
   </EmployeeDetails>
   <EmployeeDetails>
      <employeeid>2</employeeid>
      <employeename>employeename12</employeename>
      <salary>10000</salary>
   </EmployeeDetails>
   <EmployeeDetails>
      <employeeid>3</employeeid>
      <employeename>employeename14</employeename>
      <salary>1500</salary>
   </EmployeeDetails>

Input2:


<IncrementDetails>
      <employeeid>1</employeeid>
      <incrementPercentage>10</incrementPercentage>
   </IncrementDetails>
   <IncrementDetails>
      <employeeid>2</employeeid>
      <incrementPercentage>20</incrementPercentage>
   </IncrementDetails>

Expected output:


<output xmlns:client="http://xmlns.oracle.com/CopyTables/LoopTwoNodes/LoopTwoNodesBPEL" xmlns="http://xmlns.oracle.com/CopyTables/LoopTwoNodes/LoopTwoNodesBPEL">
         <client:output>
            <client:employeeid>1</client:employeeid>
            <client:employeename>employeename10</client:employeename>
            <client:salary>1100</client:salary>
            <client:comment>Congratulation!! Your increment percentage is 10</client:comment>
         </client:output>
         <client:output>
            <client:employeeid>2</client:employeeid>
            <client:employeename>employeename12</client:employeename>
            <client:salary>12000</client:salary>
            <client:comment>Congratulation!! Your increment percentage is 20</client:comment>
         </client:output>
         <client:output>
            <client:employeeid>3</client:employeeid>
            <client:employeename>employeename14</client:employeename>
            <client:salary>1575</client:salary>
            <client:comment>Default increment</client:comment>
         </client:output>
      </output>

xsl/Transformation to achieve this looks like below:


<xsl:param name="input2"/>
  <xsl:template match="/">
    <client:output>
      <xsl:for-each select="/client:input1/client:EmployeeDetails">
        <xsl:variable name="loopvar" select="position()"/>
        <xsl:choose>
          <xsl:when test='contains(oraext:create-delimited-string($input2/client:input2/client:IncrementDetails/client:employeeid,","),client:employeeid)'>
            <xsl:for-each select="$input2/client:input2/client:IncrementDetails">
              <xsl:if test="client:employeeid = /client:input1/client:EmployeeDetails[$loopvar]/client:employeeid">
                <client:output>
                  <client:employeeid>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:employeeid"/>
                  </client:employeeid>
                  <client:employeename>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:employeename"/>
                  </client:employeename>
                  <client:salary>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:salary + (/client:input1/client:EmployeeDetails[$loopvar]/client:salary * (client:incrementPercentage div 100.0))"/>
                  </client:salary>
                  <client:comment>
                    <xsl:value-of select='concat("Congratulation!! Your increment percentage is ", client:incrementPercentage)'/>
                  </client:comment>
                </client:output>
              </xsl:if>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <client:output>
              <client:employeeid>
                <xsl:value-of select="client:employeeid"/>
              </client:employeeid>
              <client:employeename>
                <xsl:value-of select="client:employeename"/>
              </client:employeename>
              <client:salary>
                <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:salary + (/client:input1/client:EmployeeDetails[$loopvar]/client:salary * (5.0 div 100.0))"/>
              </client:salary>
              <client:comment>
                <xsl:text disable-output-escaping="no">Default increment</xsl:text>
              </client:comment>
            </client:output>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </client:output>
  </xsl:template>


Happy Coding J

Cheers,

LetsLearnOracleSOA Team

No comments:

Post a Comment