简体   繁体   中英

What Toolchains Support Passing in a Nodeset to an XSLT Transformation as a Parameter?

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM