Java Xslt 20 Example

Use

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.

Caution

The working draft of the specification for http://www.w3.org/TR/xslt20/Information published on non-SAP site (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.

Activities

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 .

  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.

Example

Using XML Toolkit

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> <last-name>White</last-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.

      Note

      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.

    Note

    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; }

//...

}

Migrating XSLT Mappings Created With SAP XML Toolkit to JDK Toolkit

When you have developed an XSLT mapping using the SAP XML toolkit (see example above), and you want to make this mapping work with JDK Toolkit (XSLTC), you have to perform the following steps.

  1. Use the XSLTC cast function ( xsltc:cast) to convert expressions to the proper type in the following cases:

    • Access to complex objects used as parameters

    • Objects on which methods are called

    Make sure that the xsltc name space is present in the name spaces by using the following syntax: xmlns:xsltc="http://xml.apache.org/xalan/xsltc".

  2. When specifying name spaces, do not use package names for Java extensions name spaces. Instead of that, use fully qualified class names. Otherwise, backward compatibility with the SAP XML Toolkit cannot be guaranteed.

  3. Apply the patches described in SAP note 1731772 Information published on SAP site.

An example 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:xsltc="http://xml.apache.org/xalan/xsltc"

xmlns:map="java.util.Map"

xmlns:dyn="com.sap.aii.mapping.api.DynamicConfiguration"

xmlns:key="com.sap.aii.mapping.api.DynamicConfigurationKey">

<xsl:output indent="no" />

<xsl:param name="inputparam"/>

<xsl:template match="/">

<!-- change dynamic configuration -->

<xsl:variable name="dynamic-conf" select="map:get(xsltc:cast('java.util.Map',$inputparam), 'DynamicConfiguration')" />

<xsl:variable name="dynamic-key" select="key:create('http://sap.com/xi/XI/System/File', 'Directory')" />

<xsl:variable name="dynamic-value" select="dyn:get(xsltc:cast('com.sap.aii.mapping.api.DynamicConfiguration',$dynamic-conf), $dynamic-key)" />

<xsl:variable name="new-value" select="concat($dynamic-value, 'subfolder\')" />

<xsl:variable name="dummy" select="dyn:put(xsltc:cast('com.sap.aii.mapping.api.DynamicConfiguration',$dynamic-conf), $dynamic-key, $new-value)" />

<!-- copy payload -->

<xsl:copy-of select="." />

</xsl:template>

</xsl:stylesheet>

<>

More information: SAP note 1731772 Information published on SAP site

haganforneonand.blogspot.com

Source: http://saphelp.ucc.ovgu.de/NW750/EN/4b/f40f2cc0c33de4e10000000a42189e/content.htm

0 Response to "Java Xslt 20 Example"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel