尽管有很多关于这个主题的帖子,但它仍然没有帮助......
是否有一个工作示例来说明使用xslt进行csv转换xml?
我正在使用XSLT 2.0 to convert CSV to XML format
源csv文件和xslt都取自上面的线程:
@Test
public void testXSLT() throws IOException, TransformerException
{
Source inputText = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(inputFile));
Source xslt = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(xsltTemplate));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslt);
transformer.transform(inputText, new StreamResult(new File(outputFile)));
}
但它仍然说:
ERROR: 'Could not compile stylesheet'
FATAL ERROR: 'Error checking type of the expression 'funcall(unparsed-text-available, [parameter-ref(csv-uri/reference), parameter-ref(csv-encoding/reference)])'.'
:Error checking type of the expression 'funcall(unparsed-text-available, [parameter-ref(csv-uri/reference), parameter-ref(csv-encoding/reference)])'.
javax.xml.transform.TransformerConfigurationException: Error checking type of the expression 'funcall(unparsed-text-available, [parameter-ref(csv-uri/reference), parameter-ref(csv-encoding/reference)])'.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1018)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:791)
at com.ihsmarkit.product.dtccpvin.csv2xmlFXOutrightTest.testXSLT(csv2xmlFXOutrightTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: Error checking type of the expression 'funcall(unparsed-text-available, [parameter-ref(csv-uri/reference), parameter-ref(csv-encoding/reference)])'.
at com.sun.org.apache.xalan.internal.xsltc.compiler.FunctionCall.typeCheckStandard(FunctionCall.java:461)
at com.sun.org.apache.xalan.internal.xsltc.compiler.FunctionCall.typeCheck(FunctionCall.java:370)
at com.sun.org.apache.xalan.internal.xsltc.compiler.When.typeCheck(When.java:88)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Instruction.typeCheck(Instruction.java:41)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.LiteralElement.typeCheck(LiteralElement.java:198)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Template.typeCheck(Template.java:291)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.typeCheck(Stylesheet.java:654)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:412)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:483)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:568)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:977)
... 32 more
---------
Error checking type of the expression 'funcall(unparsed-text-available, [parameter-ref(csv-uri/reference), parameter-ref(csv-encoding/reference)])'.
at com.sun.org.apache.xalan.internal.xsltc.compiler.FunctionCall.typeCheckStandard(FunctionCall.java:461)
at com.sun.org.apache.xalan.internal.xsltc.compiler.FunctionCall.typeCheck(FunctionCall.java:370)
at com.sun.org.apache.xalan.internal.xsltc.compiler.When.typeCheck(When.java:88)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Instruction.typeCheck(Instruction.java:41)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.LiteralElement.typeCheck(LiteralElement.java:198)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Template.typeCheck(Template.java:291)
at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:484)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.typeCheck(Stylesheet.java:654)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:412)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:483)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:568)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:977)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:791)
at com.ihsmarkit.product.dtccpvin.csv2xmlFXOutrightTest.testXSLT(csv2xmlFXOutrightTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
答案 0 :(得分:1)
您尝试使用的Xalan XSLT处理器不支持XSLT 2,您需要切换Saxon 9(在Sourceforge和Maven上的开源HE版本中提供)。此外,您使用的API意味着XSLT 1具有XML输入源,如果您想使用该API,则需要传入虚拟XML输入。在链接示例中,CSV文件位置应该作为参数csv-uri
传入。作为替代方案,您可以使用Saxon 9的本机s9api(http://saxonica.com/html/documentation/using-xsl/embedding/s9api-transformation.html)并使用命名模板开始转换,在这种情况下,您不需要虚拟XML输入。