More Than Five More Than Five - 1 year ago 120
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="">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:param name="dynevidence"/>


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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download