How to pass XSLT params to ant.xslt() task?

Hi,

I am struggling with figuring out how to pass nested parameters to ant.xslt() task, which is done in Ant like this

<xslt basedir="doc" destdir="build/doc"
  extension=".html" style="style/apache.xsl">
    <param name="date" expression="07-01-2000"/>
</xslt>

so that those parameters could be referred to via xsl:param in XSLT itself.

Gradle documentation suggests that the proper way of passing nested elements is specifying them in a closure like this

task zip {
    doLast {
        ant.zip(destfile: 'archive.zip') {
            fileset(dir: 'src') {
                include(name: '**.xml')
                exclude(name: '**.java')
            }
        }
    }
}

This email topic suggests something similar, however it doesn’t seem to work for me as the closure gets invoked before the target XSLTProcess is actually created, moreover there is no such method as param() discovered anywhere in the current context.

task myReport << {
    ant.xslt() { p ->
        param(name: 'name', expression: 'expression') // doesn't work, also I've checked that p is null here
    }
}

So I’ve looked at the org.apache.tools.ant.taskdefs.XSLTProcess class in generated-gradle-jars and I can see that it’s got createParam() method, but I can’t wrap my head around how to call it, as ant.xslt() initiates that process and executes it right away and, as I mentioned above, the closure is invoked before XSLTProcess is instantiated.

Does anyone know how to solve this? Any help will be greatly appreciated.

Actually it does indeed call createParam() through UnknownElement.handleChildren() method, so there is no issue with it. Not sure what the problem was then…

Feel stupid now and tempted to delete this post, but I’ll leave it here, so that people will know that it works! :smile:

Ant has method naming conventions and will call setX(...), createX(...) and addX(...) methods on the task based on the xml config. See Writing Your Own Task.

I believe your snippet would translate to

ant.taskdef(name: 'xslt', ...)
ant.xslt(basedir:"doc" destdir:"build/doc" extension:".html" style:"style/apache.xsl") {
    param(name:"date" expression:"07-01-2000") 
} 

Thanks Lance, it indeed works as expected. The link is quite helpful too.