本文介绍了在XSLT和SAXON中运行自定义Java函数(9.1.8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Meta




  • 撒克逊XSLT处理器(v.9.1.8)

  • Java

  • XSLT 2.0



我有一个简单的java类示例文件和一些xsl转换。我的目标是在XSLT进程中通过类文件运行我的自定义java函数(通过SAXON)。 这怎么可能?当我启动下面描述的批处理文件时,cmd显示一个错误,告诉我saxon不知道该功能。所以我必须将我的类添加到Java /或Saxon CLASSPATH?



转换应该复制所有XML数据并且(返回&)显示图像文件的维度。



我的XSL转型

 < ; 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]文件:< 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命令行调用(通过.BAT )

  java -jar%~dp0\saxonb9-1-0-8j\axon9.jar -s:data.xml-xsl:transformation.xsl-o:result.xml


java 命例如 java -cp.;%~dp0\saxonb9-1-0-8j\axon9.jarnet.sf.saxon.Transform -s:data.xml-xsl:transformation .xsl-o:result.xml您需要确保包含 ImageInfo 的目录在类路径上,I已添加,假设该类位于当前工作目录中。



但是,请注意 ImageInfo:getImageWidth(@src)会尝试调用静态方法 getImageWidth ,你有实例方法,你所拥有的方法叫做 getWidth ,它没有参数。



请参阅旧版Saxon 9的文档,它应该在。


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"
解决方案

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/.

这篇关于在XSLT和SAXON中运行自定义Java函数(9.1.8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:58