Skip to Content

Recently I was working in a project wherein I had to read XML file in SAP system. I have been using iXML library for quite a long time, when such requirements arise…..however this time I thought of giving a try to Simple Transformations provided by SAP i.e. using XSLT_TOOL.

What would be covered in this tutorial is:

1. An overview of how to use Simple Transformation to read XML Data.

2. How to check for conditions of XML tags when reading XML file.

3. How to skip elements in XML file.

4. How to read attributes of XML tags.

5. How the concept of reading date and time fields work.

Both the XML file and the program has been attached for reference.

Overview

Two terms which are frequently used is “Serialization” and “Deserialization”. Serialization refers to the process of converting data into XML while Deserialization refers to the process of reading data from XML file and using it in our program.

To start with, lets take a simple XML file and try to read it. Below is the screenshot of XML file.

Screen 1.JPG

This XML file was created manually for testing purpose and it clearly represents “Sales Order” data.

From the screenshot it is visible that <ORDER> tag represents a single order, <ORDER_HEADER> represents order header details while <ORDER_LINE> node of XML is repeated multiple times representing order line details.

Important points to note is:

1. <ORDER> tag can exist multiple times in a single XML file i.e. multiple orders can be sent in an XML file (We are not going to cover an example wherein multiple <ORDER> tags exist)

2. <ORDER_HEADER> tag would always exist once in a single <ORDER>

3. <ORDER_LINE> tag can exist multiple times in a single <ORDER>.

Based on the above 3 points it is clear, that we would require a deep structure in SAP to hold this data. Since there can be multiple orders we have to create a table type for the highest element of the structure.

Below screenshots would help to understand:

To hold various orders i.e. <ORDER> tag, a table type has been created.

Table Type.JPG

The structure for this table type represents <ORDER_HEADER> and <ORDER_LINE> details of XML data

TT_Structure.JPG

Note: ORDER_HEADER is a structure while ORDER_LINE is a table type, as we can have multiple order lines but only a single header.

Screenshots for ORDER_HEADER and ORDER_LINE are as follows:

OH.JPG

OL.JPG

After the data dictionary part is completed, now lets move towards creation of a Simple Transformation using XSLT_TOOL

Go to XSLT_TOOL and create a simple transformation and click on Wizard.JPG (Edit Simple Transformation Graphically button)

Once Inside, click on the “ROOT” node and create a new root as shown in below screenshot: (Here, we need to enter the Table Type name which we had created earlier)

New Root.JPG

Drag and Drop the new root created to right hand side i.e. towards “Simple Transformation” section. You would see a screen similar to as shown below:

DAD.JPG

Now, it is important to remember that the “Simple Transformation” should resemble our XML file always in terms of name….this is because transformation does the comparison word by word.

Anything after Table.JPG table symbol represents that the value could repeat multiple times. So, if we look carefully in the above transformation the entire node of ‘ZVIN_ORDER_XML’ would be repeated multiple times i.e. this represents our <ORDER> tag.

‘ZVIN_ORDERL_XML’ tag represents our <ORDER_LINE> tag. Hence rename the tags appropriately, so that our new transformations looks as below:

New ST.JPG

We are almost done now, however now we have certain segments which are repeating like “ORDER” and “ORDER_LINE”

The higher level segments with above names are not needed, so we go to the source code by clicking on “BACK” button and remove redundant nodes by deleting them. The final transformation should be as follows:

Final Ts.JPG

If we compare the above transformation with the XML file that we are trying to upload….it is a perfect match.

To call the above transformation, we use ‘CALL_TRANSFORMATION’ statement of ABAP.

Lets perform a small test and see if the ABAP program is reading XML data or not.

OP1.JPG

Yes, the program is loading data from XML successfully.

Conditional checks on XML Tags

Now, lets introduce some complexity i.e. we introduce additional fields to our XML file such as VKORG, VTWEG and SPART.

However these tags would not be mandatory in XML file, i.e. sometimes it would be present while sometimes it may be missing (NOTE: XML tags itself might be missing)…In general, if the tags are missing in XML file while the same is mentioned in Simple Transformation then it results in error stating “System Expected Element &&&”

To handle such conditions we can introduce special conditions in the Souce Code of transformation.

Screenshot of new XML file:

New_XML.JPG

In the above screenshot SPART is introduced in XML file, however VKORG and VTWEG may also be present in future files.

To handle this, perform following:

We would need to modify our header data dictionary structure to hold VKORG, VTWEG and SPART data when they are provided in XML file. Make the necessary modification in Simple Transformation using XSLT_TOOL

New ST1.JPG

Introduce conditional checks in Source code, as shown in below screenshot:

Cond Checks.JPG

Statement <tt:d-cond check=”exist(NodeName)”> ensures that if the tags are missing then move forward to next tag else read its data.

When we execute the program with the new XML file, we can see data was loaded successfully even though VKORG and VTWEG tags were missing.

Debug1.JPG

Skip Elements/Tags of XML File

Now, let us handle another situation i.e. there are certain XML tags being passed in XML file which is of no use to me.

Now, either I can ignore the data after reading or SAP also allows us to skip processing of unnecessary tags.

Screenshot of new XML:

New XML2.JPG

In the above XML, two new tags exist namely <OURREF> and <OURCUSTOMER>.

Data from these tags are not needed in SAP and hence we decide to skip processing of these tags by writing separate conditions in Simple Transformation.

New ST2.jpg

Statement <tt:skip count=”*” name=”Node”> allows us to skip processing of node “Node” which is repeating consecutively any number of times.

* The count value may not always be ‘*’. Please check SAP help for more details.

How to read attributes of XML tag

Now, there are many cases wherein certain attributes exist in XML tags.

Once of the attributes which I had to encounter was as below:

<Order_Value Currency=”HUF”>10000</Order_Value>

In the above example the total order value is 10000 and the currency key for the same is “HUF”. Now the requirement is that we are not only supposed to read the Order Value of 10000 but also the Currency Key as “HUF”

New XML File:

New XML3.JPG

In the above XML file, we are passing the currency key along with the tag <VBELN> as an attribute. (I know that this is not a good example…consider it only for testing purpose)

Now, the requirement is to read the Currency key from the XML file.

We can do this by adding additional tags in Source Code….as shown below:

New ST3.JPG

We need to introduce a new field in our Header Structure to hold “Currency Key” and then we need to simply pass the value as shown above.

<tt:attribute name=”AttributeName” value-ref=”NodeName”/> allows us to read the attribute value and pass it to ORDER_HEADER.WAERS field.

Screenshot after reading data in debug mode:

Debug2.JPG

How the concept of reading date and time fields work.

More information on this can be obtained in the below link:

http://help.sap.com/saphelp_erp60_sp/helpdata/en/52/491a40251e2402e10000000a1550b0/content.htm

As per the above link, if the date and time are represented in ISO-8601 standard, the conversion of data in SAP would be made automatically.

For E.g. if there exists a tag <ERDAT>2013-10-10</ERDAT> in the XML file and the corresponding field in ABAP Structure has the datatype as ‘D’….then while de-serialization data would be loaded in SAP as 20101010 i.e. internal format.

Additional References:

Below are some useful links which should help in understanding Simple Transformation Process:

SAP Help on various tags to be used in Simple Transformation

http://help.sap.com/saphelp_erp60_sp/helpdata/en/18/d16240e85cef6fe10000000a1550b0/content.htm

SAP Help on CALL TRANSFORMATION statement http://help.sap.com/abapdocu_702/en/abapcall_transformation.htm

Complete Simple Transformation – http://help.sap.com/saphelp_erp60_sp/helpdata/en/1e/c062405c910272e10000000a155106/frameset.htm


Disclaimer: The attached program and XML file are for test purpose only. There might be modifications required in Simple Transformation if the XML file is modified to include additional tags.


To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Ramesh Adusumilli

    Hi Vineesh,

    Excellent article about real world use of conditional tags.

    I am new to XML and have couple of questions. Would appreciate it if can provide information if you have it.

    1.) The tags seem to be case sensitive. Is there a way to handle it. For example, the simple transformation is defined as <VKORG>, but the xml file provides <vkorg>

    2.) How to handle unexpected tags. Example, simple transformation is defined for <VKORG> and <MATNR>. XML file from external system sends <VKORG>, <VTWEG> and <MATNR>

    Hope these can be handled.

    Thank you.

    (0) 
    1. Vineesh Varghese Post author

      Thank you Ramesh for the comments.

      Its been a while that I have touched XSLT now….but from what I remember both the requirements mentioned by you are not handled by XSLT.

      This is the reason why I always recommend iXML Classes…..since it gives us much better control of what is read….and how it needs to be handled.

      You can also have a look at the SAP help…..if at all it mentions about how this can be handled.

      ST – Overview – Simple Transformations – SAP Library

      Let me know if I am wrong…..and then I could try that and then update this document.

      (0) 

Leave a Reply