XSL parsing problems
843810Feb 15 2006 — edited Feb 20 2006I'm trying to dynamically create HTML content from dynamically created XML (via XSLT) content in an app I'm working on. This is the first time I've worked with XSLT and I'm relatively new to XML, as well.
Ultimately, I'm getting the following warnings when attempting to parse my XSL file:
Document is invalid: no grammar found.
Document root element "xsl:stylesheet", must match DOCTYPE root "null".
Ultimately, the XSL transformation doesn't happen correctly. It seems to partially work, but many of the templates just don't get applied. I've verified that if I just generate an XML file and specify the XSL sheet in an xml-stylesheet directive, the style applies correctly.
Here are the top few lines from the XSL file:
-------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="ISO-8859-1" indent="yes" />
-------------
Also, here is the (somewhat simplified) Java code that produces the problem:
-------------
public void generateXML( OutputStream out ){
// Build our output stream result.
StreamResult streamResult = new StreamResult(out);
// Construct a transformer handler
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = null;
try{
hd = tf.newTransformerHandler();
}catch( TransformerConfigurationException tcex ){
tcex.printStackTrace();
return;
}
// Set the destination for the tansformer handler.
hd.setResult(streamResult);
// Start the document
try{
hd.startDocument();
}catch( SAXException saxex ){
saxex.printStackTrace();
return;
}
AttributesImpl atts = new AttributesImpl();
try{
hd.startElement("","","analysisReport",atts);
atts.clear();
// Spin through a list adding elements...
hd.endElement( "","","analysisReport");
}catch( SAXException saxex ){
saxex.printStackTrace();
}
try{
hd.endDocument();
}catch( SAXException saxex ){
saxex.printStackTrace();
return;
}
}
public void generateHTML( String strXslSheet ){
File f = new File( strXslSheet );
if( f.exists() ){
// Generate the XML data we will work from
ByteArrayOutputStream bos = new ByteArrayOutputStream();
generateXML( bos );
try{
bos.flush();
}catch( IOException ioex ){
ioex.printStackTrace();
return;
}
ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray() );
// Create a stream source from the XML data
StreamSource source = new StreamSource( bis );
// Build up a transformer to transform the XML according to the style
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setValidating( true );
DocumentBuilder db = null;
Document xslt = null;
Transformer transformer = null;
try{
db = dbf.newDocumentBuilder();
}catch( ParserConfigurationException pcex ){
pcex.printStackTrace();
return;
}
try{
xslt = db.parse( f );
}catch( IllegalArgumentException iaex ){
iaex.printStackTrace();
return;
}catch( IOException ioex ){
ioex.printStackTrace();
return;
}catch( SAXException saxex ){
saxex.printStackTrace();
return;
}
DOMSource xsltSource = new DOMSource( xslt );
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
try{
transformer = tf.newTransformer( xsltSource );
}catch( TransformerConfigurationException tcex ){
tcex.printStackTrace();
return;
}
// Build up a destination based on the output stream
StreamResult destination = new StreamResult( out );
// Do the transformation
try{
transformer.transform( source, destination );
}catch( TransformerException tex ){
tex.printStackTrace();
return;
}
}
}
-------------