Search code examples
xmlif-statementxsltsap-cpi

update value from different xml node using XSLT if in SAP CPI


I have some trouble for update value using if on XSLT from other reference element.

  • D_3035 field value SU should be available and populated.
  • D_3055 field value should be 92 from 9.

But all other BY/CA/ST on D3035 was impact and changed all value 92 on D_3055

Would you be able to provide XSLT code review and let us know?

  1. Source XML

     <G_SG2>
          <S_NAD>
             <D_3035>CA</D_3035>
             <C_C082>
                <D_3039>MYCARRIER</D_3039>
                <D_3055>92</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>BY</D_3035>
             <C_C082>
                <D_3039>0000152055</D_3039>
                <D_3055>9</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>ST</D_3035>
             <C_C082>
                <D_3039>0000152055</D_3039>
                <D_3055>9</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>SU</D_3035>
             <C_C082>
                <D_3039>3000</D_3039>
                <D_3055>9</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
    
  2. XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" indent="yes"/>
    
     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>
    
    <xsl:variable name="varD_3035" select="//D_3035"/>
    <xsl:variable name="varD_3055" select="//D_3055"/>
    
    <xsl:template match="D_3055">
    <D_3055>
            <xsl:value-of select= "if( $varD_3035 = 'SU' ) then '92'  else $varD_3055 "/>
    </D_3055>
    </xsl:template>
    </xsl:stylesheet>
    
  3. incorrect xml

    • all 92 was populated on D_3035
       <G_SG2>
          <S_NAD>
             <D_3035>CA</D_3035>
             <C_C082>
                <D_3039>MYCARRIER</D_3039>
                <D_3055>92</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>BY</D_3035>
             <C_C082>
                <D_3039>0000152055</D_3039>
                <D_3055>92</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>ST</D_3035>
             <C_C082>
                <D_3039>0000152055</D_3039>
                <D_3055>92</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
       <G_SG2>
          <S_NAD>
             <D_3035>SU</D_3035>
             <C_C082>
                <D_3039>3000</D_3039>
                <D_3055>92</D_3055>
             </C_C082>
          </S_NAD>
       </G_SG2>
    

I run XSLT several times but it is same.

I am expecting XML as below:

   <G_SG2>
      <S_NAD>
         <D_3035>CA</D_3035>
         <C_C082>
            <D_3039>MYCARRIER</D_3039>
            <D_3055>9</D_3055>
         </C_C082>
      </S_NAD>
   </G_SG2>
   <G_SG2>
      <S_NAD>
         <D_3035>BY</D_3035>
         <C_C082>
            <D_3039>0000152055</D_3039>
            <D_3055>9</D_3055>
         </C_C082>
      </S_NAD>
   </G_SG2>
   <G_SG2>
      <S_NAD>
         <D_3035>ST</D_3035>
         <C_C082>
            <D_3039>0000152055</D_3039>
            <D_3055>9</D_3055>
         </C_C082>
      </S_NAD>
   </G_SG2>
   <G_SG2>
      <S_NAD>
         <D_3035>SU</D_3035>
         <C_C082>
            <D_3039>3000</D_3039>
            <D_3055>92</D_3055>
         </C_C082>
      </S_NAD>
   </G_SG2>

Solution

  • It seems

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>
    
    
    <xsl:template match="S_NAD[D_3035 = 'SU']//D_3055">
      <xsl:copy>92</xsl:copy>
    </xsl:template>
    

    might express your requirement (although the formulation is not clear to me).

    And I don't know why, in the wanted result, you have

     <S_NAD>
         <D_3035>CA</D_3035>
         <C_C082>
            <D_3039>MYCARRIER</D_3039>
            <D_3055>9</D_3055>
         </C_C082>
      </S_NAD>
    

    as the input has

      <S_NAD>
         <D_3035>CA</D_3035>
         <C_C082>
            <D_3039>MYCARRIER</D_3039>
            <D_3055>92</D_3055>
         </C_C082>
      </S_NAD>