如何从其他模板提供变量动态值?

时间:2013-04-10 12:25:13

标签: xslt-1.0

    <xsl:template name="split">
    <xsl:param name="list"/>
        <xsl:variable name="first">
                         <xsl:value-of select="substring-before($list,' ')"/>
                    </xsl:variable>
        <xsl:copy-of select="$first"/>
</xsl:template>

           <xsl:variable name="test">c0 c1 c2 c3 c4</xsl:variable>
                <xsl:variable name="var2>
                  <xsl:call-template name="split">
                        <xsl:with-param name="returnvalue">
                             <xsl:value-of select="$test"></xsl:with-param>
                  </xsl:call-template>
              </xsl:variable>

//处理完成

我希望从模板返回值为c0然后返回模板匹配进行处理然后再次转到拆分模板返回c1完成相同处理然后返回拆分模板然后再次处理匹配模板;取决于测试变量的值......

我怎样才能逐一检索这些值并处理代码.. ??

1 个答案:

答案 0 :(得分:0)

此样式表将向前扫描输入字符串:

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

    <xsl:output method="text"/>

    <xsl:variable name="string" select="'c0 c1 c2 c3 c4'"/>
    <xsl:variable name="delim" select="' '"/>

    <xsl:template match="/">
        <xsl:call-template name="wrapper">
            <xsl:with-param name="string" select="$string"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="wrapper">
        <xsl:param name="string"/>
        <xsl:choose>
            <!-- handle empty input -->
            <xsl:when test=" $string = '' "/>

            <!-- handle next token -->
            <xsl:when test="contains($string, $delim)">
                <xsl:call-template name="process">
                    <xsl:with-param name="substring" select="substring-before($string,$delim)"/>
                </xsl:call-template>
                <xsl:call-template name="wrapper">
                    <xsl:with-param name="string" select="substring-after($string,$delim)"/>
                </xsl:call-template>
            </xsl:when>

            <!-- handle last token -->
            <xsl:otherwise>
                <xsl:call-template name="process">
                    <xsl:with-param name="substring" select="$string"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="process">
        <xsl:param name="substring"/>
        <xsl:text>RECEIVED SUBSTRING: </xsl:text>
        <xsl:value-of select="$substring"/>
        <xsl:text>&#x000A;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

产生以下输出:

RECEIVED SUBSTRING: c0
RECEIVED SUBSTRING: c1
RECEIVED SUBSTRING: c2
RECEIVED SUBSTRING: c3
RECEIVED SUBSTRING: c4
相关问题