Вопрос

I'm facing an issue with JDK (both 1.6 and 1.7) XSLT transformations. The thing is that I want to process simple WSDL that is using xsd:import for its XSD (that lies in same location) with my XSLT transformation.

public static void main(String[] args) throws Exception {
    InputStream xmlStream = new FileInputStream("/home/d1x/temp/xslt/test.wsdl");
    String xmlSystemId = "file:///home/d1x/temp/xslt/test.wsdl";

    InputStream xsltStream = XsltTransformation.class.getResourceAsStream("wsdl-viewer.xsl");
    OutputStream outputStream = new FileOutputStream("/home/d1x/temp/xslt/output.html");
    new XsltTransformation().transform(xmlStream, xmlSystemId, xsltStream, outputStream);

}

public void transform(InputStream xmlStream, String xmlSystemId, InputStream xsltStream, OutputStream outputStream) {
    Source xmlSource = new StreamSource(xmlStream, xmlSystemId);
    Source xsltSource = new StreamSource(xsltStream);

    TransformerFactory transFact = TransformerFactory.newInstance();
    try {
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.transform(xmlSource, new StreamResult(outputStream));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

When I run my code, I get this exception that is kinda hard to debug. When I remove the import, everything works fine.

Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at com.sun.org.apache.xml.internal.utils.SuballocatedIntVector.elementAt(SuballocatedIntVector.java:438)
at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase._firstch(DTMDefaultBase.java:524)
at com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl.access$200(SAXImpl.java:76)
at com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl$NamespaceChildrenIterator.next(SAXImpl.java:1433)
at com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator.next(StepIterator.java:111)
at com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator.next(StepIterator.java:111)
at com.sun.org.apache.xalan.internal.xsltc.dom.DupFilterIterator.setStartNode(DupFilterIterator.java:96)
at com.sun.org.apache.xalan.internal.xsltc.dom.UnionIterator$LookAheadIterator.setStartNode(UnionIterator.java:78)
at com.sun.org.apache.xalan.internal.xsltc.dom.MultiValuedNodeHeapIterator.setStartNode(MultiValuedNodeHeapIterator.java:212)
at com.sun.org.apache.xalan.internal.xsltc.dom.CurrentNodeListIterator.setStartNode(CurrentNodeListIterator.java:153)
at com.sun.org.apache.xalan.internal.xsltc.dom.CachedNodeListIterator.setStartNode(CachedNodeListIterator.java:55)
at GregorSamsa.topLevel()
... etc...

WSDL itself is very simple and is using the import:

...<types>
<xsd:schema>
  <xsd:import namespace="http://mytest.com" schemaLocation="test.xsd"/>
</xsd:schema>
</types>...

Used XSLT can be found at: http://tomi.vanek.sk/xml/wsdl-viewer.xsl

Это было полезно?

Решение

I managed to solve this issue by switching to Saxon implementation of JAXP instead of built-in Java implementation. The only code change was:

TransformerFactory transFact = net.sf.saxon.TransformerFactoryImpl.newInstance();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top