Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
Showing results for 
Search instead for 
Did you mean: 

Help with simple XSLT transformation - set xml tag names from parameter content

0 Kudos

I need help with a simple transformation from internal table data to XML. I can do it in two stages, as shown below. I do not know how to do this in the transformation in one step:


  BEGIN OF zxml_lot_t,
    tag TYPE c,
    n TYPE c,
    mean TYPE c,
    max TYPE i,
    min TYPE i,
    sd TYPE f,
  END OF zxml_lot_t,

  BEGIN OF zxml_st_data,
    lots TYPE zxml_lot_t,
  END OF zxml_st_data.

DATA: gs_xml TYPE zxml_st_data.
DATA: gv_xml TYPE string.

<?sap.transform simple?><b
<tt:transform xmlns:tt="" xmlns:ddic="" xmlns:def="">
  <tt:root name="ROOT" type="?"/>
  <tt:root name="ST_DATA" type="ddic:ZXML_ST_DATA"/>
        <tt:loop ref=".ST_DATA.LOTS">
            <tt:attribute name="TAG" value-ref="TAG"/>
            <tt:attribute name="N" value-ref="N"/>
            <tt:attribute name="MEAN" value-ref="MEAN"/>
            <tt:attribute name="MAX" value-ref="MAX"/>
            <tt:attribute name="MIN" value-ref="MIN"/>
            <tt:attribute name="SD" value-ref="SD"/>

The data contains a field that describes the desired tag name

I call the transformation to convert the data

OPTIONS xml_header = 'full'
SOURCE st_data = gs_xml
RESULT XML gv_xml.

Resulting in xml structured data

A second step in ABAP converts the xml to the final desired form

REPLACE ALL OCCURRENCES OF REGEX '<DIM TAG="DIM([a-zA-Z0-9_\-]{1,50})"' IN gv_xml WITH '<DIM$1'.

And so the desired output results

Can this all be done in the transformation?


Active Contributor

The language of your transformation is Simple Transformation (SAP Proprietary), not XSLT (W3C Standards).

As far as I know, you can't define dynamically the element names, except if you transform the code via an external method (tt:call-method) which implements IF_SXML_WRITER (to serialize). In your case, the method would need to transform almost everything so the transformation would do almost nothing and there's no interest in keeping the transformation.

A second solution is to use an XSLT transformation, it's slower, but you can choose the names of the elements inside the transformation itself (xsl:element name="{TAG}"...)

A third solution would be to not use a transformation because your final XML is quite simple. Note that you have the escape function in ABAP, which can escape the characters in both XML texts and attribute values.

I had never heard of the ABAP escape function. I'll take a look. The example as given is simplified from use case.