Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Nested XSLT Transformation from XML to ABAP

SaschaS
Participant
0 Kudos

Hi, I am facing the problem that I am not able to handle nested structures using the XSLT transformation.

The structure is something like

<Positions>  

   <Position>  (0..n)      

      <Schedules>
          
         Schedule   (0..n)

I am able to use a list of "Position" elements and to put one "Schedule" element into an structure. The goal is to have the table of schedule-elements within the table of position-elements. But this is not working. He is not able to populate the inner table.

method xml_conv.

    types:
      begin of t_schedpos,
        ds_no               type string,
        ds_quantity         type string,
      end of t_schedpos.


    types: t_deliveryschedules type standard table of t_schedpos with NON-UNIQUE KEY ds_no.

    types: begin of t_pos,
             line_no          type string,
             total            type string,
"             deliveryschedule type t_schedpos ,   "works as structure
             deliveryschedule type t_deliveryschedules,
           end of t_pos.

    types: begin of document,
             positions type table of t_pos with default key,
           end of document.


    data: ls_document type document.
    data xslt_error type ref to cx_xslt_exception.
    data lo_st_error type ref to cx_st_error.
    data xslt_message type string.


    data: lv_xml type string.


    concatenate
    '<?xml version="1.0" encoding="UTF-8"?> '
   '<Document>'
   '<OCPositions>'
   '  <OCPosition>'
   '   <OCPLineNo>1</OCPLineNo>'
   '   <OCPTotal>3890.4</OCPTotal>'
   '   <OCPDeliverySchedules>'
   '     <OCPDeliverySchedule>'
   '       <OCPDeliveryScheduleNo>0</OCPDeliveryScheduleNo>'
   '       <OCPQuantity>10</OCPQuantity>'
   '     </OCPDeliverySchedule>'
   '     <OCPDeliverySchedule>'
   '       <OCPDeliveryScheduleNo>1</OCPDeliveryScheduleNo>'
   '       <OCPQuantity>10</OCPQuantity>'
   '     </OCPDeliverySchedule>'
   '   </OCPDeliverySchedules>'
   '  </OCPosition>'
   '</OCPositions>'
   '</Document>'
   into lv_xml.

    try.
        call transformation zinw_order_conf
              source
                xml lv_xml
              result document = ls_document.
    endtry.
 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:variable name="head" select="Document/Header"/>
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <DOCUMENT>
 
          <xsl:variable name="positions" select="Document/OCPositions"/>
          <POSITIONS>


            <xsl:for-each select="$positions/OCPosition">
              <OCPosition>
                <LINE_NO>
                  <xsl:value-of select="OCPLineNo"/>
                </LINE_NO>
                <TOTAL>
                  <xsl:value-of select="OCPTotal"/>
                </TOTAL>
      
                <xsl:for-each select="OCPDeliverySchedules/OCPDeliverySchedule">
                  <DELIVERYSCHEDULE>
                    <DS_NO>
                      <xsl:value-of select="OCPDeliveryScheduleNo"/>
                    </DS_NO>
                    <DS_QUANTITY>
                      <xsl:value-of select="OCPQuantity"/>
                    </DS_QUANTITY>
                  </DELIVERYSCHEDULE>
                </xsl:for-each>

              </OCPosition>
            </xsl:for-each>
          </POSITIONS>

        </DOCUMENT>
      </asx:values>
    </asx:abap>
  </xsl:template>
</xsl:transform>

Can someone tell me how to use the nested tables / structures in the correct way?

Regards,

Sascha

1 ACCEPTED SOLUTION

DoanManhQuynh
Active Contributor

you have to move tag <DELIVERYSCHEDULE> outside of for-each (it is table component) and wrap the data inside other tag. like:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<DOCUMENT>
<POSITIONS>
<xsl:for-each select="Document/OCPositions/OCPosition">
<OCPosition>
<LINE_NO>
<xsl:value-of select="OCPLineNo"/>
</LINE_NO>
<TOTAL>
<xsl:value-of select="OCPTotal"/>
</TOTAL>

<DELIVERYSCHEDULE>
<xsl:for-each select="OCPDeliverySchedules/OCPDeliverySchedule">
<OCPDeliverySchedule>
<DS_NO>
<xsl:value-of select="OCPDeliveryScheduleNo"/>
</DS_NO>
<DS_QUANTITY>
<xsl:value-of select="OCPQuantity"/>
</DS_QUANTITY>
</OCPDeliverySchedule>
</xsl:for-each>
</DELIVERYSCHEDULE>
</OCPosition>
</xsl:for-each>
</POSITIONS>
</DOCUMENT>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
4 REPLIES 4

DoanManhQuynh
Active Contributor

you have to move tag <DELIVERYSCHEDULE> outside of for-each (it is table component) and wrap the data inside other tag. like:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<DOCUMENT>
<POSITIONS>
<xsl:for-each select="Document/OCPositions/OCPosition">
<OCPosition>
<LINE_NO>
<xsl:value-of select="OCPLineNo"/>
</LINE_NO>
<TOTAL>
<xsl:value-of select="OCPTotal"/>
</TOTAL>

<DELIVERYSCHEDULE>
<xsl:for-each select="OCPDeliverySchedules/OCPDeliverySchedule">
<OCPDeliverySchedule>
<DS_NO>
<xsl:value-of select="OCPDeliveryScheduleNo"/>
</DS_NO>
<DS_QUANTITY>
<xsl:value-of select="OCPQuantity"/>
</DS_QUANTITY>
</OCPDeliverySchedule>
</xsl:for-each>
</DELIVERYSCHEDULE>
</OCPosition>
</xsl:for-each>
</POSITIONS>
</DOCUMENT>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>

0 Kudos

Hi @Quynh Doan Manh, thanks a lot for your answers. You are completely right. You made my day. Regards, Sascha

0 Kudos

It can be anything at the place of <OCPDeliverySchedule>, I would recommend to use <item> instead, because it's the default name used by SAP serialization... just to not mix ABAP names and XML names in the XSLT.

0 Kudos

yes i were going to use <item> there but for it easy to explain i used his xml file name :). anw, its recommend to use <item> instead.