Could someone advise me what's wrong with the XSLT transformation below? I have stripped it down to a minimum.
Basically, I would like to have a parameter "title" replaced, but I cannot get it to run. The transformation simply ignores the parameter. I have highlighted the relevant bits with some exclamation marks.
Any advise is greatly appreciated.
public class Test {
private static String xslt =
"<?xml version=\"1.0\"?>\n" +
"<xsl:stylesheet\n" +
" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n" +
" version=\"1.0\">\n" +
" <xsl:param name=\"title\" />\n" +
" <xsl:template match=\"/Foo\">\n" +
" <html><head><title>{$title}</title></head></html>\n" + // !!!!!!!!!!!
" </xsl:template>\n" +
"</xsl:stylesheet>\n";
public static void main(String[] args) {
try {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document document = db.newDocument();
document.appendChild( document.createElement( "Foo" ) );
final StringWriter resultWriter = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer( new StreamSource( new StringReader( xslt ) ) );
// !!!!!!!!!!!!!!!!!!
transformer.setParameter( "title", "This is a title" );
// !!!!!!!!!!!!!!!!!!
transformer.transform( new DOMSource( document ), new StreamResult( resultWriter ) );
System.out.println( resultWriter.toString() );
} catch( Exception ex ) {
ex.printStackTrace();
}
}
}
I'm using Java 6 without any factory-specific system properties set.
Thank you in advance!