Skip to end of metadata
Go to start of metadata

Summary: Transforming complex webdynpro tree structure to multiple tabes using XSLT with the perservation of data relationship

Table is the most common data container in SAP world.  There may be a situation that you need to convert the data from its original structure to the table structure in order to continue the program logic.  One scenario is to convert a tree structure to a table structure or even more than one table.  The conversion can be done by using XSLT.  It depends on the complexities of the tree and your goals, the corresponding transformation will be different.  Consider the following two scenarios:

1. Simple tree structure.  This example can be seen in the link http://www.dpawson.co.uk/xsl/sect2/flatten.html

<Node name="aaa">
            <Node name= "bbb">
                        <Node name="ccc"/>
            </Node>
</Node>

The transformation of this simple structure can easily be achieved by using the <xsl:apply-template/> approach.

2. Complex tree structure. 

<context>
   <prod_tree>
       <item>
          <prod_tree_content>
              <item>...</item>
              <item> ...</item>
          </prod_tree_context>
          <prod_node_text>node_text01</prod_node_text>
          <children_loaded>1</children_loaded>
          <prod_node_id>00001</prod_node_id>
       </item>
   </prod_tree>
</context>

One example of this type of tree structure is obtained from the webdynpro tree structure.  The <item> below the <prod_tree_context> will repeat the same structure as the <item> below the <prod_tree>.  This is a type of recursive tree, with "sibling" nodes below the <item>.  The goals are to convert the tree to multiple tables and capture the relationship of parent and children as well.

When using the same approach as the first one, this is, using the <xsl:apply-template/>, redundant texts created out of those siblings nodes are appended at the end of each recursive call and leads to an unacceptable result.  Therefore, an alternative approach is required.

The following is an XSLT example to achieve transforming this complex tree structure into multiple tables and the parent-children relationship is preserved in the first table.

<xsl:template name="myID">
   <PROD_NODE_ID>
       <xsl:value-of select="PROD_NODE_ID"/>
   </PROD_NODE_ID>
   <PARENT_PROD_NODE_ID>
       <xsl:value-of select="../../PROD_NODE_ID"/>
   </PARENT_PROD_NODE_ID>
</xsl:template>

<xsl:template name="myText">
   <PROD_NODE_ID>
       <xsl:value-of select="PROD_NODE_ID"/>
   </PROD_NODE_ID>
   <PROD_NODE_TEXT>
       <xsl:value-of select="PROD_NODE_TEXT"/>
   </PROD_NODE_TEXT>
</xsl:template>

<xsl:template match="context/PROD_TREE">
   <First_table>
      <xsl:for-each select="//item">
         <ITEM>
             <xsl:call-template name="myID"></xsl:call-template>
         </ITEM>
      </xsl:for-each>
   </First_table>

   <Second_table>
      <xsl:for-each select="//item">
         <ITEM>
             <xsl:call-template name="myText"></xsl:call-template>
         </ITEM>
      </xsl:for-each>
   </Second_table>
</xsl:template>

The result tables of this transformation are the following:

<First_table>
   <ITEM>
       <PROD_NODE_ID>00001</PROD_NODE_ID>
       <PARENT_PROD_NODE_ID></PARENT_PROD_NODE_ID>
   </ITEM>
   <ITEM>
       <PROD_NODE_ID>00002 </PROD_NODE_ID>
       <PARENT_PROD_NODE_ID>00001</PARENT_PROD_NODE_ID>
   </ITEM>
</First_table>
<Second_table>
   <ITEM>
       <PROD_NODE_ID>00001 </PROD_NODE_ID>
       <PROD_NODE_TEXT>node_text01</PROD_NODE_TEXT>
   </ITEM>
   <ITEM>
       <PROD_NODE_ID>00002</PROD_NODE_ID>
       <PROD_NODE_TEXT>node_text02</PROD_NODE_TEXT>
   </ITEM>
</Second_table>

After we obtain the result of transformation, another simple ABAP transformation can convert these data to tables instantly.