0

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.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
bigB
  • 55
  • 9

1 Answers1

1

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()='']
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
  • Thanks for the response. Above code i tried but it create 2 in destination schema. As i mentioned only one tag should present in the destination schema. If any participant record Partycode is Agent then only Agent tag should present with FNAME. Suppose if no record for Partycode value Agent the tag with no value will generate in output shema – bigB May 18 '23 at 06:17
  • @bigB Oh, I get what you want now. The above will work for both your input scenarios, and even some other scenarios you haven't mentioned – Dijkgraaf May 22 '23 at 04:38
  • Hi related to this i have another query...there is small change in the input schema Field_0 FIRSTname_0 Requirement is same as above can you help me here? If PartyRole value is agent pass to Firstname. – bigB Jun 08 '23 at 10:45
  • @bigB Can you post that as a new question. And if relevant, link back to this question? – Dijkgraaf Jun 08 '23 at 21:42