HOW TO XSLT_TOOL simple transformation

作者:郑德鼎 约 5 分钟阅读 更新日期:2024-02-22 2 年前更新 标签:ABAP, 开发
目录

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

...

郑德鼎

关于作者:郑德鼎

企业信息化与 SAP 技术顾问,长期专注 SAP ABAP、FI/CO、MM、SD 等模块的技术分享与实战经验总结。查看更多介绍

来源说明:本文内容由「HOW TO XSLT_TOOL simple transformation.docx」整理生成,仅用于内部技术分享与学习交流。