使用 Java 执行调用 Java 函数的 XSLT
Using Java to perform XSLT which calls Java function
我是 XSLT 的新手。以前,我习惯于使用命令行来执行 XSLT。就像这样:
@echo off
set XALAN_JAR=%OPENCCG_HOME%\lib\xalan.jar
java -classpath .;%XALAN_JAR% org.apache.xalan.xslt.Process -IN recommend_person.xml -XSL planner.xsl -OUT people_graph.xml
这很有效,我能够在 planner.xsl
文件中调用 Java 函数。但是现在我想在 Java 程序中做同样的事情,我写了这样的程序:
/**
* Created by Zhao on 2016/8/19.
*/
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.*;
import java.io.FileOutputStream;
public class xslTest {
// 1. Instantiate a TransformerFactory.
private TransformerFactory tFactory;
// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
private Transformer transformer;
private FileOutputStream outStream;
// setup the input, output and xsl
private StreamSource xsl;
private StreamSource input;
private StreamResult output;
public void xslTest(String inputFile, String outputFile, String XSLFile) {
try {
tFactory = TransformerFactory.newInstance();
xsl = new StreamSource(XSLFile);
input = new StreamSource(inputFile);
outStream = new FileOutputStream(outputFile);
output = new StreamResult(outStream);
transformer = tFactory.newTransformer(xsl);
transformer.transform(input, output);
}catch (Exception e) {
e.printStackTrace();
}
System.out.printf(
"Transform from %s to %s using %s performed",
inputFile, outputFile, XSLFile
);
}
public static void main (String[] args) {
xslTest test = new xslTest();
String inputFile = "C:/xslt/recommend_person.xml";
String outputFile = "C:/xslt/output.xml";
String XSLFile = "C:/xslt/planner.xsl";
test.xslTest(inputFile, outputFile, XSLFile);
}
}
我的 IDE 告诉我有很多错误,例如:
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #20; Column #55; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #23; Column #65; Variable idgen is directly or indirectly referencing itself!
Transform from C:/xslt/recommend_person.xml to C:/xslt/output.xml using C:/xslt/planner.xsl performed
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #23; Column #65; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #26; Column #82; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #26; Column #82; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #29; Column #129; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #29; Column #129; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #33; Column #68; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #33; Column #68; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #36; Column #72; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #36; Column #72; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #39; Column #71; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #39; Column #71; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #51; Column #67; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #51; Column #67; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #57; Column #60; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #57; Column #60; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #60; Column #72; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #60; Column #72; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #65; Column #48; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #65; Column #48; Unknown error in XPath.
NodeIdGen
是我在 XSL 文件中使用的 Java class。当我使用命令行进行转换时效果很好。
谢谢大家。问题解决了!这与 XSLT 无关。只是一个 classpath 问题:您需要将 XSL 文件中使用的 class 文件添加到您的项目 classpath.
我是 XSLT 的新手。以前,我习惯于使用命令行来执行 XSLT。就像这样:
@echo off
set XALAN_JAR=%OPENCCG_HOME%\lib\xalan.jar
java -classpath .;%XALAN_JAR% org.apache.xalan.xslt.Process -IN recommend_person.xml -XSL planner.xsl -OUT people_graph.xml
这很有效,我能够在 planner.xsl
文件中调用 Java 函数。但是现在我想在 Java 程序中做同样的事情,我写了这样的程序:
/**
* Created by Zhao on 2016/8/19.
*/
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.*;
import java.io.FileOutputStream;
public class xslTest {
// 1. Instantiate a TransformerFactory.
private TransformerFactory tFactory;
// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
private Transformer transformer;
private FileOutputStream outStream;
// setup the input, output and xsl
private StreamSource xsl;
private StreamSource input;
private StreamResult output;
public void xslTest(String inputFile, String outputFile, String XSLFile) {
try {
tFactory = TransformerFactory.newInstance();
xsl = new StreamSource(XSLFile);
input = new StreamSource(inputFile);
outStream = new FileOutputStream(outputFile);
output = new StreamResult(outStream);
transformer = tFactory.newTransformer(xsl);
transformer.transform(input, output);
}catch (Exception e) {
e.printStackTrace();
}
System.out.printf(
"Transform from %s to %s using %s performed",
inputFile, outputFile, XSLFile
);
}
public static void main (String[] args) {
xslTest test = new xslTest();
String inputFile = "C:/xslt/recommend_person.xml";
String outputFile = "C:/xslt/output.xml";
String XSLFile = "C:/xslt/planner.xsl";
test.xslTest(inputFile, outputFile, XSLFile);
}
}
我的 IDE 告诉我有很多错误,例如:
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #20; Column #55; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #23; Column #65; Variable idgen is directly or indirectly referencing itself!
Transform from C:/xslt/recommend_person.xml to C:/xslt/output.xml using C:/xslt/planner.xsl performed
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #23; Column #65; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #26; Column #82; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #26; Column #82; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #29; Column #129; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #29; Column #129; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #33; Column #68; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #33; Column #68; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #36; Column #72; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #36; Column #72; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #39; Column #71; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #39; Column #71; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #51; Column #67; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #51; Column #67; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #57; Column #60; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #57; Column #60; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #60; Column #72; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #60; Column #72; Unknown error in XPath.
file:///C:/xslt/planner.xsl; Line #65; Column #48; Variable idgen is directly or indirectly referencing itself!
file:///C:/xslt/planner.xsl; Line #16; Column #60; javax.xml.transform.TransformerException: java.lang.ClassNotFoundException: NodeIdGen
file:///C:/xslt/planner.xsl; Line #65; Column #48; Unknown error in XPath.
NodeIdGen
是我在 XSL 文件中使用的 Java class。当我使用命令行进行转换时效果很好。
谢谢大家。问题解决了!这与 XSLT 无关。只是一个 classpath 问题:您需要将 XSL 文件中使用的 class 文件添加到您的项目 classpath.