title: Running custom Java functions within XSLT and SAXON (9.1.8)
body: <p><strong>Meta</strong></p>
  
  <ul>
  <li>Saxon XSLT processor (v. 9.1.8)</li>
  <li>Java</li>
  <li>XSLT 2.0</li>
  </ul>
  
  <p>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). <em>How is this possible?</em> 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?</p>
  
  <p>The transformation should copy all XML data and (return &amp;) display the dimension of imagefiles. </p>
  
  <p><strong>My XSL Transformation</strong></p>
  
  <pre><code>&lt;xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ImageInfo="java:ImageInfo"
  version="2.0"&gt;
  
  &lt;xsl:output method="xml"/&gt;
  &lt;xsl:template match="@* | node()"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@* | node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;xsl:template/&gt;
  
  &lt;xsl:template match="img"&gt;
      &lt;xsl:copy&gt;
          [image] file found: &lt;xsl:value-of select="ImageInfo:getImageWidth(@src)"/&gt; x &lt;xsl:value-of select="ImageInfo:getImageHeight(@src)"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p><strong>Java Class</strong></p>
  
  <pre><code>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();
  }
  }
  </code></pre>
  
  <p><strong>Saxon command line call (via .BAT)</strong></p>
  
  <pre><code>java -jar "%~dp0\saxonb9-1-0-8j\saxon9.jar" -s:"data.xml" -xsl:"transformation.xsl" -o:"result.xml"
  </code></pre>