1

Meta

  • Saxon XSLT processor (v. 9.1.8)
  • Java
  • XSLT 2.0

I got a simple example of a java class file and some xsl transformation. My goal is to run my custom java functions from the class file within the XSLT process (via SAXON). How is this possible? When I start the below described batch file the cmd displays an error calling me the function is not known to saxon. So I have to add my class to the Java / or Saxon CLASSPATH?

The transformation should copy all XML data and (return &) display the dimension of imagefiles.

My XSL Transformation

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ImageInfo="java:ImageInfo"
version="2.0">

<xsl:output method="xml"/>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
<xsl:template/>

<xsl:template match="img">
    <xsl:copy>
        [image] file found: <xsl:value-of select="ImageInfo:getImageWidth(@src)"/> x <xsl:value-of select="ImageInfo:getImageHeight(@src)"/>
    </xsl:copy>
</xsl:template>

Java Class

import javax.swing.ImageIcon;
public class ImageInfo {

String filename;
ImageIcon img;

public ImageInfo(String filename) {
    this.filename = filename;
    img = new ImageIcon(filename);
}

public int getWidth() {
    return img.getIconWidth();
}

public int getHeight() {
    return img.getIconHeight();
}
}

Saxon command line call (via .BAT)

java -jar "%~dp0\saxonb9-1-0-8j\saxon9.jar" -s:"data.xml" -xsl:"transformation.xsl" -o:"result.xml"
1
  • I understand that this question is old, but I still will be hoping for answer - where I need to put custom java extension classes so that xsl can find them? Commented Mar 14, 2017 at 14:16

2 Answers 2

1

You need the -cp option of your java command e.g. java -cp ".;%~dp0\saxonb9-1-0-8j\saxon9.jar" net.sf.saxon.Transform -s:"data.xml" -xsl:"transformation.xsl" -o:"result.xml" where you need to make sure that the directory with your ImageInfo is on the class path, I have added ., assuming the class is in your current working directory.

However, note that ImageInfo:getImageWidth(@src) would try to call a static method getImageWidth, you have instance methods, the method you have is called getWidth and it does not take an argument.

See the documentation for that old version of Saxon 9, it should be available on http://saxon.sourceforge.net/.

Sign up to request clarification or add additional context in comments.

2 Comments

extending the classpath and adding the main class from saxon helped. thanks.
Note that classpath separator will is OS-dependent - on Unix it's :
1

Once you've sorted out your classpath problems as Martin suggests, the code you want will be something like this:

<xsl:variable name="image" select="ImageInfo:new(@src)"/>
[image] file found: 
   <xsl:value-of select="ImageInfo:getWidth($image)"/> x 
   <xsl:value-of select="ImageInfo:getHeight($image)"/>

3 Comments

thank you very much. is it neccessary to create a new java object in order to call the functions?
The methods getWidth() and getHeight() are methods defined on an instance of a class, so yes, you can't call them without having an instance to apply them to.
I see. So I´ve decided to create the object within the returning type methods themselves and let them take the input parameter from the methods. Works fine now. again - thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.