Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Transformations for XML →  CALL TRANSFORMATION →  CALL TRANSFORMATION - Examples 

Transformation of XML Element Names

This example demonstrates the transformation of letters in XML element names.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new( ).

    DATA: BEGIN OF simple_struc,
            int_col1 TYPE i VALUE 111,
            int_col2 TYPE i VALUE 222,
          END OF simple_struc.

    out->begin_section( `Serialization` ).

    CALL TRANSFORMATION id
                        SOURCE simple_struc = simple_struc
                        RESULT XML DATA(asxml).
    out->begin_section( `ID`
      )->write_xml( asxml ).

    CALL TRANSFORMATION demo_id_upper_lower
                        PARAMETERS mode = 'LO'
                        SOURCE simple_struc = simple_struc
                        RESULT XML DATA(xml_lower).
    out->next_section( `DEMO_ID_UPPER_LOWER`
      )->write_xml( xml_lower ).

    CALL TRANSFORMATION demo_id_from_to_mixed
                        PARAMETERS mode = 'TO'
                        SOURCE simple_struc = simple_struc
                        RESULT XML DATA(xml_camel).
    out->next_section( `DEMO_ID_FROM_TO_MIXED`
      )->write_xml( xml_camel ).


    out->end_section(
      )->next_section( `Deserialization` ).

    CLEAR simple_struc.
    CALL TRANSFORMATION id
                        SOURCE XML xml_lower
                        RESULT simple_struc = simple_struc.
    out->begin_section( `ID for XML_LOWER`
      )->write( simple_struc ).

    CLEAR simple_struc.
    CALL TRANSFORMATION id
                        SOURCE XML xml_camel
                        RESULT simple_struc = simple_struc.
    out->next_section( `ID for XML_CAMEL`
      )->write( simple_struc ).

    CLEAR simple_struc.
    CALL TRANSFORMATION demo_id_upper_lower
                        SOURCE XML xml_lower
                        RESULT simple_struc = simple_struc.
    out->next_section( `DEMO_ID_UPPER_LOWER for XML_LOWER`
      )->write( simple_struc ).

    CLEAR simple_struc.
    CALL TRANSFORMATION demo_id_from_to_mixed
                        SOURCE XML xml_camel
                        RESULT simple_struc = simple_struc.
    out->next_section( `DEMO_ID_FROM_TO_MIXED for XML_CAMEL`
      )->write( simple_struc ).


    out->display( ).

Description

In deserializations of XML data to ABAP data, the XML elements in question must generally be written in uppercase letters to be identified. This example shows ways of transforming elements written in other ways using self-written XSL transformations.

  • A structure, simple_struc, is serialized using various XSL transformations.
  • The self-written XSL transformation DEMO_ID_UPPER_LOWER transforms the XML element names created by the serialization to lowercase letters, if the correct parameter passing is used.
  • The self-written XSL transformation DEMO_ID_FROM_TO_MIXED transforms the XML element names created by the serialization to Mixed Case Style (also known as Camel Case Style), if the correct parameter passing is used. To do this, an ABAP method is called from the transformation and this method itself calls the built-in function to_mixed.
  • Deserializations of the transformed asXML data with the identity transformation ID do not find the structure or the components.
  • Deserializations of the transformed asXML data with the self-written XSL transformations, on the other hand, are successful.
  • DEMO_ID_UPPER_LOWER also transforms lowercase letters to uppercase letters.
  • DEMO_ID_FROM_TO_MIXED also calls a method for to_mixed. This simple example transformations is not, however, symmetrical in all cases.

Instead of transformations, parsers and renderers can be used, as demonstrated in the executable example for JSON. The serial processing used here can be useful when dealing with large volumes of data.

The transformations used are as follows:

DEMO_ID_UPPER_LOWER

<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">

  <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<xsl:param name="MODE" select="'UP'"/>

<xsl:template match="">
  <xsl:element name="{sap:if($MODE='LO',
         translate(name(),$uppercase, $smallcase ),
         translate(name(),$smallcase, $uppercase ))}">
    <xsl:copy-of select="@
"/>
    <xsl:apply-templates select="node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="asx:">
  <xsl:copy>
    <xsl:copy-of select="@
"/>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text() | processing-instruction() | comment()">
  <xsl:copy/>
</xsl:template>

</xsl:transform>

DEMO_ID_FROM_TO_MIXED

<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"
  xmlns:f="FCT" exclude-result-prefixes="f">

<sap:external-function name="f:toCC" kind="class"
class="CL_DEMO_XSLT_FROM_TO_MIXED" method="TO_CAMEL_CASE">
  <sap:argument param="IN"  type="string"/>
  <sap:result   param="OUT" type="string"/>
</sap:external-function>

<sap:external-function name="f:fromCC" kind="class"
class="CL_DEMO_XSLT_FROM_TO_MIXED" method="FROM_CAMEL_CASE">
  <sap:argument param="IN"  type="string"/>
  <sap:result   param="OUT" type="string"/>
</sap:external-function>

<xsl:param name="MODE" select="'FROM'"/>


<xsl:template match="">
  <xsl:element name="{sap:if($MODE='TO',
                      f:toCC(name()),
                      f:fromCC(name()))}">
    <xsl:copy-of select="@
"/>
    <xsl:apply-templates select="node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="asx:">
  <xsl:copy>
    <xsl:copy-of select="@
"/>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text() | processing-instruction() | comment()">
  <xsl:copy/>
</xsl:template>

</xsl:transform>