Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem with nested tables in XSLT transformation (XML to ABAP)

Hi Experts,

I am trying to do deep nesting in a XSLT transformation, but canu2019t get it to work. I also looked into Simple Transformations as an alternative but there the XML would have to be adjusted with the table tags for the loop which is definitely not an option for us.

Here is what I try to achieve, I built a basic example to show what it needs to do:

XML input File:

<?xml version="1.0" encoding="utf-8"?>
<HEADER>
  <PERSON>
    <NAME>Some Name</NAME>
    <ADDRESS>
      <NUMBER>111</NUMBER>
      <STREET>Test Street</STREET>
    </ADDRESS>
  </PERSON>
</HEADER>

The required ABAP output should be an internal table of the following type:

Table column 1: NAME

Table column 2: a table with the following structure:

Nested table column 1: NUMBER

Nested table column 2: STREET

I tried the following with a nested structure rather than a nested table which did work fine.

But once I change the XSLT to accept more than one ADDRESS and change the ABAP data structures to accept a table rather than a structure, the nested table data is just not filled in. Unfortunately I was not able to find any example in the SAP system or on the net that is using a nested table and does work. I also played around with the [Airplus example|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/readdatafromXMLfileviaXSLT+program&] and was able to get that to work, but as soon as I introduce a nested tables on a level below the first level the data for that part is no longer populated.

What am I missing here?

Please find below the coding for the first example with the nested structure and the second example with the nested table (which doesnu2019t work).

ABAP report with nested structure (does work):

REPORT  zwa01_trans.

* Data Definitions --------------------------------------------------
TYPES: BEGIN OF ty_address,
         number TYPE char10,
         street TYPE char40,
       END OF ty_address.

TYPES: BEGIN OF ty_person,
         name    TYPE char20,
         address TYPE ty_address,
       END OF ty_person.

TYPES: BEGIN OF ty_xml_line,
         data(256) TYPE x,
       END OF ty_xml_line.

DATA: gt_in  TYPE TABLE OF ty_xml_line,
      gt_out TYPE TABLE OF ty_person.

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename = 'C:Documents and SettingswackerbauermMy Documents 6 - TEMP	estxml03.xml'
  TABLES
    data_tab = gt_in.

TRY.
    CALL TRANSFORMATION zwa01_test03
      SOURCE XML gt_in
      RESULT xml_output = gt_out.

  CATCH cx_xslt_exception.
    EXIT.
ENDTRY.

BREAK-POINT.

XSLT Program with nested structure (does work):

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:template match="HEADER">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <XML_OUTPUT>
          <PERSON>
            <NAME>
              <xsl:value-of select="PERSON/NAME"/>
            </NAME>
            <ADDRESS>
              <NUMBER>
                <xsl:value-of select="PERSON/ADDRESS/NUMBER"/>
              </NUMBER>
              <STREET>
                <xsl:value-of select="PERSON/ADDRESS/STREET"/>
              </STREET>
            </ADDRESS>
          </PERSON>
        </XML_OUTPUT>
      </asx:values>
    </asx:abap>
  </xsl:template>
</xsl:transform>

XML file (does work):

<?xml version="1.0" encoding="utf-8"?>
<HEADER>
  <PERSON>
    <NAME>Some Name</NAME>
    <ADDRESS>
      <NUMBER>111</NUMBER>
      <STREET>Test Street</STREET>
    </ADDRESS>
  </PERSON>
</HEADER>

Now I tried to expand this into a nested table and changed the XSLT as follows (does not return nested table values):

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:template match="HEADER">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <XML_OUTPUT>
          <PERSON>
            <NAME>
              <xsl:value-of select="PERSON/NAME"/>
            </NAME>
            <xsl:for-each select="ADDRESS">              <<<<INSERT
              <ADDRESS>
                <NUMBER>
                  <xsl:value-of select="PERSON/ADDRESS/NUMBER"/>
                </NUMBER>
                <STREET>
                  <xsl:value-of select="PERSON/ADDRESS/STREET"/>
                </STREET>
              </ADDRESS>
            </xsl:for-each>                              <<<<INSERT
          </PERSON>
        </XML_OUTPUT>
      </asx:values>
    </asx:abap>
  </xsl:template>
</xsl:transform>

After that I adjusted the ABAP code as follows:

REPORT  zwa01_trans.

* Data Definitions --------------------------------------------------
TYPES: BEGIN OF ty_address,
         number TYPE char10,
         street TYPE char40,
       END OF ty_address.

TYPES: BEGIN OF ty_person,
         name    TYPE char20,
         address TYPE ty_address occurs 0,    <<< ADDED OCCURS 0
       END OF ty_person.

This will add a nested table. The XML remained unchanged (It will just create one entry in the nested table). When executing the report now you can see that the table has been nested in the original structure, but is just empty. The curious thing is if I add ADDRESS more than once (e.g.) five times I get five empty lines in the nested table but they are all empty - so it does recognize the repeating tags, but just not transferring the data.

I also tried

address like standard table of gt_address

where gt_address is a data structure of number and street. I even created the structure in the dictionary, but that didnu2019t make a difference either.

I am out of ideas - anybody stumbled accros this and found a solution? I tested it in SAP 4.7 and ECC 6.0 with the same result.

Thank you,

Michael

Tags:

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question