1

This is a followup to an answer on a previous question I had about XSLT.

To recap, I didn't realize that without EXSLT, XSLT wouldn't let you dynamically create an xpath expression with string values. One of the suggested workarounds was

to query the input document's DOM before you execute the transform, and pass the node-set into the transform

I was using Apache Ant to do the transformation, and per the manual on the xslt/style task's parameters

Text value to be placed into the parameter. Was originally intended to be an XSL expression.

it sounds like Apache Ant doesn't support this. It got me wondering though, how would this semantics of this work in a system that did support this?

So, what Toolchains or systems support passing a nodeset from the source document into a transformation as a parameter. Bonus points for example code.

Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • you need just to pass parameters to the xslt via ant or you want to pass part of the build file as a parameter to the xslt stylesheet? – Alex Nikolaenkov Feb 19 '11 at 12:44

2 Answers2

0

You can't pass a nodeset as a parameter in standard xslt 1.0. To do that you must use an xslt 2.0 parser.

For instance: http://wiki.apache.org/ant/UsingAntWithXSLT20AndSaxon

Yoeri
  • 1,876
  • 15
  • 24
  • Node Set is a valid data type for XSLT/XPath 1.0 and you can pass them as parameter. How to do it? It depends on the XSLT processor. –  Feb 20 '11 at 23:42
0

I'm not completely sure about my answer because it seems that I cannot understand your question properly but basing on the thread you reference and your quotes I can come up with the following suggestion:

build.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project name="Test XSLT" default="test-xslt" basedir=".">
    <target name="test-xslt">
        <xslt in="test.xml" style="ant-with-param.xsl" out="ant-with-param-out.xml">
            <param name="param-set-id" expression="2"/>
        </xslt>
    </target>
</project>

test.xml:

<?xml version="1.0" encoding="UTF-8"?>

<params>
    <set id="1">
        <param name="name" value="Name from the first set"/>
    </set>
    <set id="2">
        <param name="name" value="Name from the second set"/>
    </set>
</params>

ant-with-param.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl">
    <xsl:param name="param-set-id"/>
    <xsl:variable name="param-set" select="//params/set[@id = $param-set-id]"/>

    <xsl:template match="/">
        <name>
            <xsl:value-of select="exsl:node-set($param-set)//param[@name = 'name']/@value"/>
        </name>
    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<name>Name from the second set</name>

Given stylesheet fetches parameters from the input document basing on the value of the variable passed from the build-file. Parameters are fetched with the help of the XPath expression from the source document and used later with the help of the exsl:node-set() extension function. By default ant uses Xalan as an xslt processor. Full list of its extensions can be found at the project's home page.

Alex Nikolaenkov
  • 2,505
  • 20
  • 27