cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT Version

itabhishek9
Participant
0 Kudos

Hi SDNites,

I am implementing mapping via XSLT and have to get the system date and came to know that the cotext functions of date and time are available from XSLT 2.0. Can you please let me know,

1. When writing code for XSLT, only place where I can see the version is at the top <?xml version = "1.0".   If I change it to 2.0 and import into PI will it work.

2. Do we need to have different compilers for version 1.0 and 2.0?

3. In case of version 1.0, how can I get the system date?

4. What are the limitations of XSLT over JAVA mapping i.e. when we are evaluating which mapping will suffice the need to meet the requirement.

Abhi

Accepted Solutions (0)

Answers (6)

Answers (6)

itabhishek9
Participant
0 Kudos

Thanks Navdeep and Evgeniy for helping me with this issue.

I am now able to resolve it. The resolution was - Project in which Class is present has to be explicitly mentioned in ClassPath in configuration.

I will now import the same in PI / PO and hopefully it should be a smooth walk.

Regards,

Abhi

hugozam
Member
0 Kudos

Can you please provide the final sample code that work for you. I have the same problem and I cannot get to fix it.

former_member751591
Participant
0 Kudos

Thanks for coming to SAP Community for answers. Please post your question as a new question here:

Since you're new in asking questions here, check out our tutorial about asking and answering questions (if you haven't already), as it provides tips for preparing questions more effectively, that draw responses from our members.

Please note, that your post here won't be answered.

itabhishek9
Participant
0 Kudos

Thanks Navdeep and Evgeniy for sharing the code snippets.

I have done the same as you have mentioned but it is not working. Let me put the steps that I have followed.

1. I have written the code in Eclipse and the test results I have mentioned in from Eclipse. Once it works fine here I will move it to PI / PO.

2. Have created a Project and within that project created XSLT and also the Class and method which needs to be called inside it.

Below is the code snippets from my end,

Class and method,

package pack_java_within_xslt;

public class Class_java_within_xslt {

public static String test() {
  String var = "abc";
  return var;
}
}

XSLT Code

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:testclass="java://pack_java_within_xslt.Class_java_within_xslt">

<xsl:value-of select="testclass:test()"></xsl:value-of>

Below is the error I get in Eclipse

09:17:33,412 DEBUG [debugger] XalanStyleFrame  -  name for-each

(Location of error unknown)javax.xml.transform.TransformerException: Instance method call to method test requires an Object instance as first argument

Regards,

Abhi

former_member190293
Active Contributor
0 Kudos

Hi Abhishek!

Just for case: try to define string parameter for your method and call it from XSLT with any dummy value.

Regards, Evgeniy.

itabhishek9
Participant
0 Kudos

Hi Evgeniy,

As suggested, I have made below changes and still getting the error,

JAVA Code

package pack_java_within_xslt;

public class Class_java_within_xslt {

public static String test(String Test) {
  return Test;
}
}

XSLT Code

<xsl:variable name="Test_Var" select="'abc'" />

<xsl:value-of select="testclass:test($Test_Var)"></xsl:value-of>

14:36:55,372 DEBUG [debugger] XalanStyleFrame  -  name for-each

(Location of error unknown)java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.test([ExpressionContext,] ).

Regards,

Abhi

former_member190293
Active Contributor
0 Kudos

Hi Abhishek!

By the way: what is your PI/PO version/SP?

Don't you eventualy use XML toolkit in your operation mapping?

Regards, Evgeniy.

Snavi
Active Participant
0 Kudos

Hi Abhishek,

I believe you are trying to test this in eclispe and not in PI/PO.

  • you need to make sure the eclipse has the correct xslt processor jar files for java extensions.
  • i am using xalan and the same code works for me in eclipse
  • add your java project to the classpath in your test configuration
  • the namepace testclass in your xsl should be the package.class when you test in eclipse

And if you use the code provided by Evgeniy in PI/PO it will work

former_member190293
Active Contributor
0 Kudos

Hi Abhishek!

I guess there are two ways to fix it:

1. Import Saxon XSLT processor and use it for your XSLT transformations.

2. For current Xalan processor try following syntax in your transformation:

xmlns:testclass="xalan://pack_java_within_xslt.Class_java_within_xslt"

But first try to remove "//" from namespace definition.

Before:

xmlns:testclass="java://pack_java_within_xslt.Class_java_within_xslt">

After:

xmlns:testclass="java:pack_java_within_xslt.Class_java_within_xslt">

Regards, Evgeniy.

itabhishek9
Participant
0 Kudos

Thanks everyone for the repsonse.

As in my current requirement, I have to get the system date within XSLT version 1.0 for which I have adopted the route of calling JAVA method. But when I do it, I am geeting the error as,

Instance method call to method test requires an Object instance as first argument.

I have used following syntaxes,

1. xmls:testclass="package name.classname"
2. xmls:testclass="xalan://package name.classname"
3. xmls:testclass="urn:java://package name.classname"

And then used testclass.test() to call the method.

Please guide why the above mentioned error is being obtained.

Regards,
Abhi

Snavi
Active Participant
0 Kudos

Hi Abhishek,

if you want to use xslt 1.0 and call java method, please refer below sap help link for your requirement.

your syntax should be like

namespace declatation

xmlns:testclass="java:package.classname"

and then use it as testclass:methodname(parameters)

XSLT Mapping with Java Enhancement - Managing Services in the Enterprise Services Repository - SAP L...

former_member190293
Active Contributor
0 Kudos

Hi Abhishek!

With XSLT 1.0 I would recommend the following:

1. Create some wrapper class for using Date/Time java functions:

package com.mycompany.xpi.utils.xslt;

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class DateTimeHelper {

       public static String getTimeStamp() {

             String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

              return timeStamp;

       }

}

2. Import class in ESR as Imported Archive.

3. Use it in your XSLT 1.0 transformation like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

   xmlns:datetime="java:com.mycompany.xpi.utils.xslt.DateTimeHelper"

   exclude-result-prefixes="datetime">

<xsl:template match="/*">

     <DateTimeHelper>

          <TimeStamp>

               <xsl:value-of select="datetime:getTimeStamp()"/>

          </TimeStamp>

     </DateTimeHelper>

</xsl:template>

</xsl:stylesheet>

Regards, Evgeniy.

former_member190293
Active Contributor
0 Kudos

Hi, Abhishek!

Version of XSLT stylesheet is defined in its root element:

<xsl:stylesheet version="2.0">

For list of date/time functions you should refer to documentation for used XSLT processor. For example, for Saxon processor you can look here:

XSLT 2.0 and XPath 2.0 Functions

About choosing between java and XSLT mappings: in my opinion, java mappings should be used in specific cases such as working with attachments, encoding/decoding operations, encrypting/decrypting messages or its parts, working with message data that can't be accessed with standard methods - in other words, in cases, where you can't get desired result with graphical or XSLT mapping.

XSLT mapping, in its turn, can amazingly help in case of complex transformation such as document tree reorganization, elements groupping and so on.

Java mapping code is usualy more complicated and it requires a good knowledge of Java basics.

Regards, Evgeniy.

former_member186851
Active Contributor
0 Kudos

Hello Abhishek

Recent discussion on handling XSLT 2.0

you can follow this and compile XSLT 2.0.

And performance is the issue for Java mapping as Nitin suggested but just for time functions It should be ok to use Java also and for current date you can use time functions in graphical, Guess this will return the system time only

nitindeshpande
Active Contributor
0 Kudos

Hi Abhishek,

You can go through the below blog on how to use XSLT 2.0 version in your SAP PI/PO system -

Regarding your XSLT vs Java query, XSLT is preferred when you have deep context and the context handling becomes difficult in Graphical Mapping. Java mapping degrades the performance, hence suggested to do it with either XSLT or Graphical.

Regards,

Nitin