19.7    XSL Transformations

Extensible Stylesheet Language (XSL) is a stylesheet language for XML documents. Extensible Stylesheet Language Transformations (XSLT) allows you to transform XML documents into other formats. XSLT is a declarative programming language used for the structural transformation of XML documents and is standardized by W3C (World Wide Web Consortium).

XML documents consist of tree structures, and XSLT programs contain the rules for transformation in templates. These rules are applied to the source tree for transformation. For navigation and selection of node sets from the source tree, XSLT uses XML Path Language (XPath), which is a query language.

XSLT allows you to deserialize XML documents into ABAP data objects and vice versa. The XSLT processor, called the SAP XSLT Processor, is implemented in the kernel of SAP NetWeaver AS ABAP. The SAP XSLT Processor executes the XSLT programs, which exist in the repository just like any other ABAP programs. You can create these programs in Object Navigator via the context menu path CreateOtherTransformation.

The CALL TRANSFORMATION statement is used to call the transformation. The syntax for using the statement is as follows:

CALL TRANSFORMATION trans SOURCE [XML]..
RESULT [XML]..

In the statement, trans is an XSLT or a Simple Transformation (ST) program. If XML is provided for both the source and result, the XML document will be transformed into a different XML document using the specified transformation.

A parameter list, structure, or internal table can be used to serialize or deserialize data into ABAP data objects. Because XSLT expects data in XML format for transformation and can’t interpret ABAP data directly, the ABAP data is implicitly transformed into ABAP Serialization XML (asXML) before it’s processed by XSLT.

19.7.1    Serialization

During serialization, ABAP data is first converted to asXML using a predefined transformation ID, the result of which is used as the source for XSLT. If ID itself is used for the ID in the transformation, then the asXML itself is output without further transformation.

Listing 19.5 shows sample code to transform the data in an ABAP structure to XML. The DISPLAY_XML_STRING function module displays the XML output, as shown in Listing 19.5.

DATA : BEGIN OF sflight,
carrid TYPE s_carr_id VALUE 'AA',
connid TYPE s_conn_id VALUE '0017',
END OF sflight.
DATA xmlstr TYPE xstring.

CALL TRANSFORMATION ID
SOURCE FLIGHT = sflight
RESULT XML xmlstr.

CALL FUNCTION 'DISPLAY_XML_STRING'
EXPORTING xml_string = xmlstr.

Listing 19.5    XML Transformation

XML Output

Figure 19.125    XML Output

19.7.2    Deserialization

During deserialization, the result of the XML transformation is deserialized directly into the ABAP data. The following is an example of deserialization:

CALL TRANSFORMATION ID
SOURCE XML xmlstr
RESULT FLIGHT = sflight.

Similarly, you can use individual fields or internal tables for serialization and deserialization. Listings 19.6 shows sample code for serializing internal table data. The result of the code in Listing 19.6 is shown in Figure 19.126.

DATA: it_sflight TYPE STANDARD TABLE OF sflight,
xmlstr TYPE xstring.
SELECT * FROM sflight INTO TABLE it_sflight WHERE carrid EQ 'AA'.
CALL TRANSFORMATION ID
SOURCE TABLE = it_sflight
RESULT XML xmlstr.
CALL FUNCTION 'DISPLAY_XML_STRING'
EXPORTING xml_string = xmlstr.

Listing 19.6    Serializing Internal Table Data

Result of Code in Listing 19.6

Figure 19.126    Result of Code in Listing 19.6

You can serialize objects as well, if the class implements the IF_ SERIALIZABLE_OBJECT interface.