XSLT:根据条件设置多个变量

Jos*_*ost 10 xslt exslt xslt-1.0

我想根据一个条件环境分配多个变量.我知道如何只为一个变量做到这一点:

<xsl:variable name="foo">
    <xsl:choose>
        <xsl:when test="$someCondition">
            <xsl:value-of select="3"/>
        <xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="4711"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)

但是,如果我想根据相同的条件$ someCondition分配两个变量呢?

我不想再次编写相同的xsl:choose语句,因为它在实际示例中有点冗长且计算密集.

有问题的环境是带有exslt扩展的libxslt(xslt 1.0).

编辑:我想要的是一种类似的行为

if (condition) {
    foo = 1;
    bar = "Fred";
}
else if (...)  {
    foo = 12;
    bar = "ASDD";
}
(... more else ifs...)
else {
    foo = ...;
    bar = "...";
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*m C 11

你可以让你的主变量返回一个元素列表; 每个要设置的变量一个

  <xsl:variable name="all">
     <xsl:choose>
        <xsl:when test="a = 1">
           <a>
              <xsl:value-of select="1"/>
           </a>
           <b>
              <xsl:value-of select="2"/>
           </b>
        </xsl:when>
        <xsl:otherwise>
           <a>
              <xsl:value-of select="3"/>
           </a>
           <b>
              <xsl:value-of select="4"/>
           </b>
        </xsl:otherwise>
     </xsl:choose>
  </xsl:variable>
Run Code Online (Sandbox Code Playgroud)

然后,使用exslt函数,您可以将其转换为"节点集",然后可以使用该节点集来设置各个变量

  <xsl:variable name="a" select="exsl:node-set($all)/a"/>
  <xsl:variable name="b" select="exsl:node-set($all)/b"/>
Run Code Online (Sandbox Code Playgroud)

不要忘记你需要为XSLT中的exslt函数声明namepsace以使其工作.