More Than Five More Than Five - 2 months ago 21
Java Question

Can't get setParameter to work in XSLT transformer

Trying to pass a parameter to a XSLT stylesheet. Here is my java code...

public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
String filename = "MyFile.xml";
Source xslt = new StreamSource(new File("MyXSL.xsl"));
Transformer transformer = factory.newTransformer(xslt);
transformer.setParameter("dynevidence", filename);

Source text = new StreamSource(new File(filename));
transformer.transform(text, new StreamResult(new File("output.html")));
}


Here is my XSLT sheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
<html>
<body>
<xsl:param name="dynevidence"/>
<B>Description</B>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


Nothing is outputted for the parameter and the markup goes straight to Description. Any ideas what I a doing wrong?

Answer

Declare <xsl:param name="dynevidence"/> as a top-level child of xsl:stylesheet, then where you want to use the value use $dynevidence.

Comments