Professional Documents
Culture Documents
ABAP Exercises XSLT XML PDF
ABAP Exercises XSLT XML PDF
Exercises / Solutions
The context for the exercises in this unit is based on the "customers" example. This customers list contains three parts: unique id,
name, and address.
We will assume that there is some XML standard for customers and our task is to map data in an XML document to an ABAP table of
a data type (provided by an application) and back. We first perform this task using XSLT, later we use Simple Transformations.
General Remarks
In order to write transformations between two formats, it is important to fully understand both the formats and how the mapping
should work. After executing the report in Exercise 1, you have a general idea of how the ABAP data type and the "standard" XML
format for customers look like. Here is some additional information:
a) The ABAP data type (t_customers) for customers is defined at the beginning of program ZABAP252_nn (line 24ff), it is a
table of structures (with substructures).
b) The "standard" XML format for customers has a root named Customers in the namespace
http://www.standards.org/customers. It has 0 or more subelements named Customer each of which corresponds to
a table line in the ABAP table. The attribute id and the subelements Name and Address correspond to the similar named
components of the table line. These two subelements have the following properties:
• The element Name has two subelements Last and First if there is no middle name. It has the subelements Last and
Given otherwise where Given has two subelements First and Middle.
• The subelements of Address are all optional and can appear in any order. A missing subelement State corresponds to
the ABAP component STATE to have the value 'XX', other missing subelements correspond to initial values of the
similar named components.
c) For the curious: The customer XML document (used in the report for the XML to ABAP transformations) comes from an
XSLT transformation (ZABAP252_CUSTOMERS) with literal content, the output of the transformation does not depend on
the input. The customer ABAP table (used in the report for the ABAP to XML transformations) comes from transforming the
customer XML document with the proposed solution of Exercise 4.
http://evphl401.phl.sap.corp:50180/sap/bc/bsp/sap/zabap252/intro.htm
*&---------------------------------------------------------------------*
*& Report ZABAP252_99
*&
*&---------------------------------------------------------------------*
*& Exercises for TechEd 04: workshop ABAP 252
*& XSLT and ST
*&---------------------------------------------------------------------*
REPORT zabap252_99.
CONSTANTS: group_nr TYPE n LENGTH 2 VALUE '99'.
PARAMETERS:
exerc1 TYPE char1 RADIOBUTTON GROUP exer DEFAULT 'X',
exerc2 TYPE char1 RADIOBUTTON GROUP exer,
exerc3 TYPE char1 RADIOBUTTON GROUP exer,
exerc4 TYPE char1 RADIOBUTTON GROUP exer,
exerc5 TYPE char1 RADIOBUTTON GROUP exer.
SELECTION-SCREEN SKIP 1.
PARAMETERS:
abap2xml TYPE char1 RADIOBUTTON GROUP dirn DEFAULT 'X',
xml2abap TYPE char1 RADIOBUTTON GROUP dirn.
*---------------------------------------------------------------------*
*
* Main Program
*
*---------------------------------------------------------------------*
START-OF-SELECTION.
DATA ex TYPE REF TO cx_root.
DATA ex_st TYPE REF TO cx_st_error.
DATA ex_xslt TYPE REF TO cx_xslt_system_error.
DATA prog_name TYPE string.
DATA line TYPE i.
TRY.
CASE 'X'.
WHEN exerc1.
SAP TECHED ’04 OCTOBER 2004 4
*&--------------------------------------------------------------------*
*& Form exercise
*&--------------------------------------------------------------------*
* Run exercise, both for ABAP to XML and v/v
*---------------------------------------------------------------------*
* -->A2X Part of name for ABAP to XML transformation
* -->X2A ditto, for XML to ABAP, with ST: same as A2X
* -->GN group number, usually constant GROUP_NR
*---------------------------------------------------------------------*
FORM exercise USING a2x TYPE string x2a TYPE string gn TYPE n
RAISING cx_transformation_error.
DATA trans_name TYPE string.
DATA customers TYPE t_customers.
DATA xml TYPE xstring.
IF xml2abap IS INITIAL.
CONCATENATE 'ZABAP252_' a2x '_' gn INTO trans_name.
PERFORM load_abap CHANGING customers.
* >>> ABAP to XML transformation in trans_name (root CUSTOMER_TABLE)
CALL TRANSFORMATION (trans_name)
SOURCE customer_table = customers
RESULT XML xml.
PERFORM show_xml USING xml.
ELSE.
CONCATENATE 'ZABAP252_' x2a '_' gn INTO trans_name.
PERFORM load_xml CHANGING xml.
* >>> XML to ABAP transformation in trans_name (root CUSTOMER_TABLE)
CALL TRANSFORMATION (trans_name)
SOURCE XML xml
RESULT customer_table = customers.
PERFORM show_abap USING customers.
ENDIF.
ENDFORM. "exercise
*=====================================================================*
SAP TECHED ’04 OCTOBER 2004 5
*
* Load/Show ABAP/XML (general procedures)
*
*=====================================================================*
*&--------------------------------------------------------------------*
*& Form load_abap
*&--------------------------------------------------------------------*
* load ABAP data (from XML data in XSLT prg ZABAP252_CUSTOMERS)
*---------------------------------------------------------------------*
* -->CUSTOMERS ABAP customer table
*---------------------------------------------------------------------*
FORM load_abap CHANGING customers TYPE t_customers.
DATA xml TYPE xstring.
PERFORM load_xml CHANGING xml.
CALL TRANSFORMATION zabap252_st4_99
SOURCE XML xml RESULT customer_table = customers.
ENDFORM. "load_abap
*&--------------------------------------------------------------------*
*& Form show_abap
*&--------------------------------------------------------------------*
* show ABAP data in an ABAP list
*---------------------------------------------------------------------*
* -->CUSTOMERS ABAP customer table
*---------------------------------------------------------------------*
FORM show_abap USING customers TYPE t_customers.
FIELD-SYMBOLS: <cust> TYPE t_customer.
FORMAT COLOR COL_HEADING.
WRITE: 'ID',
AT 6 'NAME-{FIRST,MID,LAST}',
AT 40 'ADDR-{ZIP_CODE,CITY,STATE}',
AT /40 'ADDR-{STREET_NO,STREET}'.
FORMAT COLOR COL_BACKGROUND.
* SKIP.
*&--------------------------------------------------------------------*
*& Form load_xml
*&--------------------------------------------------------------------*
* load XML data (from XML data in XSLT prg ZABAP252_CUSTOMERS)
*---------------------------------------------------------------------*
* -->XML XML as XSTRING
*---------------------------------------------------------------------*
SAP TECHED ’04 OCTOBER 2004 6
*&--------------------------------------------------------------------*
*& Form show_xml
*&--------------------------------------------------------------------*
* show XML in HTML Viewer
*---------------------------------------------------------------------*
* -->XML XML to show as XSTRING, exception if no XML
*---------------------------------------------------------------------*
FORM show_xml USING xml TYPE xstring RAISING cx_xslt_runtime_error.
" Misuse of CALL TRANSFORMATION to convert xstring to table of X...:
CALL TRANSFORMATION id SOURCE XML xml RESULT XML m_xml.
CALL SCREEN 0100.
ENDFORM. "show_xml
*---------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*---------------------------------------------------------------------*
* helper module to use HTML Viewer, uses PF-Status HTML_VIEWER
*---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'HTML_VIEWER'.
CREATE OBJECT html_viewer
EXPORTING parent = cl_gui_container=>screen0.
*---------------------------------------------------------------------*
* MODULE user_command_0100 INPUT
*---------------------------------------------------------------------*
* helper module for navigation in HTML Viewer
*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
SET SCREEN 0.
IF html_viewer IS NOT INITIAL.
CALL METHOD html_viewer->free( ).
CLEAR html_viewer.
ENDIF.
ENDMODULE. "user_command_0100 INPUT
SAP TECHED ’04 OCTOBER 2004 7
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sap="http://www.sap.com/sapxsl"
xmlns:asx="http://www.sap.com/abapxml"
exclude-result-prefixes="asx" >
<xsl:strip-space elements="*"/>
<xsl:output encoding="utf-8" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/asx:abap/asx:values/CUSTOMER_TABLE">
<xsl:for-each select="NAME[1]">
<Name>
<Last><xsl:value-of select="LAST"/></Last>
<xsl:choose>
<xsl:when test="MID">
<Given>
<First><xsl:value-of select="FIRST"/></First>
<Middle><xsl:value-of select="MID"/></Middle>
</Given>
</xsl:when>
<xsl:otherwise>
<First><xsl:value-of select="FIRST"/></First>
</xsl:otherwise>
</xsl:choose>
</Name>
</xsl:for-each>
<xsl:for-each select="ADDR[1]">
<xsl:variable name="s" select="string(STATE)"/>
<Address>
<City><xsl:value-of select="CITY"/></City>
<ZIP><xsl:value-of select="ZIP_CODE"/></ZIP>
<xsl:if test="$s!='XX'">
<State><xsl:value-of select="$s"/></State>
</xsl:if>
<Street>
<xsl:value-of select="STREET_NO"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="STREET"/>
</Street>
</Address>
</xsl:for-each>
</Customer>
</xsl:for-each>
</c:Customers>
</xsl:template>
</xsl:transform>
SAP TECHED ’04 OCTOBER 2004 8
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sap="http://www.sap.com/sapxsl"
xmlns:asx="http://www.sap.com/abapxml"
exclude-result-prefixes="c" >
<xsl:strip-space elements="*"/>
<xsl:output encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/c:Customers">
<item>
<ID><xsl:value-of select="@id"/></ID>
<xsl:for-each select="Name[1]">
<NAME>
<LAST><xsl:value-of select="Last"/></LAST>
<xsl:choose>
<xsl:when test="Given">
<FIRST><xsl:value-of select="Given[1]/First"/></FIRST>
<MID><xsl:value-of select="Given[1]/Middle"/></MID>
</xsl:when>
<xsl:otherwise>
<FIRST><xsl:value-of select="First"/></FIRST>
</xsl:otherwise>
</xsl:choose>
</NAME>
</xsl:for-each>
<xsl:for-each select="Address[1]">
<xsl:variable name="s" select="string(Street)"/>
<xsl:variable name="p" select="sap:find-first($s,', ')"/>
<ADDR>
<CITY><xsl:value-of select="City"/></CITY>
<ZIP_CODE><xsl:value-of select="ZIP"/></ZIP_CODE>
<STATE><xsl:value-of select="sap:if(State, State, 'XX')"/></STATE>
<STREET><xsl:value-of select="substring($s,$p +2)"/></STREET>
<STREET_NO><xsl:value-of select="substring($s,1,$p -1)"/></STREET_NO>
</ADDR>
</xsl:for-each>
</item>
</xsl:for-each>
</CUSTOMER_TABLE>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
SAP TECHED ’04 OCTOBER 2004 9
<?sap.transform simple?>
<tt:transform
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:c="http://www.standards.org/customers" >
<tt:root name="CUSTOMER_TABLE"/>
<tt:template>
</Customer>
</tt:loop>
</c:Customers>
</tt:template>
</tt:transform>
SAP TECHED ’04 OCTOBER 2004 10
No part of this publication may be reproduced or transmitted in any form or for any purpose without the
express permission of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components
of other software vendors.
Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390,
OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli, and
Informix are trademarks or registered trademarks of IBM Corporation in the United States and/or other
countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or
registered trademarks of Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web
Consortium, Massachusetts Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented
and implemented by Netscape.
MaxDB is a trademark of MySQL AB, Sweden.
SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver and other SAP products and services
mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in
Germany and in several other countries all over the world. All other product and service names mentioned
are the trademarks of their respective companies. Data contained in this document serves informational
purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and
its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty
of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The
only warranties for SAP Group products and services are those that are set forth in the express warranty
statements accompanying such products and services, if any. Nothing herein should be construed as
constituting an additional warranty.