如何在XSLT中实现if-else语句?

214

我正在尝试在XSLT中实现一个if-else语句,但我的代码无法解析。有人有什么想法吗?

  <xsl:variable name="CreatedDate" select="@createDate"/>
  <xsl:variable name="IDAppendedDate" select="2012-01-01" />
  <b>date: <xsl:value-of select="$CreatedDate"/></b> 

  <xsl:if test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:if>
  <xsl:else>
    <h2> dooooooooooooo </h2>
  </xsl:else>

可能是XSL if else条件的重复问题。 - user801116
5个回答

396
你必须使用 <xsl:choose> 标签重新实现它:
<xsl:choose>
  <xsl:when test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:when>
  <xsl:otherwise>
    <h2> dooooooooooooo </h2>
  </xsl:otherwise>
</xsl:choose>

83

如果只需要快速检查一个条件,可以使用 If 语句。 当你有多个选项时,请使用以下所示的 <xsl:choose>

   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

此外,您可以使用多个<xsl:when>标签来表达If .. Else IfSwitch模式,如下所示:
   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:when test="$CreatedDate = $IDAppendedDate">
       <h2>booooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

前面的例子相当于以下伪代码:

   if ($CreatedDate > $IDAppendedDate)
   {
       output: <h2>mooooooooooooo</h2>
   }
   else if ($CreatedDate = $IDAppendedDate)
   {
       output: <h2>booooooooooooo</h2>
   }
   else
   {
       output: <h2>dooooooooooooo</h2>
   }

1
你能否请修正下面的语句?我们都知道如果没有跟随 {} 的 if(case > x) 只会执行一行代码。我看到很多初学者都会写出你发布的这个内容,可能他们中的许多人是直接复制的。 - Oliver
1
顺便说一下,if else条件只是一个例子或者说是伪代码。好的,我考虑了你的建议并进行了编辑。 - Rookie Programmer Aravind

42

如果我可以提出一些建议(虽然是两年后,但希望对未来的读者有所帮助):

  • 将常见的h2元素因素化。
  • 将常见的ooooooooooooo文本因素化。
  • 如果使用XSLT 2.0,请注意新的XPath 2.0 if/then/else结构。

XSLT 1.0解决方案(也适用于XSLT 2.0)

<h2>
  <xsl:choose>
    <xsl:when test="$CreatedDate > $IDAppendedDate">m</xsl:when>
    <xsl:otherwise>d</xsl:otherwise>
  </xsl:choose>
  ooooooooooooo
</h2>

XSLT 2.0的解决方案

<h2>
   <xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
   ooooooooooooo
</h2>

4

这篇博客文章的原始内容。我们可以通过以下代码实现if else。

<xsl:choose>
    <xsl:when test="something to test">

    </xsl:when>
    <xsl:otherwise>

    </xsl:otherwise>
</xsl:choose>

这是我所做的:

<h3>System</h3>
    <xsl:choose>
        <xsl:when test="autoIncludeSystem/autoincludesystem_info/@mdate"> <!-- if attribute exists-->
            <p>
                <dd><table border="1">
                    <tbody>
                        <tr>
                            <th>File Name</th>
                            <th>File Size</th>
                            <th>Date</th>
                            <th>Time</th>
                            <th>AM/PM</th>
                        </tr>
                        <xsl:for-each select="autoIncludeSystem/autoincludesystem_info">
                            <tr>
                                <td valign="top" ><xsl:value-of select="@filename"/></td>
                                <td valign="top" ><xsl:value-of select="@filesize"/></td>
                                <td valign="top" ><xsl:value-of select="@mdate"/></td>
                                <td valign="top" ><xsl:value-of select="@mtime"/></td>
                                <td valign="top" ><xsl:value-of select="@ampm"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
                </dd>
            </p>
        </xsl:when>
        <xsl:otherwise> <!-- if attribute does not exists -->
            <dd><pre>
                <xsl:value-of select="autoIncludeSystem"/><br/>
            </pre></dd> <br/>
        </xsl:otherwise>
    </xsl:choose>

我的输出

在此输入图片描述


2
最简单直接的方法是进行第二个if测试,但条件要求取反。这种技术比choose-when-otherwise嵌套块更短、更易于阅读和更容易正确实现。
<xsl:variable name="CreatedDate" select="@createDate"/>
     <xsl:variable name="IDAppendedDate" select="2012-01-01" />
     <b>date: <xsl:value-of select="$CreatedDate"/></b> 
     <xsl:if test="$CreatedDate &gt; $IDAppendedDate">
        <h2> mooooooooooooo </h2>
     </xsl:if>
     <xsl:if test="$CreatedDate &lt;= $IDAppendedDate">
        <h2> dooooooooooooo </h2>
     </xsl:if>

下面是一份政府网站样式表中使用此技术的真实例子:http://w1.weather.gov/xml/current_obs/latest_ob.xsl

6
需要记住并确保第二个if测试匹配第一个的补集,这会使任何后续的修改更容易出错。 - Philippe-André Lorin
2
我同意,Pal。此外,我认为上面的例子很难读懂,而使用xsl:choose会更加简单明了,其意义也更加清晰。 - Doug Barbieri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接