【问题标题】:xml Transformation can't resolve namespace definitionxml 转换无法解析命名空间定义
【发布时间】: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


【解决方案1】:

这可能是因为 XSLT 源(系统 ID)是由代码前面的 URL 定义的。此 URL 格式错误(或可能为 null,因此堆栈跟踪中出现空指针异常)。

【讨论】:

  • 您的响应是最接近解决方案的:我传递的对象 Source 未绑定到 URL,而是绑定到指向不存在文件的 InputStream(我拼错了文件名),导致MalformedURLException。
【解决方案2】:

XML 命名空间名称不必指向 XSD,或者实际上指向任何东西。它们确实必须是语法正确的 URI,如果它们是 URL,它们必须是绝对 URL,而不是相对 URL。您还没有向我们展示您的样式表,但这是要修复的第一件事。

编辑添加:

查看您向我们展示的内容(在此答案的 cmets 中),立即跳出一个错误:

xmlns:test="mysite.it/test/"; 

错了。命名空间名称必须是绝对 URI,而不是相对的。改成

xmlns:test="http://mysite.it/test/"; 

另外,分号不属于那里。删除它们,并检查正确的 XML 语法。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2014-01-10
  • 1970-01-01
相关资源
最近更新 更多