Skip to Content
-2

XML file to Internal Table conversion - XSLT Code

Dear All.

please find my input xml file.

we would like to convert this xml as an internal table to store the values to Ztables.

t3.png

Customer doesn't have a SAP PI resource, so we decided to do this scenario like, XML XSLT with ABAP. and i found an refrence link.

XML XSLT with ABAP

SIMPLE XML PROCESSING IN ABAP

using XSLT_TOOL if new for us, is there a way where we can get the XSLT code for the same. can anyone can help us to get the XSLT code.

i can see using "simple transformation" in XSLT_TOOL we can get the code automatically, but i don't think above xml structure may suites or not?

please do needful.

please find the attached xml file for your reference.pain-002-001-03-filelevelacknowledgement-sample.txt

Add comment
10|10000 characters needed characters exceeded

  • Deal Al.

    we've created an XSLT code, and while debugging in below abap code

    CALL TRANSFORMATION ztest0001
     SOURCE XML gt_itab "l_xml_x1
     RESULT xml_output = it_airplus
     .
    * catch any error, very helpful if the XSLT isn't correct
     CATCH cx_xslt_exception INTO xslt_err.

    we are facing below issue/exception.

    ëxception:
    Transformation error:  The element {http://www.sap.com/abapxml#}abap was expected for the XML-ABAP transformation
    

    input xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <CstmrPmtStsRpt>
        <GrpHdr>
          <MsgId>20120504-PSR/33</MsgId>
          <CreDtTm>2012-05-04T22:34:16</CreDtTm>
          <InitgPty>
            <Id>
              <OrgId>
                <BICOrBEI>CITIUS33</BICOrBEI>
              </OrgId>
            </Id>
          </InitgPty>
        </GrpHdr>
        <OrgnlGrpInfAndSts>
          <OrgnlMsgId>V3C-4-PVT-Messageid</OrgnlMsgId>
          <OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId>
          <OrgnlCreDtTm>2012-05-04T14:07:01</OrgnlCreDtTm>
          <OrgnlNbOfTxs>75</OrgnlNbOfTxs>
          <OrgnlCtrlSum>75</OrgnlCtrlSum>
          <GrpSts>PART</GrpSts>
          <StsRsnInf>
            <AddtlInf>ACK - FILE PARTIALLY SUCCESSFUL</AddtlInf>
          </StsRsnInf>
        </OrgnlGrpInfAndSts>
      </CstmrPmtStsRpt>
    </Document>
    

    XSLT Code;

    <xsl:transform version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:sap="http://www.sap.com/sapxsl">
    
    
    <xsl:strip-space elements="*"/>
    
    
    <xsl:template match="Document">
    		<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
    			<asx:values>
    				<XML_OUTPUT>
    					<xsl:for-each select="CstmrPmtStsRpt">
    						<CstmrPmtStsRpt>
    							<HEAD>
    								<xsl:variable name="head" select="GrpHdr" />
    								<MSGID>
    									<xsl:value-of select="$head/MsgId" />
    								</MSGID>
    								<CREDTTM>
    									<xsl:value-of select="$head/CreDtTm" />
    								</CREDTTM>
    							</HEAD>
    							<DETAILS>
    								<xsl:for-each select="OrgnlGrpInfAndSts">
    									<xsl:variable name="OrgnlGrpInfAndSts" select="." />
    									<OrgnlGrpInfAndSts>
    										<ORGNLMSGID>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/OrgnlMsgId"/>
    										</ORGNLMSGID>
    										<ORGNLMSGNMID>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/OrgnlMsgNmId"/>
    										</ORGNLMSGNMID>
    										<ORGNLCREDTTM>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/OrgnlCreDtTm"/>
    										</ORGNLCREDTTM>
    										<ORGNLNBOFTXS>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/OrgnlNbOfTxs"/>
    										</ORGNLNBOFTXS>
    										<ORGNLCTRLSUM>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/OrgnlCtrlSum"/>
    										</ORGNLCTRLSUM>
    										<GRPSTS>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/GrpSts"/>
    										</GRPSTS>
    										<ADDTLINF>
    											<xsl:value-of select="$OrgnlGrpInfAndSts/AddtlInf"/>
    										</ADDTLINF>
    									</OrgnlGrpInfAndSts>
    								</xsl:for-each>
    							</DETAILS>
    						</CstmrPmtStsRpt>
    					</xsl:for-each>
    				</XML_OUTPUT>
    			</asx:values>	
    		</asx:abap>
    	</xsl:template>
    </xsl:transform>
    
    

    ABAP Code:

    REPORT  zrpt_xslt_demo.
    
    
    TYPE-POOLS: abap, ixml.
    
    
    TYPES: BEGIN OF t_ap_head,
            msgid(20),
            credttm(10),
            END OF t_ap_head.
    
    
    TYPES: BEGIN OF t_ap_detail,
        credttm TYPE zcredttm,
      bicorbei TYPE zbicorbei,
      orgnlmsgid TYPE zorgnlmsgid,
      orgnlmsgnmid TYPE zorgnlmsgnmid,
      orgnlcredttm   TYPE zorgnlcredttm,
      orgnlnboftxs TYPE zorgnlnboftxs,
      orgnlctrlsum TYPE zorgnlctrlsum,
      grpsts TYPE zgrpsts,
      addtlinf TYPE zaddtlinf,
           END OF t_ap_detail.
    
    
    TYPES: BEGIN OF t_ap_details,
            inv_detail TYPE t_ap_detail,
           END OF t_ap_details.
    
    
    TYPES: BEGIN OF t_ap,
            head    TYPE t_ap_head,
            details TYPE t_ap_details,
           END OF t_ap.
    
    
    DATA: xslt_err   TYPE REF TO cx_xslt_exception,
          err_string TYPE string.
    
    
    PARAMETERS: pa_file TYPE string DEFAULT 'C:\DB\demo_xslt.xml'.
    
    
    DATA: gt_itab       TYPE STANDARD TABLE OF char2048.
    DATA: it_airplus    TYPE STANDARD TABLE OF t_ap,
          wa_airplus    TYPE t_ap.
    
    
    
    
    START-OF-SELECTION.
    
    
      CALL METHOD cl_gui_frontend_services=>gui_upload
        EXPORTING
          filename                = pa_file
        CHANGING
          data_tab                = gt_itab
        EXCEPTIONS
          file_open_error         = 1
          file_read_error         = 2
          no_batch                = 3
          gui_refuse_filetransfer = 4
          invalid_type            = 5
          no_authority            = 6
          unknown_error           = 7
          bad_data_format         = 8
          header_not_allowed      = 9
          separator_not_allowed   = 10
          header_too_long         = 11
          unknown_dp_error        = 12
          access_denied           = 13
          dp_out_of_memory        = 14
          disk_full               = 15
          dp_timeout              = 16
          not_supported_by_gui    = 17
          error_no_gui            = 18
          OTHERS                  = 19.
    
    
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    
    
      TRY.
          CALL TRANSFORMATION ztest0001
            SOURCE XML gt_itab                                  "l_xml_x1
            RESULT xml_output = it_airplus
            .
    * catch any error, very helpful if the XSLT isn't correct
        CATCH cx_xslt_exception INTO xslt_err.
    
    
          err_string = xslt_err->get_text( ).
          WRITE: / 'Transformation error: ', err_string.
          EXIT.
      ENDTRY.
    * SETTING A BREAKPOINT TO WATCH THE WORKAREA
    * by the internal table "it_airplus"
      BREAK-POINT.
    
    
      LOOP AT it_airplus INTO wa_airplus.
      ENDLOOP.
    

    can anyone please suggest/correct my XSLT code.

Further commenting has been locked.
  • Get RSS Feed

5 Answers

  • Best Answer
    Feb 27, 2017 at 08:51 AM

    There are different ways to parse XML files into ABAP.

    Libraries: iXML or sXML

    Transformations: XSLT or ST.

    Make yourself acquainted.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 27, 2017 at 08:43 AM

    The XSLT (transformation) need to created to process the input file. You can refer to the wiki below XML XSLT with ABAP

    Regards,

    Add comment
    10|10000 characters needed characters exceeded

    Further commenting has been locked.
  • Feb 27, 2017 at 09:16 AM

    Hi,

    There is many ways to do that. But can you tell us, whether you are using SAP PI or not or direct send this to ECC (ABAP System)?

    Regards,

    Anoop Rai

    Add comment
    10|10000 characters needed characters exceeded

    Further commenting has been locked.
  • avatar image
    Former Member
    Mar 09, 2017 at 02:11 PM

    try creating transformation using the XSLT guide

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 10, 2017 at 06:43 AM

    Hi Ramesh,

    Why to use a XSLT transformation?, your XML seems to be a simpler example, you can access directly to the header part and later in a LOOP to parse the body. For ABAPer is not difficult to do it, check this example

    https://wiki.scn.sap.com/wiki/display/ABAP/Parse+an+xml+file+and+insert+values+in+ABAP

    Regards.

    Add comment
    10|10000 characters needed characters exceeded