cancel
Showing results for 
Search instead for 
Did you mean: 

Handling null values in context in XSLT mapping for SAP Integration Advisor

Subhadeep
Explorer
0 Kudos
1,510

Hello SAP Experts,

Hope you are doing well.

I wanted your suggestion in solving a problem in handling null values in Contexts in XSLT Mapping in SAP Integration Advisor within SAP Integration Suite.

So, let's give you an idea. The agenda is to populate the corresponding values of a field in the target structure when another field is coming within a segment that repeats N times in a payload. Previously I was using the position () function, but the case does not hold good when empty values are populating in the source payload.

Let us go through this sample payload:

<RECORDS>
  <EMPLOYEE>
     <NAME>Mark</NAME>
     <ID>100</ID>
  </EMPLOYEE>
  <EMPLOYEE>
      <NAME>Daniel</NAME>
      <ID>101</ID>
  </EMPLOYEE>
  <EMPLOYEE>
      <NAME>Chris</NAME>
      <ID>102</ID>
  </EMPLOYEE>
</RECORDS>

So, in this case when I get the payload with say NAME equals Daniel, I want the ID 101 to be populated in the target. The position () function takes the 2nd occurrence of this NAME field and checks the 2nd position of ID field, and populates the 2nd value of ID array, i.e. 101.

However, when in any case the ID field does not appear in 2nd occurrence of EMPLOYEE segment, and it comes as like this:

<RECORDS>
  <EMPLOYEE>
     <NAME>Mark</NAME>
     <ID>100</ID>
  </EMPLOYEE>
  <EMPLOYEE>
      <NAME>Daniel</NAME>
  </EMPLOYEE>
  <EMPLOYEE>
      <NAME>Chris</NAME>
      <ID>102</ID>
  </EMPLOYEE>
</RECORDS

I need to populate a constant value for this ID now, but the array doesnot identify the ID blank element and takes 102 as the 2nd element and populates the same to target field.

Presently the Code I am using is:

<xsl:variable name="NAME" select="$nodes_in/NAME"/>
<xsl:variable name="ID" select="$nodes_in/ID"/>
<xsl:for-each select="$NAME">
<xsl:if test="contains(.,'Daniel')">
    <xsl:variable name="position_qualf" select="position()"/>
    <xsl:value-of select="$ID[$position_qualf]"/>
    </xsl:if>
</xsl:for-each>
Kindly help here with your valuable knowledge and insights.Regards,Subhadeep

Accepted Solutions (0)

Answers (1)

Answers (1)

Robert-Fels
Participant
0 Kudos
Use the following expression to select the ID field for the EMPLOYEE segment that contains the NAME field with the value 'Daniel':
<xsl:variable name="ID" select="../ID"/>
Add an xsl:choose element to check whether the ID field is present or not. If it is present, use the xsl:when element to output the value of the ID field. If it is not present, use the xsl:otherwise element to output a constant value (e.g. 'N/A') as the ID:
<xsl:choose>
<xsl:when test="$ID">
<xsl:value-of select="$ID"/>
</xsl:when>
<xsl:otherwise>
N/A
</xsl:otherwise>
</xsl:choose>
Here's the modified XSLT code:
<xsl:variable name="NAME" select="$nodes_in/NAME"/>
<xsl:for-each select="$NAME">
<xsl:if test="contains(.,'Daniel')">
<xsl:variable name="ID" select="../ID"/>
<xsl:choose>
<xsl:when test="$ID">
<xsl:value-of select="$ID"/>
</xsl:when>
<xsl:otherwise>
N/A
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>

I hope this helps! Let me know if you have any questions.