Forum Discussion

Sahil's avatar
Sahil
Contributor
5 years ago
Solved

Write expression for conditional check

Hi,
I want to write this code in expression snaplogic.
I need to map TestID.

<xsl:variable name=“TestID”>
<xsl:if test=“exists(node()/IDOC/A1[VW=‘F’]/NO)”>
<xsl:copy-of select=“node()/IDOC/A1[VW=‘F’]/NO”>
</xsl:copy-of>
</xsl:if>
<xsl:if test=“not(exists(node()/IDOC/A1[VW=‘F’]/NO))”>
<xsl:copy-of select=“node()/IDOC/A1[VW=‘F’]/NP”>
</xsl:copy-of>
</xsl:if>
</xsl:variable>

How do I do it?

  • Hi @Sahil,

    Here are two expressions, I was not sure because it is array does always will have only one VW that contains ‘F’ in the value.

    First expression. Finds all NO or NP that contains ‘F’ in VW field.
    $Data.IDOC.A1.filter(e => e.get('VW').contains('F')).map(x => x.get('NO') != null ? x.NO : x.NP)

    But I think that the second expression is more correct. This expression finds the first one element in the array that contains ‘F’ in VW, and returns NO if exist or else NP.
    $Data.IDOC.A1.find(e => e.get('VW').contains('F')).get('NO', $Data.IDOC.A1.find(e => e.get('VW').contains('F')).get('NP'))

    By default this expression return null, and if you want something else you can add second parameter in the get method for default values.
    Ex: .get('NP', "Default")

    Regards,
    Viktor

10 Replies

  • viktor_n's avatar
    viktor_n
    Contributor II

    Hi @Sahil,

    Here are two expressions, I was not sure because it is array does always will have only one VW that contains ‘F’ in the value.

    First expression. Finds all NO or NP that contains ‘F’ in VW field.
    $Data.IDOC.A1.filter(e => e.get('VW').contains('F')).map(x => x.get('NO') != null ? x.NO : x.NP)

    But I think that the second expression is more correct. This expression finds the first one element in the array that contains ‘F’ in VW, and returns NO if exist or else NP.
    $Data.IDOC.A1.find(e => e.get('VW').contains('F')).get('NO', $Data.IDOC.A1.find(e => e.get('VW').contains('F')).get('NP'))

    By default this expression return null, and if you want something else you can add second parameter in the get method for default values.
    Ex: .get('NP', "Default")

    Regards,
    Viktor

  • pmanchevski's avatar
    pmanchevski
    New Contributor II

    Hello @Sahil

    Did you try to use it XLST transformer snap? It transforms your incoming data by applying the XSL transformation rules.

    You don’t need to map it by hand.

    Regards,
    Pero Manchevski

    • Sahil's avatar
      Sahil
      Contributor

      Hi,
      I am working on a migration project and migrate from SAP platform to snapLogic.
      so it would be better if I do it in expression inside mapper.

    • Sahil's avatar
      Sahil
      Contributor

      hi Viktor,

      I need to translate the whole logic which was done in SAP to snaplogic.

  • viktor_n's avatar
    viktor_n
    Contributor II

    I’ve misunderstood, I see now what you want to do.

    So, I don’t have problem with writing expressions, but I am not good at reading xslt schemas.
    Some of the syntax I understand, but “node()/IDOC/A1[VW=‘F’]/NO)” can you explain this to me. Is this a path ?
    And what are node() and copy-of ?

    • Sahil's avatar
      Sahil
      Contributor

      Hi
      so basically in snaplogic terms it would be:-
      $Data.IDOC.A1[VW=‘F’].NO

    • Sahil's avatar
      Sahil
      Contributor

      “copy-of” will copy the value of the xml element.

  • viktor_n's avatar
    viktor_n
    Contributor II

    You can try something like this.

    $Data.IDOC.A1.get('@VW') == "F" ? $Data.IDOC.A1['@VW'].get('NO', $Data.IDOC.B1['@VW'].NP) : null

    If this won’t work then, you can send me sample data so I can have better understanding of the problem.

    • Sahil's avatar
      Sahil
      Contributor

      hi, it did not work.
      below is the xml:-

      <?xml version="1.0" encoding="UTF-8"?>
      <Data>
      	<IDOC BEGIN="1">
      		<K01 SEGMENT="1">
      			<CURCY>EUR</CURCY>
      			<RECIPNT_NO>4455566</RECIPNT_NO>
      		</K01>
      		<ED SEGMENT="1">
      			<QUALF>014</QUALF>
      			<ORGID>A</ORGID>
      		</ED>
      		<ED SEGMENT="1">
      			<QUALF>009</QUALF>
      			<ORGID>002</ORGID>
      		</ED>
      		<A1 SEGMENT="1">
      			<VW>ZB</VW>
      			<NP>WEE</NP>
      			<NO>55666</NO>
      		</A1>
      		<A1 SEGMENT="1">
      			<VW>GS</VW>
      			<NP>0000070252</NP>
      			<NO>334455</NO>
      		</A1>
      		<A1 SEGMENT="1">
      			<VW>LF</VW>
      			<NP>0000070252</NP>
      			<RAS>E</RAS>
      			<NO>23334455</NO>
      			
      		</A1>
      		<A1 SEGMENT="1">
      			<VW>LS</VW>
      			<NP>0000070252</NP>
      			<NO>55666</NO>
      		</A1>
      	</IDOC>
      </Data>
      

      so in the above example, it should give 23334455 and if <NO> does not exists then 0000070252.