Skip to Content

Introduction

There have been multiple threads cropping up in the forum lately regarding FCC conversion for deep/nested structures. Unfortunately, the File Content Conversion (FCC) functionality (whether directly in the File/FTP adapter or through MessageTransformBean module) has not changed significantly for nearly 10 years since the days of XI 3.0 We are still stuck with standard FCC functionality that is only able to handle flat structures – all record-types/segment can only be at the same single level. However, in reality not all flat file structures conform to this “expected” pattern. We would have expected SAP to enhance the capabilities of the standard FCC functionality to cater for these commonly used deep structures, but that does not seem to be the case.

Therefore, all these years, there have been a myriad of custom solutions/workarounds to handle this. Some custom approaches are listed below:

In general, my preferred choice for custom development goes in the following order – Graphical mapping, Java mapping, XLST, adapter module. However there are certain areas where XSLT excels in, achieving robust transformation with minimal coding. Some of these are sorting XML and performing partial identity (1-1) transformation. The combination of the copy and apply-templates command allows for identity transformation without the need to individually specify field names.

In this two-part series, I will share the approach based on XSLT that was used in one of my previous projects to handle creation and parsing of deeply structured flat files. I have since tweaked the XSLT codes so that it is able to be used in any scenario with minimal changes to the code. Here are the scenarios that will be covered in each part of this series.

Source Code and Explanation

The full source code, example input and output files are available in the following GitHub repository

GitHub repository for DeepFCC

The logic comprises of three XSLT template match sections.

1. Flatten segments with subsegments

This template uses *[*] to match any nodes that have subsegments (subnodes with child fields.) Once such a segment is matched, the immediate child fields are copied over 1-1, while the subsegments are brought up to the same level (flattened.) The logic then continues recursively.


<!-- Match any segment with child segments/fields -->
<xsl:template match="*[*]">
  <!-- (1) - Copy the current segment, then select attributes and child elements that are fields -->
  <xsl:copy>
   <xsl:apply-templates select="@* | *[not(*)]"/>
  </xsl:copy>
  <!-- (2) - Further select child elements that are segments -->
  <xsl:apply-templates select="*[*]"/>
</xsl:template>








The following sample input and output shows that the subsegments are brought to the same level in the hierarchy as the parent segment.

Input Output

<Level1>

   <Field1>A</Field1>

   <Field2>B</Field2>

   <Level2>

     <Field3>C</Field3>

     <Field4>D</Field4>

     <Field5>E</Field5>

   </Level2>

</Level1>

<Level1>

   <Field1>A</Field1>

   <Field2>B</Field2>

</Level1>

<Level2>

   <Field3>C</Field3>

   <Field4>D</Field4>

   <Field5>E</Field5>

</Level2>

2. Identity transformation for root element

This template matches the root element of the XML document and copies it 1-1 to the output.

Note: To reuse this XSLT code, the Root element need to be changed according to the root element name of the source input XML.


<!-- Match root element, copy details and and select child attributes and nodes -->
<xsl:template match="*[local-name()='MT_Deep']">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>








3. Identity transformation for all other attributes and nodes

This template is similar to the one above and does a 1-1 transformation for all other attributes/nodes.


<!-- Match all other attributes (@*) and other node types (elements, comments) -->
<!-- Copy the current element, and select child attributes and nodes -->
<xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>








Note: In theory templates 2 and 3 can be combined into one by specifying an additional /* match for the root element. However, such logic does not provide the intended result when being executed on PI’s mapping runtime, although it works on Online XSLT Test Tool and the XML-Tools plugin of Notepad++. It will also work if usage of the SAP XML Toolkit is specified in the Operation Mapping, however this goes against SAP’s recommendation as documented in the following SAP Library link – XSLT Mapping – SAP Library.

I am unsure of the reason for the behavioral difference when it is executed on the XSLT parser of the JDK. Otherwise, the XSLT code would have been a generic logic that could have been reused in any transformation without any modification.

Example Scenario

Here is a sample scenario where the requirement is to achieve the creation of a flat file with deep/nested structures.

The output file will be a shipment file which contains all the details for the shipment. Each shipment file can have multiple deliveries, each delivery in turn can have multiple orders, and each order multiple items. Each segment needs to occur under the parent segment, therefore indicating a deep/nested structure.

Segment Name Parent Segment Type Indicator Occurrence
Header H 1
Delivery D 1 – unbounded
Order Delivery O 1 – unbounded
OrderText Order T 0 – 1
Item Order I 1 – unbounded
Footer F 1

Design

This article will only focus on the sections that are required to enable this XSLT approach, therefore common steps for design and configuration will not be covered.

Target structure definition

The data type for the target structure will be defined as a deep XML schema. No flat DT definition that is required for FCC needs to be defined. Below is the sample deep XML schema representation of the output format.

/wp-content/uploads/2015/02/dt_deep_643432.png

Message Mapping

Create a normal graphical message mapping to map the source structure to the deep target structure above.

Import XSLT code

Zip the XSL file and import it to ESR as an Imported Archive.

Operation Mapping

Typically the XSLT mapping will be used as the second step of the Operation Mapping. The first step will be the message mapping to map the source to target. Then the XSLT will flatten the final output.

Step Mapping Type Name
1 Message Mapping <Message Mapping object created above>
2 XSL Deep2Flat

Below is a sample execution of the second step of the OM to flatten the deep structure in preparation for FCC. The result shows that the XSLT have flattened all the segments to the same level.

Note: The output of the XSLT mapping step is not a valid XML document when validated against the original data type definition, but it does not matter as the main purpose of the output is as an input to the FCC at the receiver channel.

/wp-content/uploads/2015/02/om_deep2flat_643457.png

Configuration

The normal configuration steps are performed. At the receiver channel, specify the FCC parameters. The following screenshot shows the FCC parameters using MessageTransformBean on an SFTP channel.

/wp-content/uploads/2015/02/mtb_643458.png

Testing Results

After execution of the interface, we can see from the after FCC log version that the payload has been converted into a flat file format with deep structure.

/wp-content/uploads/2015/02/outputfcc_643469.png

Similarly, when the CSV file is viewed in Excel, we can see that all the subsegments are created in the correct order underneath their corresponding parent segments.

/wp-content/uploads/2015/02/csv_643470.png

Conclusion

With a relatively simple XSLT logic, we can still achieve creation of flat file with deep structure whilst still using the standard FCC functionality. The benefits of this XSLT approach are:

  • Easily reusable for other interfaces – just need to change the name of the root element
  • The XSLT logic works irrespective of the number of levels of the deep structure
  • No intermediate structure needs to be defined for flat XML representation of the file – this is required when using the Graphical mapping approach
  • No changes required if fields of a segment are changed, i.e new fields, deleted fields, modified field name

In the continuation of this series, part 2 covers handling of deep structure FCC at the sender side.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Midhun Madhav

    Hi

    I have an IDoc to File scenario. In receiver File adapter, I use FCC. It has five or six levels of hierarchies. Can this be used here? When I use this , do I still have to use FCC

    Regards

    (0) 
    1. Eng Swee Yeoh Post author

      Hi Midhun

      Yes, this can be used for multiple levels of hierarchy, and you still have to use the standard FCC on the receiver channel.

      Rgds

      Eng Swee

      (0) 
  2. Srikanth Mavuri

    Hi All,

    This blog was very nice and useful.

    Can any one explain the execution sequence of this XSLT Mapping Program.

    and could you please explain the concept of Message Transform Bean and other Bean types also if there.

    Thanks in Advance……

    Regards,

    Srikanth Mavuri.

    (0) 

Leave a Reply