HOW TO XSLT_TOOL simple transformation

<?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="FIELDS" line-type="ddic:/COCKPIT/SHDR_EXT"/> <tt:root name="HEADER" type="ddic:/COCKPIT/SHDR_EXT"/> <tt:variable name="V_FIELDNAME" type="C" length="19"/>   <tt:template>     <Documents>       <Document>         <Invoice>         <tt:skip count="*" name="Source"/>           <Supplier>         <tt:skip count="*" name="Name"/>         <tt:skip count="*" name="Identifier"/>               <Number tt:value-ref=".HEADER.VENDOR_NO"/>         <tt:skip count="*" name="Number"/>         <tt:skip count="*" name="Description"/>         <tt:skip count="*" name="CorporateGroupNumber"/>         <tt:skip count="*" name="POBox"/>         <tt:skip count="*" name="Street"/>         <tt:skip count="*" name="StreetSupplement"/>         <tt:skip count="*" name="PostalCode"/>         <tt:skip count="*" name="City"/>         <tt:skip count="*" name="CountryCode"/>         <tt:skip count="*" name="CountryName"/>         <tt:skip count="*" name="TelephoneNumber"/>         <tt:skip count="*" name="FaxNumber"/>         <tt:skip count="*" name="VATRegistrationNumber"/>         <tt:skip count="*" name="Location"/>         <tt:skip count="*" name="State"/>         <tt:skip count="*" name="TaxNumber1"/>         <tt:skip count="*" name="TaxNumber2"/>         <tt:skip count="*" name="CustomSupplierField1"/>         <tt:skip count="*" name="CustomSupplierField2"/>         <tt:skip count="*" name="EmailAddress"/>           </Supplier>           <Buyer>              <tt:skip count="*" name="Name"/>              <tt:skip count="*" name="Identifier"/>              <Number tt:value-ref=".HEADER.COMP_CODE"/> <tt:skip count="*" name="Description"/> <tt:skip count="*" name="CorporateGroupNumber"/> <tt:skip count="*" name="POBox"/> <tt:skip count="*" name="Street"/> <tt:skip count="*" name="StreetSupplement"/> <tt:skip count="*" name="PostalCode"/> <tt:skip count="*" name="City"/> <tt:skip count="*" name="CountryCode"/> <tt:skip count="*" name="CountryName"/> <tt:skip count="*" name="TelephoneNumber"/> <tt:skip count="*" name="FaxNumber"/> <tt:skip count="*" name="VATRegistrationNumber"/>           </Buyer>           <Fields>             <tt:loop ref=".FIELDS">              <Field>                 <tt:assign to-var="V_FIELDNAME" val="' '"/>                 <tt:attribute name="Name">                   <tt:read var="V_FIELDNAME"/>                 </tt:attribute>                 <tt:switch-var>                    <tt:cond-var check="var(V_FIELDNAME)='Invoice/CreditNote'">                      <tt:value ref=".HEADER.HEADER_TXT"/>                    </tt:cond-var>                 <tt:cond-var check="var(V_FIELDNAME)='InvoiceReference'">                      <tt:value ref=".HEADER.REF_DOC_NO"/>                </tt:cond-var>                     <tt:cond-var>                    <tt:skip/>                     </tt:cond-var>                 </tt:switch-var>              </Field>             </tt:loop>            </Fields>              <tt:skip count="*" name="Tables"/>         </Invoice>            <tt:skip count="*" name="System"/>            <tt:skip count="*" name="Processlog"/>       </Document>     </Documents>   </tt:template> </tt:transform>

0

Bryan Sippel

Trouble using tt:switch / tt:switch-var in simple transformation

Jun 16, 2016 at 08:13 PM | 36 Views

Hello gurus,

We are performing a simple transformation on an XML string we retrieve from an external source. A typical sample XML is as follows...

<VEHICLE ID="2064015">

<LON>-121.02363</LON>

<LAT>37.650665</LAT>

<DATE>2016-05-13 06:53:10.0</DATE>

<STREETNAME>Lone Palm Ave</STREETNAME>

<STREETNUM>1127</STREETNUM>

<LANDMARK>1707 Pacific Supply Modesto</LANDMARK>

<LANDMARKADDR>1155 North Emerald Ave</LANDMARKADDR>

<CITY>Modesto</CITY>

<STATE>CA</STATE>

<ODOMETER>10466431</ODOMETER>

<PARAMS>

<PARAM name="Name">GPS Antenna</PARAM>

<PARAM name="Status">Connected</PARAM>

</PARAMS>

</VEHICLE>

The PARAMS tag can have immediate level tags with the "name" attributes "Name" and "Status" that I would like to transform into my ABAP internal table, but it can alternatively have a nested deep structure that I need to ignore if the associated "name" attribute is "bus".

<PARAMS>

<PARAM name="bus">

<bus>

<driver></driver>

<trip/>

<event>O</event>

<eid/>

<desc>Active Fault</desc>

<data>

<datum>

<fmi>3</fmi>

<desc>Auxiliary Pressure #1-VOLTAGE ABOVE NORMAL, OR SHORTED TO HIGH SOURCE</desc>

<oc>126</oc>

<spn>1387</spn>

<lampstaus>0</lampstaus>

<source>49</source>

</datum>

</data>

</bus>

</PARAM>

</PARAMS>

The SAP documentation describes the tt:switch and tt:switch-var commands, but the examples are a bit lacking.

I have made several attempts using tt:read, tt:attribute, tt:assign, tt:skip, etc, trying to save the attribute value into a variable for testing, but I am still getting the same rather vague error message, "System expected a value for the type C".

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

<tt:root name="ROOT"/>

<tt:root name="ROOTPARAMS"/>

<tt:variable name="PARAMNAME"/>

<tt:template name="ZVM_XML_TO_TAB">

<HISTORY>

<tt:loop name="vehicle" ref=".ROOT">

<VEHICLE>

...

<PARAMS>

<tt:loop name="params" ref="$ref.PARAMS">

<PARAM>

<tt:attribute name="name" ref="var(PARAMNAME)"/>

<tt:switch-var>

<tt:cond-var check="var(PARAMNAME) = 'Name' or var(PARAMNAME) = 'Status'">

<tt:attribute name="name" value-ref="var(PARAMNAME)"/>

<tt:value ref="$params.VMPARAMVALUE"/>

</tt:cond-var>

<tt:cond-var>

</tt:cond-var>

</tt:switch-var>

</PARAM>

</tt:loop>

</PARAMS>

...

</VEHICLE>

</tt:loop>

</HISTORY>

</tt:template>

</tt:transform>

Can anyone please tell me what I am doing wrong with the tt:switch-var statement and how it should be corrected?

Essentially, I need to loop through the PARAMS tags, keeping any values where the name attribute is "Name" or "Status" and excluding any values where the name attribute is "bus" and its associated deep structure content.

Thanks,

Bryan

ABAP Development

Follow

RSS Feed

Related questions

Simple Transformation CX_ST_MATCH_ELEMENT

By Former Member, Dec 23, 2016

Simple Transformation Problem

By Daniel Mueller, Dec 08, 2017

Create xml from deep structure renaming structure names to tag names

By Former Member, Oct 11, 2017

1 Answer

Sort by:

Votes

|

Newest

|

Oldest

Sandra Rossi

Jun 19, 2016 at 12:15 AM

0

I can't answer your questions, as ST is not easy to understand. So, you could simplify your ST, transfer all PARAM nodes, and exclude the "bus" node using ABAP.

Program:

types : begin of ty_ls_param,

paramname  TYPE string,

paramvalue TYPE string,

end of ty_ls_param.

data lt_params TYPE TABLE OF ty_ls_param.

data string type string.

concatenate

'<?xml version="1.0" encoding="UTF-16"?>'

'<PARAMS>'

'    <PARAM name="Name">GPS Antenna'

'    <PARAM name="Status">Connected'

'    <PARAM name="bus">test bus'

'</PARAMS>'

into string.

try.

call transformation ztest source xml string result data = lt_params.

data lx_root type ref to cx_root.

catch cx_root into lx_root.

endtry. DELETE lt_params WHERE paramname = 'bus'.

BREAK-POINT.

Transformation "ztest":

<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

<tt:root name="DATA"/> <tt:template>     <PARAMS>         <tt:loop ref="DATA">             <PARAM>                 <tt:attribute name="name" value-ref="PARAMNAME"/>                 <tt:d-cond><bus><tt:skip/></bus></tt:d-cond>                 <tt:value ref="PARAMVALUE"/>             </PARAM>         </tt:loop>     </PARAMS> </tt:template>

</tt:transform>

Share

1 Comment

Sandra Rossi

Jun 19, 2016 at 05:03 PM

The closest transformation I could get for matching yours, is the following:

<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates"> <tt:root name="DATA"/> <tt:variable name="V_PARAMNAME" type="C" length="10"/> <tt:template>     <PARAMS>         <tt:loop ref="DATA">             <PARAM>                 <tt:assign to-var="V_PARAMNAME" val="' '"/>                 <tt:attribute name="name">                     <tt:read var="V_PARAMNAME"/>                 </tt:attribute>                 <tt:switch-var>                     <tt:cond-var check="var(V_PARAMNAME)='Name'                                                  or var(V_PARAMNAME)='Status'">                         <tt:attribute name="name" value-ref="PARAMNAME"/>                         <tt:value ref="PARAMVALUE"/>                     </tt:cond-var>                     <tt:cond-var>                         <tt:skip/>                     </tt:cond-var>                 </tt:switch-var>             </PARAM>         </tt:loop>     </PARAMS> </tt:template> </tt:transform>

With this transformation, you still get one line in the internal table, but all its components are initial.

Note that your "<tt:attribute name="name" ref="var(PARAMNAME)"/>" always produces an exception as "var(PARAMNAME)" (that you can see while debugging step-by-step the transformation) is not a data node.

I came to the conclusion that it's impossible to skip a line in an internal table during deserialization.

In your case, maybe the best solution is to define one structure for Name and one for Status instead of an internal table.

Like 0

Share

Vineesh Varghese

Posted onJuly 9, 2013 7 minute read

Simple Transformation – De-serialization

FollowRSS feedLike

7 Likes 18,726 Views 2 Comments

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.

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.

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

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:

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  (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)

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:

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 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:

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:

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.

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:

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

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

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.

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:

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.

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:

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:

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:

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.

Alert Moderator

Assigned tags

ABAP Development

simple transformation

simple transformations

xslt tool

Related Blog Posts

Simple Transformation

By Former Member, Jun 25, 2013

ABAP and XML – Introducing ST

By Horst Keller, Oct 27, 2004

Editing Source-Based Transformations for XML

By Wolfgang Woehrle, Oct 20, 2015

Related Questions

XML space tag problem in XML simple transformation de-serialization

By Rajesh Amirineni, Nov 01, 2014

Simple Transformation serialization - XML has sorted attributes

By Former Member, Mar 10, 2010

Simple transformation

By Former Member, Sep 06, 2005

2 Comments

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

Ramesh Adusumilli

February 6, 2014 at 1:06 am

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.

Like(0)

Vineesh Varghese Post author

February 6, 2014 at 11:31 am

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.

返回ABAP参考资料列表