【问题标题】:Store output of a XSLT node into XSLT variable将 XSLT 节点的输出存储到 XSLT 变量中
【发布时间】:2013-07-23 19:43:42
【问题描述】:

我的 XSLT v1.0 代码 -

<Test1>
    <xsl:text>"</xsl:text>
    <xsl:choose>
        <xsl:when test="/root/node1">B</xsl:when>
        <xsl:when test="/root/node2">S</xsl:when>
        <xsl:otherwise>NA</xsl:otherwise>
    </xsl:choose>
    <xsl:text>"</xsl:text>     
</Test1>

我想将上述节点&lt;Test1&gt; 的输出存储到一个变量中。像这样的,

<xsl:variable name="test">
    <xsl:value-of select="??"/>
</xsl:variable>

使用此变量的值来计算其他值或显示该值,

<Test2>
    <xsl:text>"</xsl:text>
    <xsl:value-of select="$test"/>
    <xsl:text>"</xsl:text>
</Test2>

我应该写什么而不是??来获取节点&lt;Test1&gt;的值?或者有没有其他方法可以将节点的输出值读入 XSLT 中的变量?

【问题讨论】:

    标签: xslt


    【解决方案1】:

    我想你只是想要

    <xsl:variable name="test">
        <xsl:text>"</xsl:text>
        <xsl:choose>
            <xsl:when test="/root/node1">B</xsl:when>
            <xsl:when test="/root/node2">S</xsl:when>
            <xsl:otherwise>NA</xsl:otherwise>
        </xsl:choose>
        <xsl:text>"</xsl:text>
    </xsl:variable>
    

    然后

    <Test2>
        <xsl:text>"</xsl:text>
        <xsl:value-of select="$test"/>
        <xsl:text>"</xsl:text>
    </Test2>
    

    您可以使用元素节点作为包装器,但如果您只是想处理一些字符串,例如BNA,它并不能改善事情。

    【讨论】:

    • 我有多个节点,例如Test1,并且不想复制代码以将值读入变量。你能建议点别的吗?提前致谢。
    • 您可以将值存储在变量中,甚至是节点中。但至少对于 XSLT 1.0 处理器,您最终会得到结果树片段,您所能做的就是在它们上使用copy-ofvalue-of。我不确定你想如何使用你的变量,对于你所展示的情况,我认为我的建议就足够了,请编辑你的问题并提供更多细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多