Sunday, April 5, 2009

Generate Simple Transformation for XML in ABAP - Part II

Just wanted to quickly add some more information to my last post on the same topic "Discovering a Hidden Gem :Generate Simple Transformation for XML in ABAP"

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

<NewDataset>
   <Table>
      <Town POST_CODE="B5">BIRMINGHAM</Town>
      <County>WARWICK</County>
  </Table>
<Table>
      <Town POST_CODE="NP20">NEWPORT</Town>
      <County>WALES</County>
</Table>
</NewDataset>
Initial steps are similar to the example in my last post [ so I've just copied the screen shots from there ]
  1. Create Table Type ZNEWDATASET in transaction SE11. The hierarchy structure should be compatible with the XML hierarchy.





  2. Now create the ST program in transaction XSLT_TOOL. Spot the magic stick in the editor menu.




  3.  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.




  4. 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.



  5. 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.




  6. Important thing to remember: Always use 'SAVE' before adding , deleting or modifying nodes. Otherwise you will face errors during node operations.
  7. 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: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:attribute name="POST_CODE" value-ref="$REF.POST_CODE"/>
                                                      <tt:value ref="TOWN"/>
                                               </Town>
                                     <County tt:value-ref="COUNTY"/>
                          </Table>
                    </tt:loop>
               </NewDataSet>
         </tt:template>
</tt:transform>
Example ABAP code to call the transformation is as below:
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

5 comments:

SAP Jobs said...

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

SAP FI said...

Excellent explanation...I was googling to know XML in SAP ABAP. This article clarified my doubts. Keep writing all the good stuff.

Regards,

Anonymous said...

Beautiful blog...this is what exactly i was looking for .......

Anonymous said...

It works great. After activating ST I decided to add another field in my structure. I have activated the structure and the table type.

How to reflect the new field in my ST?

Anonymous said...

It Is A Great Work

Post a Comment

comment

Copyright

MyFreeCopyright.com Registered & Protected