Skip to Content
0

Conversion of XML into ABAP (XSLT transformation).Error(Invalid XML format)

Mar 20, 2017 at 08:08 AM

570

avatar image
Former Member

hi all,

i am using call transformation to convert XML to abap internal table and getting the error that the Input XML is in invalid format.

when open my input payload XML in notepad i realized that all the XML tags is coming in one line and if i arrange the XML file manually by putting each tag in separate line and passing saved XML to call transformation then i got output.

please suggest if there is any solution where i can pass the original XML payload to call transformation without doing any change manually.

beloq is ABAP code.

 GET REFERENCE OF IT_FINAL INTO GS_RESULT_XML-VALUE.
  GS_RESULT_XML-NAME = 'CSTMRPMTSTSRPT_GET'.
  APPEND GS_RESULT_XML TO GT_RESULT_XML.
* Perform the XSLT stylesheetv3filelevel-ack-accept-sample.txt
  TRY.
      CALL TRANSFORMATION ZXSLT_TEST
      SOURCE XML GT_ITAB
      RESULT (GT_RESULT_XML).
   CATCH CX_ROOT INTO GS_RIF_EX.
      GS_VAR_TEXT = GS_RIF_EX->GET_TEXT( ).
      MESSAGE GS_VAR_TEXT TYPE 'E'.
  ENDTRY.

I am attaching input XML payload also.

please do the needful.

Thanks,

Atique

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

avatar image
Former Member Mar 20, 2017 at 09:51 AM
0

Problem is somewhere else. I am able to process your file successfully. Re-check the contents of GT_ITAB for both good and bad cases.

Try running below code after correcting the file path.

DATA lt_xml TYPE stringtab.
cl_gui_frontend_services=>gui_upload(
  EXPORTING
    filename                = 'D:\tempp\test.xml'
  CHANGING
    data_tab                = lt_xml    " Transfer table for file contents
  EXCEPTIONS
    OTHERS                  = 1
).

DATA lv_xml TYPE string.
CONCATENATE LINES OF lt_xml INTO lv_xml.

DATA lv_xml_out TYPE string.
CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml_out.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

thanks for your quick reply Nakli

Actually requirement is like to convert XML data into ABAP internal table using XSLT call transformation.

My report is working fine if i pass the modified input payload i.c. arranging the XML file manually by putting each tag in separate line.

below is my abap code:

REPORT ZRPT_XML_ABAP.
TYPE-POOLS: ABAP, IXML.
TYPES: BEGIN OF  GRPHDR,
         MSGID   TYPE CHAR50,
         CREDTTM TYPE CHAR20,
       END OF GRPHDR.

TYPES: BEGIN OF ADDTLINF,
         ADDTLINF(50),
       END Of ADDTLINF.

TYPES: BEGIN OF  STSRSNINF,
         ADDTLINF TYPE STANDARD TABLE OF STRING WITH NON-UNIQUE DEFAULT KEY,
       END OF  STSRSNINF.
TYPES: BEGIN OF  ORGNLGRPINFANDSTS,
         ORGNLMSGID   TYPE CHAR50,
         ORGNLMSGNMID TYPE CHAR20,
         ORGNLCREDTTM TYPE CHAR25,
         ORGNLNBOFTXS TYPE NUMC10,
         ORGNLCTRLSUM TYPE P DECIMALS 3,
         GRPSTS       TYPE CHAR20,
         STSRSNINF    TYPE STSRSNINF,
       END OF  ORGNLGRPINFANDSTS.

TYPES: BEGIN OF CSTMRPMTSTSRPT,
         GRPHDR            TYPE GRPHDR,
         ORGNLGRPINFANDSTS TYPE ORGNLGRPINFANDSTS,
       END OF CSTMRPMTSTSRPT.

TYPES: BEGIN OF TY_FINAL,
*      DOCUMENT TYPE ZDOCUMENT,     "ZCSTMRPMTSTSRPT,
         CSTMRPMTSTSRPT TYPE CSTMRPMTSTSRPT,
       END OF  TY_FINAL.
DATA: IT_FINAL TYPE STANDARD TABLE OF TY_FINAL,
      WA_FINAL TYPE                   TY_FINAL.

* Result table that contains references
* of the internal tables to be filled
DATA: GT_RESULT_XML TYPE ABAP_TRANS_RESBIND_TAB,
      GS_RESULT_XML TYPE ABAP_TRANS_RESBIND.

* For error handling
DATA: GS_RIF_EX   TYPE REF TO CX_ROOT,
      GS_VAR_TEXT TYPE STRING.
DATA: GT_ITAB TYPE STANDARD TABLE OF CHAR2048,   " WITH HEADER LINE.
      GS_ITAB TYPE CHAR2048.

DATA: DUMMY(1) TYPE C,
      TITLE(7) TYPE C.

PARAMETERS GS_FILE TYPE STRING.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR GS_FILE.
  TITLE = 'Dataset'(c14).
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      DEF_FILENAME     = SPACE
      DEF_PATH         = GS_FILE
      MASK             = ',*.*.'
      MODE             = 'O'
      TITLE            = TITLE
    IMPORTING
      FILENAME         = GS_FILE
      RC               = DUMMY
    EXCEPTIONS
        NO_BATCH         = 08
      SELECTION_CANCEL = 12
      SELECTION_ERROR  = 16.

START-OF-SELECTION.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
    EXPORTING
      FILENAME                = GS_FILE
    CHANGING
      DATA_TAB                = GT_ITAB
    EXCEPTIONS
      FILE_OPEN_ERROR         = 1
      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.

*deleting the document tag from XML
  LOOP AT GT_ITAB INTO GS_ITAB.
    SEARCH GS_ITAB FOR 'DOCUMENT'.
    IF SY-SUBRC EQ 0.
      DELETE GT_ITAB.
    ENDIF.
  ENDLOOP.

* Fill the result table with a reference to the data table.
* Within the XSLT style sheet, the data table can be accessed with
* "CSTMRPMTSTSRPT_GET".
  GET REFERENCE OF IT_FINAL INTO GS_RESULT_XML-VALUE.
  GS_RESULT_XML-NAME = 'CSTMRPMTSTSRPT_GET'.
  APPEND GS_RESULT_XML TO GT_RESULT_XML.

* Perform the XSLT stylesheet
  TRY.
      CALL TRANSFORMATION ZXSLT_TEST
      SOURCE XML GT_ITAB
      RESULT (GT_RESULT_XML).
    CATCH CX_ROOT INTO GS_RIF_EX.
      GS_VAR_TEXT = GS_RIF_EX->GET_TEXT( ).
      MESSAGE GS_VAR_TEXT TYPE 'E'.
  ENDTRY.

    LOOP AT IT_FINAL INTO WA_FINAL.
     endloop.
  ENDIF.

XSLT code:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <CSTMRPMTSTSRPT_GET>
          <xsl:apply-templates select="//CSTMRPMTSTSRPT"/>
        </CSTMRPMTSTSRPT_GET>
      </asx:values>
    </asx:abap>
  </xsl:template>
  <xsl:template match="CSTMRPMTSTSRPT">
    <item>
      <CSTMRPMTSTSRPT>
        <GRPHDR>
          <MSGID>
            <xsl:value-of select="GRPHDR/MSGID"/>
          </MSGID>
          <CREDTTM>
            <xsl:value-of select="GRPHDR/CREDTTM"/>
          </CREDTTM>
        </GRPHDR>
        <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>
          <STSRSNINF>
             <ADDTLINF>
                <xsl:for-each select="ORGNLGRPINFANDSTS/STSRSNINF/ADDTLINF">
                   <item>
                  <xsl:value-of select="."/>
                    </item>
                </xsl:for-each>
              </ADDTLINF>
          </STSRSNINF>
        </ORGNLGRPINFANDSTS>
      </CSTMRPMTSTSRPT>
    </item>
  </xsl:template>
</xsl:transform>

it will be big help for me if i can able to process incoming XML payload without modified it.

0
avatar image
Former Member Mar 20, 2017 at 11:38 AM
0

thanks for your quick reply Nakli

Actually requirement is like to convert XML data into ABAP internal table using XSLT call transformation.

My report is working fine if i pass the modified input payload i.c. arranging the XML file manually by putting each tag in separate line.

below is my abap code:

REPORT ZRPT_XML_ABAP.
TYPE-POOLS: ABAP, IXML.
TYPES: BEGIN OF GRPHDR,
 MSGID TYPE CHAR50,
 CREDTTM TYPE CHAR20,
 END OF GRPHDR.

TYPES: BEGIN OF ADDTLINF, ADDTLINF(50), END Of ADDTLINF.

TYPES: BEGIN OF STSRSNINF, ADDTLINF TYPE STANDARD TABLE OF STRING WITH NON-UNIQUE DEFAULT KEY, END OF STSRSNINF. TYPES: BEGIN OF ORGNLGRPINFANDSTS, ORGNLMSGID TYPE CHAR50, ORGNLMSGNMID TYPE CHAR20, ORGNLCREDTTM TYPE CHAR25, ORGNLNBOFTXS TYPE NUMC10, ORGNLCTRLSUM TYPE P DECIMALS 3, GRPSTS TYPE CHAR20, STSRSNINF TYPE STSRSNINF, END OF ORGNLGRPINFANDSTS.

TYPES: BEGIN OF CSTMRPMTSTSRPT, GRPHDR TYPE GRPHDR, ORGNLGRPINFANDSTS TYPE ORGNLGRPINFANDSTS, END OF CSTMRPMTSTSRPT.

TYPES: BEGIN OF TY_FINAL, * DOCUMENT TYPE ZDOCUMENT, "ZCSTMRPMTSTSRPT, CSTMRPMTSTSRPT TYPE CSTMRPMTSTSRPT, END OF TY_FINAL. DATA: IT_FINAL TYPE STANDARD TABLE OF TY_FINAL, WA_FINAL TYPE TY_FINAL.

* Result table that contains references * of the internal tables to be filled DATA: GT_RESULT_XML TYPE ABAP_TRANS_RESBIND_TAB, GS_RESULT_XML TYPE ABAP_TRANS_RESBIND.

* For error handling DATA: GS_RIF_EX TYPE REF TO CX_ROOT, GS_VAR_TEXT TYPE STRING. DATA: GT_ITAB TYPE STANDARD TABLE OF CHAR2048, " WITH HEADER LINE. GS_ITAB TYPE CHAR2048.

DATA: DUMMY(1) TYPE C, TITLE(7) TYPE C.

PARAMETERS GS_FILE TYPE STRING. AT SELECTION-SCREEN ON VALUE-REQUEST FOR GS_FILE. TITLE = 'Dataset'(c14). CALL FUNCTION 'WS_FILENAME_GET' EXPORTING DEF_FILENAME = SPACE DEF_PATH = GS_FILE MASK = ',*.*.' MODE = 'O' TITLE = TITLE IMPORTING FILENAME = GS_FILE RC = DUMMY EXCEPTIONS NO_BATCH = 08 SELECTION_CANCEL = 12 SELECTION_ERROR = 16.

START-OF-SELECTION.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD EXPORTING FILENAME = GS_FILE CHANGING DATA_TAB = GT_ITAB EXCEPTIONS FILE_OPEN_ERROR = 1 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.

*deleting the document tag from XML LOOP AT GT_ITAB INTO GS_ITAB. SEARCH GS_ITAB FOR 'DOCUMENT'. IF SY-SUBRC EQ 0. DELETE GT_ITAB. ENDIF. ENDLOOP.

* Fill the result table with a reference to the data table. * Within the XSLT style sheet, the data table can be accessed with * "CSTMRPMTSTSRPT_GET". GET REFERENCE OF IT_FINAL INTO GS_RESULT_XML-VALUE. GS_RESULT_XML-NAME = 'CSTMRPMTSTSRPT_GET'. APPEND GS_RESULT_XML TO GT_RESULT_XML.

* Perform the XSLT stylesheet TRY. CALL TRANSFORMATION ZXSLT_TEST SOURCE XML GT_ITAB RESULT (GT_RESULT_XML). CATCH CX_ROOT INTO GS_RIF_EX. GS_VAR_TEXT = GS_RIF_EX->GET_TEXT( ). MESSAGE GS_VAR_TEXT TYPE 'E'. ENDTRY.

LOOP AT IT_FINAL INTO WA_FINAL. endloop. ENDIF.

XSLT code:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
 <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
 <asx:values>
 <CSTMRPMTSTSRPT_GET>
 <xsl:apply-templates select="//CSTMRPMTSTSRPT"/>
 </CSTMRPMTSTSRPT_GET>
 </asx:values>
 </asx:abap>
 </xsl:template>
 <xsl:template match="CSTMRPMTSTSRPT">
 <item>
 <CSTMRPMTSTSRPT>
 <GRPHDR>
 <MSGID>
 <xsl:value-of select="GRPHDR/MSGID"/>
 </MSGID>
 <CREDTTM>
 <xsl:value-of select="GRPHDR/CREDTTM"/>
 </CREDTTM>
 </GRPHDR>
 <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>
 <STSRSNINF>
 <ADDTLINF>
 <xsl:for-each select="ORGNLGRPINFANDSTS/STSRSNINF/ADDTLINF">
 <item>
 <xsl:value-of select="."/>
 </item>
 </xsl:for-each>
 </ADDTLINF>
 </STSRSNINF>
 </ORGNLGRPINFANDSTS>
 </CSTMRPMTSTSRPT>
 </item>
 </xsl:template>
</xsl:transform>

it will be big help for me if i can able to process incoming XML payload without modified it.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

When you have manually broken down the xml, Document open and Document close tags are in separate lines.

As a result, your logic to delete the document tag works fine.

When you don't split the xml, document tag deletion logic is probably deleting everything inside document tag as well.

Try correcting that.

EDIT: You can replace the document tag deletion logic with below code. Accordingly you need to use source xml as lv_xml instead of gt_itab. The code you pasted has terrible formatting. Keep that in mind going forward.

DATA lv_xml TYPE string.
CONCATENATE LINES OF gt_itab INTO lv_xml.
REPLACE REGEX '<DOCUMENT[^>]+>' IN lv_xml WITH ''.
REPLACE '</DOCUMENT>' IN lv_xml WITH ''.
CALL TRANSFORMATION zxslt_test SOURCE XML lv_xml RESULT (gt_result_xml).
0