【发布时间】:2013-12-30 17:20:51
【问题描述】:
我的代码是这样的:
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 配置上。
【问题讨论】:
-
你能显示输入 XML 和 XSLT 代码的 sn-ps 吗?
-
如何创建作为
xslt参数传递给此方法的Source?我看不出 XSLT 本身有什么问题,但是如果这个Source配置不正确,那么它可能会产生您所看到的那种错误。 -
你说得对,我终于找到了错误:我正在从一个不存在的文件中的 null InputStream 创建一个 Source,错误消息让我走错了路。 . 这样一个微不足道的错误真是浪费时间......
-
我不知道如何关闭帖子。我可以回复我自己的问题并签名作为正确答案吗?
标签: xslt transform xerces xalan