In this example, I will show how to use "Edit Simple Transformation Graphically" feature of XSLT_TOOL to generate simple transformations for XML elements having attributes.
Let's say there is a requirement to generate XML document from the ABAP data/ internal tables. And the resultant XML document should have the following structure. Basically, POST_CODE should be an attribute of "Town" element.
<?xml version="1.0" encoding="utf-8" ?>Initial steps are similar to the example in my last post [ so I've just copied the screen shots from there ]
<NewDataset>
<Table>
<Town POST_CODE="B5">BIRMINGHAM</Town>
<County>WARWICK</County>
</Table>
<Table>
<Town POST_CODE="NP20">NEWPORT</Town>
<County>WALES</County>
</Table>
</NewDataset>
- Create Table Type ZNEWDATASET in transaction SE11. The hierarchy structure should be compatible with the XML hierarchy.
- Now create the ST program in transaction XSLT_TOOL. Spot the magic stick in the editor menu.
- Click on 'Edit Simple Transformation Graphically' button (magic stick) and you will get the editor as below. Create a new root (right click - context menu ) in "Data Roots" section. I've named it NEWDATASET but the important thing is to enter the correct type, created earlier.
- Now you can see the data root hierarchy as below. Drag and Drop the NEWDATASET root on right panel ( Simple Transfromation ). It automaically generates the ST nodes corresponding to the ABAP structure. However, you need to adjust the names as per the XML element names.
- Now adjust the names in ST panel as per actual XML element names. Delete the PostCode element as we need this as an attribute of Town, rather than an element.
- Important thing to remember: Always use 'SAVE' before adding , deleting or modifying nodes. Otherwise you will face errors during node operations.
- Select node "Town", and then click on "First Child" button at the top toolbar. Further, use right click to get the conext menu for "Town" element and create a new attribute 'POST_CODE". Now drag the POST_CODE from left panel (data) to right panel on the POST_CODE attribute to create association. The index numbers shows the association between data nodes and xml nodes.
Save and Activate. The ST program (Z_RAM_TEST_ATTR1), generated by the utility, is as below:
<?sap.transform simple?><tt:attribute name="POST_CODE" value-ref="$REF.POST_CODE"/>
<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="NEWDATASET" type="ddic:ZNEWDATASET"/>
<tt:template>
<NewDataSet>
<tt:loop ref=".NEWDATASET">
<Table>
<Town>
<tt:value ref="TOWN"/>
</Town>
<County tt:value-ref="COUNTY"/>Example ABAP code to call the transformation is as below:
</Table>
</tt:loop>
</NewDataSet>
</tt:template>
</tt:transform>
REPORT z_ram_abap_to_xml.
DATA : lt_source TYPE znewdataset,
wa_source LIKE LINE OF lt_source,
xml_result TYPE xstring. "xstring ensures UTF-8 encoding
wa_source-town = 'BIRMINGHAM'.
wa_source-county = 'WARWICK'.
wa_source-post_code = 'B5'.
APPEND wa_source TO lt_source.
wa_source-town = 'NEWPORT'.
wa_source-county = 'WALES'.
wa_source-post_code = 'NP20'.
APPEND wa_source TO lt_source.
CALL TRANSFORMATION z_ram_test_attr1
SOURCE newdataset = lt_source[]
RESULT XML xml_result .
CALL FUNCTION 'SCOL_TRACE_SHOW_XML'
EXPORTING
xdoc = xml_result.
In case you want help on how to download the XML documents etc, please check the standard example transaction SSTDEMO1









3 comments:
I like the fact that its straight forward to follow and even though you did put in bold red "Always use 'SAVE' before adding" - some of us forget! lol
Excellent explanation...I was googling to know XML in SAP ABAP. This article clarified my doubts. Keep writing all the good stuff.
Regards,
Beautiful blog...this is what exactly i was looking for .......
Post a Comment
comment