Search code examples
xsltbiztalkbiztalk-mapper

BizTalk mapping inline XSLT


Input schema have field <Partycode Value="Agent" />. Based on the this node I need to generate one destination node. Destination is single schema. If partycode is Agent I want destination node with value of Fname.

If partycode is not agent I want destination node without any value. <Participatent></Participatent> unbounded in the schema. In destination is not unbounded

Scenario 1 Inputschema

<ns0:Schema xmlns:ns0="http://Host_service_proj.Inbound_test1">
  <Participatent>
    <ParticipantDetails>
      <Participant>
        <Partycode Value="" />
        <FIRSTname>FIRSTname_0</FIRSTname>
        </Participant>
     <Participant>
         <Partycode Value="Agent" />
        <FIRSTname>Agentname1</FIRSTname>
         </Participant>
    </ParticipantDetails>
  </Participatent>
</ns0:Schema>

Expected result

<ns0:CreateClaim xmlns:ns0="http://Host_service_proj.Oubound_Test1">
  <Agent>Agentname1</Agent>
</ns0:CreateClaim>

Scenario 2 Inputschema

<ns0:Schema xmlns:ns0="http://Host_service_proj.Inbound_test1">
  <Participatent>
    <ParticipantDetails>
      <Participant>
          <Partycode Value="" />
        <FIRSTname>FIRSTname_0</FIRSTname>
      </Participant>
    </ParticipantDetails>
  </Participatent>
</ns0:Schema>

Expected result

<ns0:CreateClaim xmlns:ns0="http://Host_service_proj.Oubound_Test1">
<Agent></Agent>
</ns0:CreateClaim>

I am trying to write inline xslt for this

<xsl:for-each select="Participatent/ParticipantDetails/Participant">
  <xsl:variable name="var:v1" select="userCSharp:LogicalEq(string(Partycode/@Value) , &quot;Agent&quot;)" />
  <xsl:if test="string($var:v1)='true'">
    <xsl:variable name="var:v2" select="FIRSTname/text()" />
    <Agent>
      <xsl:value-of select="$var:v2" />
    </Agent>
  </xsl:if>
</xsl:for-each>

But I am not able to do blank <Agent> in destination in the second scenario.


Solution

  • You need to use choose rather than an if, and don't use a loop, just use XPath.

    <xsl:variable name="var:v1" select="/*[local-name()='Schema' and namespace-uri()='http://Host_service_proj.Inbound_test1']/*[local-name()='Participatent' and namespace-uri()='']/*[local-name()='ParticipantDetails' and namespace-uri()='']/*[local-name()='Participant' and namespace-uri()=''][Partycode/@Value='Agent']/*[local-name()='FIRSTname' and namespace-uri()='']" />
       <xsl:choose>
         <xsl:when test="string($var:v1)!=''">
           <Agent>
             <xsl:value-of select="$var:v1" />
           </Agent>
         </xsl:when>
         <xsl:otherwise>
           <Agent>
           </Agent>
         </xsl:otherwise>
       </xsl:choose>
    

    In fact you don't need variables and a decision at all, you can do it with one line with XPath.

           <Agent>
             <xsl:value-of select="/*[local-name()='Schema' and namespace-uri()='http://Host_service_proj.Inbound_test1']/*[local-name()='Participatent' and namespace-uri()='']/*[local-name()='ParticipantDetails' and namespace-uri()='']/*[local-name()='Participant' and namespace-uri()=''][Partycode/@Value='Agent']/*[local-name()='FIRSTname' and namespace-uri()='']" />
           </Agent>
    

    Breaking the XPath down for readability, you can see part where it has [Partycode/@Value='Agent'] is the part that selects the Participant node you want.

    /*[local-name()='Schema' and namespace-uri()='http://Host_service_proj.Inbound_test1']
    /*[local-name()='Participatent' and namespace-uri()='']/*[local-name()='ParticipantDetails' and namespace-uri()='']
    /*[local-name()='Participant' and namespace-uri()=''][Partycode/@Value='Agent']
    /*[local-name()='FIRSTname' and namespace-uri()='']
    

    And just in case you have more than one Participant with the Partycode = Agent, you can tell it to just select the first one.

    /*[local-name()='Schema' and namespace-uri()='http://Host_service_proj.Inbound_test1']
    /*[local-name()='Participatent' and namespace-uri()='']/*[local-name()='ParticipantDetails' and namespace-uri()='']
    /*[local-name()='Participant' and namespace-uri()=''][Partycode/@Value='Agent'][1]
    /*[local-name()='FIRSTname' and namespace-uri()='']