cancel
Showing results for 
Search instead for 
Did you mean: 

How to use standard java functions in a XSLT mapping

Former Member
0 Kudos

Hi All,

I wish to use a standard java function in a XSLT mapping, The issue is either i am giving incorrect namespace which is used to invoke the function or the signature of the function call is incorrect, I have read all the links in http://help.sap.com, and i know <b> one can enhance a XSLT mapping by writing one's own java code and thereby using java standard functions </b>, but the requirement is such that i need to try and use java standard function in XSLT mapping itself.

Please refer to the sample code below:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:javamap="java:java.lang.String">

<xsl:output method="text"/>

<xsl:template match="/">

<xsl:variable name="input" select="Title">

<xsl:if test="function-available('javamap:toUpperCase')">

<xsl:value-of select="javamap:toUpperCase($input)"/>

</xsl:if>

Author:<xsl:value-of select="Author"/>

</xsl:template>

</xsl:stylesheet>

error encountered is: Illegal number or type of arguments.

please reply if you have tried a similar scenario in SAP XI.

Thanks & Regards,

Varun

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

Hi Varun,

It is not mentioned in the documentation, but I assume that you can only use static functions. Anyway 'toUpperCase' has no parameters, you use it with a parameter. This cannot work.

I think you can use an XSLT function for your request:

http://www.w3schools.com/xpath/xpath_functions.asp#string

Check it out.

Regards

Stefan

Answers (5)

Answers (5)

former_member187339
Active Contributor
0 Kudos

Hi Varun,

Have a look at the following link:

http://help.sap.com/saphelp_nw04/helpdata/en/55/7ef3003fc411d6b1f700508b5d5211/content.htm

There its written:

<i> Implement a Java class that contains the static methods of transforming XML documents or structures.</i>

I think the function should be a <b>static</b> one.

Try using any static function from

http://www.cafeaulait.org/course/week4/53.html

Regards

Suraj

Former Member
0 Kudos

Hi Varun,

First of all i want to tell you that as per the documentation you can only call the static function inside xslt mapping. Your toUpperCase method is a non static function.

What i am getting is that you have an element called Author and you want to convert its value into uppercase.

you can write your own user defined function which is static.

Signature of your java method :

public static string toUpperCase(String Author,Map inputparam)

try this xslt map.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:javamap="java:JavaProgram">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="Author">

<xsl:value-of select="//Author_name"/>

</xsl:param>

<xsl:param name="inputparam" />

<xsl:template match="/">

<Author>

<xsl:if test="function-available('javamap:toUpperCase')">

<xsl:value-of select="javamap:toUpperCase($Author,$inputparam)"/>

</xsl:if>

</Author>

</xsl:template>

</xsl:stylesheet>

Hope this will work.

Thanks and Regards

Vishal Kumar

Former Member
0 Kudos

Hi Varun,

You should be using one more parameter called $inputparam. When you make the java code call then it should be like,

<xsl:value-of select="javamap:toUpperCase($input, $inputparam)"/>

This $inputparam is to hold the value that your Java method returns. This means that in your java method also you should be you should include one more parameter as follows:

Map inputparam.

More info can be obtained from this link.

http://help.sap.com/saphelp_nw04/helpdata/en/55/7ef3003fc411d6b1f700508b5d5211/content.htm

Cheers

JK

PS: Award points if this helps you

Former Member
0 Kudos

Hi Varun,

check with hardcoded values like...

<xsl:variable name="input" select="'myTitle'">

<xsl:if test="function-available('javamap:toUpperCase')">

<xsl:value-of select="javamap:toUpperCase( $input)"/>

Here value 'myTitle' will be assigned to variable 'input'.

Refer this link for more information.

http://www.edankert.com/transforms/foil33.html

Hope this helps.

Regards,

Uma

udo_martens
Active Contributor
0 Kudos

Hi Varun,

errormessage: "Illegal number or type of arguments"...

Did your javaprogramm work with only one parameter? If yes, which type? (should be string, convert in java programm)

You want to convert letters to Upper Case? How do your program behavior, if it get an empty string? Coz your variable input is selecting "Title". That would work only if you have only root tag 'Title'. Else you get an empty string. Better use select="//Title", what is taking first found element called "Title".

Change your select attribute.

And put a second constructor (javamap())to your java programm, put there only: return ""; to avoid error message if parameter list is empty.

Regards,

Udo

Former Member
0 Kudos

Hi Varun,

Please check out if these blog helps you.

/people/pooja.pandey/blog/2005/06/27/xslt-mapping-with-java-enhancement-for-beginners

Regards

Vijaya

Shabarish_Nair
Active Contributor
0 Kudos

you could look at both the following weblogs which would give you a clear picture on your requirement.

/people/jayakrishnan.nair/blog/2005/06/28/dynamic-file-namexslt-mapping-with-java-enhancement-using-xi-30-sp12-part-ii

/people/pooja.pandey/blog/2005/06/27/xslt-mapping-with-java-enhancement-for-beginners