Tuesday, November 13, 2012

Using XSL Choose in inline XSLT in BizTalk mapper


We can use <xsl;choose> in BizTalk mapper using inline XSLT.
Scenario : Suppose if a XML is coming to BizTalk server and we have to check that if a customer is 'Premium' or 'Normal' or anything then we can use <xsl:choose> to check these conditions.

Source Schema :















Destination Schema :















Map:

In BizTalk Mapper we can add a scripting functiod and add the inline XSLT to the functiod.




















Inline XSLT :




<EmployeeDetail>
  <xsl:for-each select ="Data">
    <Details>
      <xsl:variable name="var:v1" select="EmployeeType/text()" />
      <xsl:choose>
        <xsl:when test="$var:v1 = 'Premium'">
          <Name>
            <xsl:value-of select="Employee_Name/text()"/>
          </Name>
        </xsl:when>
        <xsl:when test="$var:v1 = 'Normal'">
          <ID>
            <xsl:value-of select="EmployeeID/text()"/>
          </ID>
        </xsl:when>
        <xsl:otherwise>
          <Address>
            <xsl:value-of select="Address/text()"/>
          </Address>
        </xsl:otherwise>
      </xsl:choose>
    </Details>
  </xsl:for-each>
</EmployeeDetail>




Input XML:

Case 1:


<?xml version="1.0"?>
-<ns0:Employee xmlns:ns0="http://XSLT_Test.Source">
  -<Data>
    <Employee_Name>John</Employee_Name>
    <EmployeeType>Premium</EmployeeType>
    <EmployeeID>10</EmployeeID>
    <Address>44,Manhatton</Address>
    <PhoneNo>9986647474</PhoneNo>
  </Data>
</ns0:Employee>



Case 2:


<?xml version="1.0"?>
-<ns0:Employee xmlns:ns0="http://XSLT_Test.Source">
  -<Data>
    <Employee_Name>John</Employee_Name>
    <EmployeeType>Premium</EmployeeType>
    <EmployeeID>10</EmployeeID>
    <Address>44,Manhatton</Address>
    <PhoneNo>9986647474</PhoneNo>
  </Data>
</ns0:Employee>




Case 3:


<?xml version="1.0"?>
-<ns0:Employee xmlns:ns0="http://XSLT_Test.Source">
  -<Data>
    <Employee_Name>John</Employee_Name>
    <EmployeeType>Invalid</EmployeeType>
    <EmployeeID>10</EmployeeID>
    <Address>44,Manhatton</Address>
    <PhoneNo>9986647474</PhoneNo>
  </Data>
</ns0:Employee>





Test Map Results :

We can Test the map using "Test Map" and following are the results :

Case 1 :

<EmployeeDetail xmlns:ns0="http://XSLT_Test.Destination">
    <Details>

         <Name>John</Name>
       </Details>
   </EmployeeDetail>


Case 2 :

<EmployeeDetail xmlns:ns0="http://XSLT_Test.Destination">

   <Details>
      <ID>10</ID>
      </Details>
   </EmployeeDetail>

Case 3 :
  
     <EmployeeDetail xmlns:ns0="http://XSLT_Test.Destination">
        <Details>

   <Address>44,Manhatton</Address>
    </Details>
 </EmployeeDetail>


From above result we can see that how <xsl:choose> can be used to decide on the conditions.
When the input was a 'Premium' customer then the result was only 'Name' field.
    When the input was a 'Normal' customer then the result was only 'ID' field.
    When the input was a 'Invalid' customer then the result was only 'Address' field.



No comments:

Post a Comment