On Sun, Jul 08, 2001 at 04:52:30PM -0500, Storoe, Shawn wrote:
> If I have a program that creates xml files with xsl to format it into
> proper html and I just want to display with output within the velocity
> templates, what is the best way to go about doing this?
This gives me the opportunity to answer a question which I posted
earlier but which went unanswered (I have since worked out a solution).
Firstly, enable the XSLT service :)
Now, I don't know what best suits you needs, but two possibilities are:
(a) Inserting the transformed data into the template context for use
within a template's output:
import org.apache.turbine.services.xslt.TurbineXSLT;
[...]
TurbineXSLT.transform( stylesheetName, new StringReader( inputString ),
outputStringWriter );
context.put( "name", outputStringWriter.toString() );
(And all the variations upon that theme.)
(b) Cancelling the template output and providing your own response via
the output of the XSLT (and if there is an exception during the
transform you can still drop through to an error template):
import org.apache.turbine.services.xslt.TurbineXSLT;
[...]
TurbineXSLT.transform( stylesheetName, \
reader, \
stringWriter );
// get hold of the servlet's response
PrintWriter pw = data.getResponse().getWriter();
// now that we are reasonably sure of success, bypass the template
data.declareDirectResponse();
// set our response headers
data.getResponse().setContentType( "text/html" );
data.getResponse().setHeader( "Content-Length", \
new Integer( stringWriter.getBuffer.length() ).toString() );
// ...and the encoding, if you can
// write it (using the reponse PrintWriter, to handle characters)
pw.print( resultBuffer );
pw.flush();
return;
Of course some thing may seem like a waste of resources (using
the StringWriters, PrintWriters, etc.) so you could do it simpler
and more direct, I guess, but you might end up with empty responses
or raw tracebacks if an error occurs (but that's just speculation---I
haven't actually tried it :)
Someone else may be able to provide a more informed strategy to do this,
but the above should work anyway.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]