Question

My code is something like:

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);

}

And when I try to transform my 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

at the line:

tFactory.newTransformer(xslt);

I think it's about the fact that both xml and xslt declare namespaces that didn't point to an existing xsd, so i thought to disable the validation of the schema, and i saw the function setAttribute and setFeature, but i can't find any working example, or a list of attributes/features available. Debugging the code i found that the implementation class of the xml transformer is: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

-- edit --

As suggested I post my files also:

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>

-- EDIT --

added missing declaration: xsi:schemaLocation on tag xsl:stylesheet. Still doesn't work, it keep giving the same error.

I tried to run my xslt with XMLSpy and it worked fine, i really think that the problem is on the TransformerFactory Configuration.

Was it helpful?

Solution

This is probably because the XSLT Source (system id) is defined by an URL earlier in the code. This URL is malformed (or null maybe, hence null pointer exception in the stacktrace).

OTHER TIPS

XML Namespace names don't have to point to XSDs, or indeed point to anything. They do have to be syntactically correct URIs, and if they're URLs they have to be absolute URLs rather than relative. You haven't shown us your stylesheet, but that's the first thing to look at fixing.

EDITED TO ADD:

Looking at what you've shown us (in the comments on this answer), one error jumps out immediately:

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

Wrong. Namespace names must be absolute URIs, not relative. Change it to

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

Also, the semicolons don't belong there. Remove them, and review proper XML syntax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top