【问题标题】:how to concatenate (part of) variable to String in XSLT如何在 XSLT 中将(部分)变量连接到字符串
【发布时间】:2013-05-21 13:14:39
【问题描述】:

我想在这里看到将变量“vari”的前几个字符连接到 String abc=':

href="{concat('abc=', substring-before('vari', '='))}"

这是整个sn-p:

<xsl:template match="report:subelement">
    <tr>
        <td>
            <message>
              <xsl:variable name="vari"  select="."></xsl:variable>
                   <xsl:copy-of select="." />
            </message>
        </td>
        <td>
           <button type="button"  onclick="window.location.href=this.getAttribute('href')"  href="{concat('abc=', substring-before('vari', '='))}" >Kill thread</button>
        </td>
    </tr>
</xsl:template>

这可能是一个微不足道的问题,但我只是在学习 xslt。

【问题讨论】:

    标签: string variables xslt string-concatenation


    【解决方案1】:

    你很接近,但是: 要访问变量的值,您必须使用美元 ($) 作为前缀。不要将变量名放在撇号中。
    因此尝试:

    href="{concat('abc=', substring-before($vari, '='))}
    

    这会引发错误,因为您的变量声明与用法不在同一上下文中。 变量声明必须在同一元素中或在祖先中。将声明放在子元素模板的顶部或&lt;tr 元素中。

    更新的工作模板:

    <xsl:template match=""report:subelement">
        <xsl:variable name="vari"  select="."></xsl:variable>
        <tr>
            <td>
                <message>
                    <xsl:copy-of select="." />
                </message>
            </td>
            <td>
                <button type="button"  onclick="window.location.href=this.getAttribute('href')" 
                        href="{concat('abc=', substring-before($vari, '='))}" >Kill thread</button>
            </td>
        </tr>
    </xsl:template>
    

    【讨论】:

    • 谢谢,但是如果我声明了一个变量,以后怎么给它赋值呢?
    • 你不能。变量的值是不可改变的。也许你应该用一个更完整的例子来更新你的问题。
    • 那我不明白。如果我不能分配一个值,那么我以后可以从它那里读取什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多