"
Supported by

Save XML data in an ST (Simple Transformation)

Some days ago I was using an ST (Simple Transformation) and thought that, even though its job is to convert an input into an output, it is can be used to store XML data.

Let’s asy we have the following XML:


  <cocktails>
    <cocktail id="gt" nome="Gin Tonic"/>
    <cocktail id="ws" nome="Whiskey Sour"/>
    <cocktail id="cl" nome="Campari Laranja"/>
  </cocktails>

The way to store it in an ST is to wrap it like this:


<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="/">
    <cocktails>
      <cocktail id="gt" name="Gin Tónico"/>
      <cocktail id="ws" name="Whiskey Sour"/>
      <cocktail id="cl" name="Campari Laranja"/>
    </cocktails>
  </xsl:template>
</xsl:transform>

And the ABAP code to read it to a string is quite simple:


DATA: xml_str TYPE string.
xml_str = ||.
CALL TRANSFORMATION zcocktail_data
  SOURCE XML xml_str
  RESULT XML xml_str.

Note that the input string must be a valid XML even though it will be ignored.
And so, now we have our XML in a string.

To convert this XML into an internal table we just have to use another simple ST:


<?sap.transform simple?>
<tt:transform 
  xmlns:tt="http://www.sap.com/transformation-templates" 
  xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" 
  xmlns:def="http://www.sap.com/abapxml/types/defined">
  <tt:root name="T_COCKTAIL" type="ddic:ZCOCKTAIL_T"/>
  <tt:template>
    <cocktails>
      <tt:loop ref=".T_COCKTAIL">
        <cocktail>
          <tt:attribute name="id" value-ref="ID"/>
          <tt:attribute name="name" value-ref="NAME"/>
        </cocktail>
      </tt:loop>
    </cocktails>
  </tt:template>
</tt:transform>

And call it using the following code:


DATA: t_cocktail TYPE zcocktail_t.
CALL TRANSFORMATION zcocktail_itbl
  SOURCE XML xml_str
  RESULT constants = t_cocktail.

And that’s it. An unorthodox but practical and somewhat simple way to store static data in SAP without using the database.

Greetings from Abapinho.

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).