The XSLT specification proposes an enhancement mechanism that enables the system to call external routines from XSLT for transforming XML structures. Java is a programming language that is supported by some Java XSLT processors as an enhancement language.
Using the XSLT mapping with Java enhancement, you can implement mappings quicker and more flexibly.
The working draft of the specification for XSL Transformations (XSLT) version 2.0 (version from December 20, 2001), does not define the features of the enhancement mechanism of the XSL transformation. There are also no accompanying standards that standardize the enhancement mechanism with Java. For this reason, XSLT processors available on the open market vary considerably in this respect.
Below is a description of one of the ways that the SAP J2EE Engine supports the XML Toolkit 2.0.
To implement an XSLT mapping with Java enhancement, proceed as follows:
...
1. Implement a Java class that contains the static methods of transforming XML documents or structures. Within this Java class you can also write messages to a trace that is visible in the message monitoring using the AbstractTrace object.
The PCK does not support the AbstractTrace object.
2. Include the method calls in the XSLT mapping program (see below).
3. Import the XSLT mapping program and the Java class as an archive to the Enterprise Services Repository. You can also use two different archives. The archive with the Java class must be in the same or an underlying software component version of the XSLT mapping program (see also: XSLT and Java Mapping).
The following example illustrates the procedure using a simple XSLT mapping for a message.
The following table shows the message instance that you want to transfer to the target message, using an XSLT mapping program with Java enhancement.
Source Instance |
Target Instance |
<person> <first-name>Robert</first-name> </person> |
<person> <name>Robert White</name> </person> |
You must link the <first-name> and <last-name> elements using a concat() Java method. You also want to write a message to the mapping trace in this method. Carry out the following steps in the XSLT mapping program in which you want to call this Java method:
...
1. Declare the Java class with a namespace definition as an attribute of the <xsl:stylesheet> element. The namespace name can be any string you want; in the example it is called javamap (see below). The name of the namespace comprises the string java: as the prefix, and the complete name of the class. The complete name of a Java class comprises the name of the package and the class name. In the example this is com.company.group.MappingClass.
2. Use <xsl:param> to define the parameters that you want to transfer in the method calls. In the example below, three parameters of the concat() method signature are defined correspondingly:
○ The first parameter takes the value of the <first-name> element.
○ The last parameter takes the value of the <last-name> element.
○ The parameter name inputparam is defined and enables you to transfer the constants of the StreamTransformationConstants class to the Java program.
The mapping runtime sets the parameter inputparam.
3. The method is called with the element <xsl:value-of>. Specify the method with the corresponding parameters using the attribute select. The name for the namespace javamap replaces the complete class name as a prefix.
SAP advises you to check the availability of methods before you call them. Test the XSL element <xsl:if> using the attribute test, for example.
The XSLT program looks like this:
<?xml version="1.0"
encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:javamap="java:com.company.group.MappingClass">
<xsl:param
name="first">
<xsl:value-of select="//first-name"/>
</xsl:param>
<xsl:param
name="last">
<xsl:value-of select="//last-name"/>
</xsl:param>
<xsl:param name="inputparam" />
<xsl:template match="/">
<name>
<xsl:if
test="function-available('javamap:concat')">
<xsl:value-of select="javamap:concat($first, $last, $inputparam)"/>
</xsl:if>
</name>
</xsl:template>
</xsl:stylesheet>
The concat() class method of the Java class com.company.group.MappingClass groups the values of the <first-name> and <last-name> elements of the source instance together in a string that represents the value of the <name> element of the target instance. The method also has the inputparam parameter of the Map type for writing information to the trace. You use this to first fetch an AbstractTrace object and then use its methods addInfo()or AddWarning() to transfer messages to the trace:
package com.company.group;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTrace;
import
com.sap.aii.mapping.api.StreamTransformationConstants;
public class MappingClass {
private static AbstractTrace trace = null;
public
static String concat(String first,
String
last,
Map
inputparam)
{
// write trace information
trace =
(AbstractTrace)inputparam.get(
StreamTransformationConstants.MAPPING_TRACE
);
trace.addInfo(“concat():
\nfirst-name
= “ + first +
“\nlast-name
= “ + last );
// return concatentation
return
first + ' ' + last;
}
//...
}