如何在xsl中设置变量名动态

时间:2012-12-18 10:22:15

标签: xslt

以下是我的代码,我想给出隐藏变量并为其分配值以便稍后访问。

 <xsl:for-each select="//Root/Record">
     <xsl:if test="(@CIMtrek_accountlist_customer_number != '') ">
     <option style="padding:5px;">
    <xsl:attribute name="class">>
    <xsl:choose>
            <xsl:when test="(position() mod 2) = 0">
            AlternateRowOne
</xsl:when>
<xsl:otherwise>
AlternateRowTwo
</xsl:otherwise>
    </xsl:choose>
            </xsl:attribute>
// here i want to set hidden varialble and assign the value for it
        <xsl:attribute name="value">
                    <xsl:value-of
            select="@CIMtrek_accountlist_customer_number" /></xsl:attribute>
        <xsl:value-select="@CIMtrek_accountlist_customer_number" />
                </option>
                </xsl:if>
                </xsl:for-each>

变量名称将类似于此类

<input type="hidden"
name="hdnDialogListCtrlPlaceholder_CIMtrek_DailyshipCo_Destination_"+i
id="hdnDialogListCtrlPlaceholder_CIMtrek_DailyshipCo_Destination_"+i
 />

where i =0
and i++;

name_1
name_2
name_n

可以使用:<FieldRef Name="<FieldInternalName>" Explicit="TRUE"/> 如何在xsl中执行此操作

2 个答案:

答案 0 :(得分:2)

我通常以下列方式实现递归。

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

    <xsl:template match="/">
        <xsl:call-template name="recur">
            <xsl:with-param name="max_recursions" select="5"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="recur">
        <xsl:param name="n">0</xsl:param>
        <xsl:param name="max_recursions"/>
        REPEATING UNIT HERE
        <xsl:if test="$max_recursions != $n">
            <xsl:call-template name="recur">
                <xsl:with-param name="n" select="$n + 1"/>
                <xsl:with-param name="max_recursions" select="$max_recursions"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

虽然并不经常建议,但通常可以通过构建良好的XPath实现更快,更简洁,更易读的代码。

答案 1 :(得分:1)

您正在尝试在XSLT中编写过程代码,但这不起作用。如果你解释你想要执行什么转换(输入是什么,输出是什么,以及它们是如何相关的?)那么我们可以向你展示如何以“XSLT方式”,即声明性地实现它。