Monday, December 10, 2007

How can I use XSLT with ABAP engine?

Introduction

The fist time I looked at SAP XI, I searched for the best way to perform value mapping with data from SAP. Today we can use XSLT mapping at the ABAP core in SAP XI 3.0 with the best solution there is: ABAP XSLT extensions. In this weblog I will show you how easy it can be done to use SAP Abap functions in your XSLT.

An XSLT Mapping

Before focussing on the sap data we will focus on the XSLT itself. We want to translate source of system A into target structure B.


Source System A:



Robert
Eijpe
Male

Huizermaatweg
243A
1276 LJ
Huizen

NL




Target System B :


Mr
Robert Eijpe
Huizermaatweg 243A
1276 LJ
Huizen
Netherlands


For the mapping program we can build an XSLT program using a standard mapping tool like Mapforce or Stylus Studio. The generated XSLT program looks like:





<br /> <xsl:value-of select="p2:mt_System_A/Person/Gender"><br />



















If you run this XLST program, you will see this is not exactly what we need, but it is the basis of our SAP XSLT program. The result of this XSLT program looks like:



<b><i>Male</i></b>
Robert Eijpe
Huizermaatweg 243A
1276 LJ
Huizen
NL


If we compare the XSLT result with the needed xml for system B, we have a value mismatch at the tags Title and Country. We will use SAP XSLT extensions to solve this problem.

A basic SAP XSLT Mapping

When the XSLT mapping is ready, we can create a XSLT transformation at the SAP XI Abap core. With transaction XSLT_TOOL we start the SAP XSLT Editor.

image

After pushing the “Create” button, we have to fill out the following pop-up. It is important to choose ‘XSLT Program’ as Transformation Type.

image

We proceed to the tab ‘SourceCode’ and copy the generated XSLT from our XSTL tool into de editor field. After this we can activate the Transformation.


image

Testing the SAP XSLT Mapping

Before we continue, we have to test the transformation within SAP. This is important because SAP XSLT is based on the XSLT 1.0 standard, but SAP didn’t support all features of the XSLT 1.0 standard at the ABAP stack. SAP has also added some features of the XSLT 2.0 standard. More info can be found in SAP XSLT Processor Reference from SAP help.


Before testing can be done we have to save the System A xml into a file on our local PC. From the XSLT Editor we enter the SAP XSLT test tool by pressing the [] button. We can also go directly to the tool starting transaction XSLT.


image

We have to fill out the source path of the xml System A file and press the button “View HTML”. If SAP supports all features of the XSLT, you will get the same result as in the external XSLT tool. Now we are ready for the SAP Extensions.

Using SAP XSLT Extensions

All SAP extensions are declared in the “http://www.sap.com/sapxsl” namespace. We have to add this namespace to the style sheet, so we can use it.



We also declare an additional namespace for our own build functions. In this case “http://sdn.sap.com/sapxsl”.


In the sap help we found the Additional SAP Functions and Statements. We will use these to solve our value mismatches.


Mismatch 1 – Title

In the basic XSLT mapping we get MALE as result of the mapping with the gender field of System A. System B expects the value Mr. Let’s define additional conditions to this mapping:


System A System B (XI logon English)
Male Mr.
Female
Ms.
Others
empty

We can solve this implementing the SAP XSLT Extension Statement “sap:function” and use the standard SAP XSLT Extension functions “sap:if()”. The solution could be:






We have to add this function just before . To make the magic work, we also have to modify the value of the Title tag into:

<br /> <xsl:value-of select="sdn:GetTitle(string(p2:mt_System_A/Person/Gender))"><br />

Mismatch 2 – Country

Every SAP system contains table T005T with the country code and country description. We can make use of this table by using the SAP XSLT Extension Statement “sap:external-function” . With this statement we can call an abap object class method. This method could be static, but also a method from an initiated class is possible. In our case we will use the static method.


To do this we create an abap class ‘Z_SDN_COUNTRIES’ using transaction SE24.


image

We choose as a class type for an 'usual ABAP Class'. The instantiation will be 'public' and the class will be marked as 'final'.


image

Now we add a public static method named 'GETCOUNTRY' to the class.


image

We declare the importing and returning parameters and implement the method.


image

After this we save and activated the class.
Now we have finished the class creation, we can use this class in our XSLT.

The SAP XSLT Extension Statement “sap:external-function” will call the class method as follow:






We have to add this function also just before and modify the value of the Country tag into:




Use the XSLT Mapping within SAP XI 3.0

Having done all this our XSLT Mapping program 'Z_SDN_SYSTEM_A_TO_B' with SAP XSLT Extensions at the ABAP engine is ready for use in SAP XI.


image


No comments:

Blog Archive