Hi folks,
The following code demonstrates a stylesheet successfully executing
under Xalan-Java Interpretive and failing under XSLTC. The problem
appears to lie with the Nodeset passed by XSL java extensions to
static methods. When interpreted I get a DTMNodeList which can be
wrangled into a Node. Under XSLTC I get a DTMAxisIterNodeList which
meets all my attempts to inspect it with null pointer and
arrayOutOfBounds exceptions.
The intention of the technique shown here is to allow an arbitrary
number of named arguements to be passed to a single resolve method.
Any help would be appreciated.
Regards,
James Prosser
Pasted below are:
1. test.xml a document to transform
2. test.xsl a stylesheet that demonstrating technique (in a raw form).
3. Test.java applies test.xsl to test.xml and provides a method called
by test.xsl.
4. The winXP command lines used to compile and run code.
5. The results of running Test.java
6. My environment
1. test.xml
===================================================================
<?xml version='1.0' encoding='UTF-8'?>
<client ClientID="123456">
<initial-consult>
<diagnosis>Autism</diagnosis>
</initial-consult>
</client>
2. test.xsl
===================================================================
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
xmlns:test="http://xml.apache.org/xalan/au.com.portal.test.xsltc"
version='1.0'>
<xsl:template match="client">
<xsl:variable name="args">
<args sub-view="client-details.xml">
<arg param="ClientID"><xsl:value-of select="@ClientID"/></arg>
<arg param="Service">Ambulatory</arg>
</args>
</xsl:variable>
<report>
<xsl:copy-of select="test:Test.resolve(xalan:nodeset($args)/args)"/>
<flag>
<xsl:value-of select="initial-consult/diagnosis"/>
</flag>
</report>
</xsl:template>
</xsl:stylesheet>
3. Test.java
==================================================================
package au.com.portal.test.xsltc;
// Sun Java API
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Test {
// values for property javax.xml.transform.TransformerFactory
static String interpreted =
"org.apache.xalan.processor.TransformerFactoryImpl";
static String compiled =
"org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
// Local file names of input and output files.
static String xslInURI = "test.xsl";
static String xmlInURI = "test.xml";
static String interpretedOutURI = "interpreted.xml";
static String compiledOutURI = "compiled.xml";
// document: return a namespace aware Document ==========================
private static Document document() {
DocumentBuilder docBuilder = null;
try { DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
docBuilder = fac.newDocumentBuilder();
}
catch (Exception e) {e.printStackTrace();}
return docBuilder.newDocument();
}
// printNode: print node to System.out ===================================
private static void printNode(Node node) throws Exception {
StringWriter stringWriter = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(node),
new StreamResult(stringWriter));
System.out.println(stringWriter.toString());
}
// transform: perform transformation =====================================
private static void transform(String outURI) throws Exception {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer t = tFactory.newTransformer(new
StreamSource(Test.xslInURI));
t.transform(new StreamSource(Test.xmlInURI),
new StreamResult(new FileOutputStream(outURI)));
}
// resolve: method called by test.xsl ====================================
// A useful resolve would return XML generated by a named template whose
// params are bound by values in nodelist. Here we just return the args.
public static Node resolve(NodeList nodeList) throws Exception {
System.out.println("--" + nodeList + "--");
// Retrieves Element from org.apache.xml.dtm.ref.DTMNodeList in
// interpreted mode but fails org.apache.xml.dtm.ref.DTMAxisIterNodeList
// in compiled mode.
Element viewElement = (Element) (document().importNode(nodeList.item(0),
true));
printNode(viewElement); // trace statement
return viewElement;
}
// main ==================================================================
public static void main(String[] args) throws Exception {
System.out.println("---- Interpreted transform
------------------------");
System.setProperty("javax.xml.transform.TransformerFactory",
interpreted);
transform(interpretedOutURI);
System.out.println("---- Compiled transform
---------------------------");
System.setProperty("javax.xml.transform.TransformerFactory",
compiled);
transform(compiledOutURI);
System.out.println("Goodbye");
}
}
4. Compile and run command lines used under Windows XP
========================
REM Compile
e:
SET XSLTCPATH=e:\cherrywood\test-bench\xsltc
SET JARPATH=%XSLTCPATH%\jar
SET
CLASSPATH=.;%JARPATH%\xalan.jar;%JARPATH%\serializer.jar;%JARPATH%\xercesImpl.jar;%JARPATH%\xml-apis.jar;
cd %XSLTCPATH%\au\com\portal\test\xsltc
javac Test.java
REM Run
e:
SET XSLTCPATH=e:\cherrywood\test-bench\xsltc
SET JARPATH=%XSLTCPATH%\jar
SET
CLASSPATH=.;%JARPATH%\xalan.jar;%JARPATH%\serializer.jar;%JARPATH%\xercesImpl.jar;%JARPATH%\xml-apis.jar;
cd %XSLTCPATH%
java au.com.portal.test.xsltc.Test
5. Output from running Test.java
==============================================
---- Interpreted transform ------------------------
[EMAIL PROTECTED]
<?xml version="1.0" encoding="UTF-8"?><args
sub-view="client-details.xml"><arg p
aram="ClientID">123456</arg><arg param="Service">Ambulatory</arg></args>
---- Compiled transform ---------------------------
[EMAIL PROTECTED]
ERROR: ''
Exception in thread "main" javax.xml.transform.TransformerException:
java.lang.N
ullPointerException
at
org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl
.java:637)
at
org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl
.java:301)
at au.com.portal.test.xsltc.Test.transform(Test.java:55)
at au.com.portal.test.xsltc.Test.main(Test.java:80)
Caused by: java.lang.NullPointerException
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source)
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source)
at au.com.portal.test.xsltc.Test.resolve(Test.java:66)
at test.template$dot$0()
at test.applyTemplates()
at test.applyTemplates()
at test.transform()
at
org.apache.xalan.xsltc.runtime.AbstractTranslet.transform(AbstractTra
nslet.java:593)
at
org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl
.java:630)
... 3 more
---------
java.lang.NullPointerException
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source)
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source)
at au.com.portal.test.xsltc.Test.resolve(Test.java:66)
at test.template$dot$0()
at test.applyTemplates()
at test.applyTemplates()
at test.transform()
at
org.apache.xalan.xsltc.runtime.AbstractTranslet.transform(AbstractTra
nslet.java:593)
at
org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl
.java:630)
at
org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl
.java:301)
at au.com.portal.test.xsltc.Test.transform(Test.java:55)
at au.com.portal.test.xsltc.Test.main(Test.java:80)
6. Environment
================================================================
#---- BEGIN writeEnvironmentReport($Revision: 1.29 $): Useful stuff
found: ----
version.DOM.draftlevel=2.0fd
java.class.path=.;e:\cherrywood\test-bench\xsltc\jar\xalan.jar;e:\cherrywood\te
t-bench\xsltc\jar\serializer.jar;e:\cherrywood\test-bench\xsltc\jar\xercesImpl.
ar;e:\cherrywood\test-bench\xsltc\jar\xml-apis.jar;
version.JAXP=1.1 or higher
java.ext.dirs=C:\Program Files\Java\jre1.5.0_03\lib\ext
version.xerces2=Xerces-J 2.7.1
version.xerces1=not-present
version.xalan2_2=Xalan Java 2.7.0
version.xalan1=not-present
version.ant=not-present
java.version=1.5.0_03
version.DOM=2.0
version.crimson=not-present
sun.boot.class.path=C:\Program
Files\Java\jre1.5.0_03\lib\rt.jar;C:\Program Fil
s\Java\jre1.5.0_03\lib\i18n.jar;C:\Program
Files\Java\jre1.5.0_03\lib\sunrsasig
.jar;C:\Program Files\Java\jre1.5.0_03\lib\jsse.jar;C:\Program
Files\Java\jre1.
.0_03\lib\jce.jar;C:\Program
Files\Java\jre1.5.0_03\lib\charsets.jar;C:\Program
Files\Java\jre1.5.0_03\classes
#---- BEGIN Listing XML-related jars in: foundclasses.java.class.path ----
xalan.jar-path=e:\cherrywood\test-bench\xsltc\jar\xalan.jar
serializer.jar-apparent.version=serializer.jar present-unknown-version
serializer.jar-path=e:\cherrywood\test-bench\xsltc\jar\serializer.jar
xercesImpl.jar-apparent.version=xercesImpl.jar from Xerces-J-bin.2.7.1
xercesImpl.jar-path=e:\cherrywood\test-bench\xsltc\jar\xercesImpl.jar
xml-apis.jar-apparent.version=xml-apis.jar from head branch of
xml-commons, tag
xml-commons-external_1_3_02
xml-apis.jar-path=e:\cherrywood\test-bench\xsltc\jar\xml-apis.jar
#----- END Listing XML-related jars in: foundclasses.java.class.path -----
version.SAX=2.0
version.xalan2x=Xalan Java 2.7.0
#----- END writeEnvironmentReport: Useful properties found: -----
# YAHOO! Your environment seems to be OK.