XSL parsing problems

I'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;

}

}

}

-

[4237 byte] By [brockjonesa] at [2007-10-2 12:26:12]
# 1

There is a simple and effective solution for XSL parsing. One built in class is avaiable which will handle the complete process. you have to give xml & xsl file as its input. the out put will be generated as html.

see the following link

http://www.15seconds.com/issue/010810.htm

Deepu_Inda at 2007-7-13 9:19:47 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

I've simplified the code quite a bit since my first code (I'm new to this stuff, so there were some roundabout elements in there). However - I'm still having a problem (although not necessarily the same problem).

The new Java code looks like this (minus extraneous bits/error handling):

public void generateHTMLReport( OutputStream out, String strXslSheet ){

File f = new File( strXslSheet );

if( f.exists() ){

ByteArrayOutputStream bos = new ByteArrayOutputStream();

// Generate the XML data we will work from

generateXMLReport( bos );

bos.flush();

// Convert the XML to HTML by applying an XSL transform

ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray() );

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer( new StreamSource( strXslSheet ) );

transformer.transform( new StreamSource( bis), new StreamResult( out ) );

}

}

Now the transformation takes place, but it's not correct, whole parts of the transform are just skipped over or done incorrectly. I've narrowed it down to something to do with using an <xsl:sort ...> element. Wherever the XSL includes a sort, the result is an empty set. Inefficiency aside, the following does not work correctly:

<xsl:template name="totalDamageTable">

<xsl:param name="matchSet"/>

<xsl:variable name="maxValue">

<xsl:for-each select="$matchSet">

<xsl:sort select="total" data-type="number" order="descending"/>

<xsl:if test="position() = 1">

<xsl:value-of select="number( total )" />

</xsl:if>

</xsl:for-each>

</xsl:variable>

</xsl:template>

If I remove the sort directive, I get the first value in the list, as I would expect.

I am just using the default implementation with the 1.5.0_06 JDK. The interesting thing is that the issue does not reproduce against 1.4.2.

brockjonesa at 2007-7-13 9:19:47 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...