我的代码类似于:
public static void transform(InputStream in, Source xslt, OutputStream out) throws TransformerException {
//com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer t = tFactory.newTransformer(xslt);
Source xmlSource = new StreamSource(in);
Result outResult = new StreamResult(out);
t.transform(xmlSource, outResult);
}
当我尝试转换我的xml时:
java.net.MalformedURLException
ERROR: 'Cannot compile stylesheet'
CRITICAL ERROR: 'java.net.MalformedURLException'
:null
javax.xml.transform.TransformerConfigurationException: java.net.MalformedURLException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
at it.unimaticaspa.digipark.utils.XmlUtils.transform(XmlUtils.java:42)
at it.unimaticaspa.digipark.utils.XmlUtilsTest.testTransform(XmlUtilsTest.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.MalformedURLException
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(Unknown Source)
... 27 more
Caused by: java.lang.NullPointerException
... 41 more
在这一行:
tFactory.newTransformer(xslt);
我认为这是因为xml和xslt都声明了没有指向现有xsd的命名空间,所以我想禁用模式的验证,我看到函数setAttribute和setFeature,但我可以'找到任何工作示例,或可用的属性/功能列表。 调试代码我发现xml转换器的实现类是: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
- 编辑 -
根据建议,我也发布了我的文件:
的xml:
<?xml version="1.0" encoding="utf-8"?>
<test:Root xmlns:test="http://www.mysite.it/test/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mysite.it/test/xsdTest.xsd">
<test:Branch>
<test:Leaf>someValue1</test:Leaf>
</test:Branch>
<test:Branch>
<test:Leaf>someValue2</test:Leaf>
<test:Leaf>someValue3</test:Leaf>
</test:Branch>
<test:Branch>
<test:Sub-Branch>
<test:Leaf>someValue4</test:Leaf>
</test:Sub-Branch>
<test:Leaf>someValue5</test:Leaf>
</test:Branch>
</test:Root>
的xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xmlns:test="http://www.mysite.it/test/"
xsi:schemaLocation="http://www.unimaticaspa.it/test/xsdTest.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="test:Root">
<sincro:IdC xmlns:sincro="http://www.uni.com/U3011/sincro/"
xmlns:somenamespace="http://www.mysite.it/mysite-metadata/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.uni.com/U3011sincro/IdC.xsd http://www.mysite.it/mysite-metadata/Metadata.xsd"
sincro:url="http://www.uni.com/U3011/sincro/"
sincro:version="1.0">
<sincro:VdC>
<sincro:ID sincro:scheme="local">asd</sincro:ID>
</sincro:VdC>
<xsl:for-each select="test:Branch">
<sincro:FileGroup>
<sincro:File sincro:encoding="BASE64" sincro:format="TXT">
<sincro:ID>asd</sincro:ID>
<sincro:MoreInfo sincro:XMLScheme="http://www.mysite.it/mysite-metadata/Metadata.xsd">
<sincro:EmbeddedMetadata>
<somenamespace:Metadata>
<somenamespace:mytag>asd</somenamespace:mytag>
</somenamespace:Metadata>
</sincro:EmbeddedMetadata>
</sincro:MoreInfo>
</sincro:File>
</sincro:FileGroup>
</xsl:for-each>
</sincro:IdC>
</xsl:template>
</xsl:stylesheet>
- 编辑 -
在标签xsl:stylesheet上添加了缺少声明:xsi:schemaLocation。 仍然不起作用,它一直给出同样的错误。
我尝试使用XMLSpy运行我的xslt并且它工作正常,我真的认为问题出在TransformerFactory配置上。
答案 0 :(得分:2)
这可能是因为XSLT Source(系统ID)是由代码中较早的URL定义的。此URL格式错误(或者可能为null,因此堆栈跟踪中的空指针异常)。
答案 1 :(得分:0)
XML命名空间名称不必指向XSD,或者确实指向任何东西。他们做必须是语法正确的URI,如果他们是URL,则必须是绝对URL而不是相对URL。你还没有向我们展示你的样式表,但这是第一个看待修复的东西。
编辑添加:
看看你向我们展示了什么(在这个答案的评论中),一个错误立即跳出来:
xmlns:test="mysite.it/test/";
错误。命名空间名称必须是绝对URI,而不是相对URI。将其更改为
xmlns:test="http://mysite.it/test/";
此外,分号不属于那里。删除它们,并检查正确的XML语法。