首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在XSLT中实现if-else语句?

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

Stack Overflow用户
提问于 2012-11-29 17:14:46
回答 3查看 503.2K关注 0票数 189

我试图在XSLT中实现一个if -else语句,但是我的代码就是不能解析。有谁有什么想法吗?

代码语言:javascript
复制
  <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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-29 17:17:26

您必须使用<xsl:choose>标签重新实现它:

代码语言:javascript
复制
<xsl:choose>
  <xsl:when test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:when>
  <xsl:otherwise>
    <h2> dooooooooooooo </h2>
  </xsl:otherwise>
</xsl:choose>
票数 358
EN

Stack Overflow用户

发布于 2012-11-29 17:23:49

If语句用于快速检查一个条件。当您有多个选项时,请使用<xsl:choose>,如下所示:

代码语言:javascript
复制
   <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模式,如下所示:

代码语言:javascript
复制
   <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>

前面的示例等同于下面的伪代码:

代码语言:javascript
复制
   if ($CreatedDate > $IDAppendedDate)
   {
       output: <h2>mooooooooooooo</h2>
   }
   else if ($CreatedDate = $IDAppendedDate)
   {
       output: <h2>booooooooooooo</h2>
   }
   else
   {
       output: <h2>dooooooooooooo</h2>
   }
票数 75
EN

Stack Overflow用户

发布于 2015-03-14 11:16:50

最直接的方法是进行第二次if-test,但条件颠倒了。这种技术比选择时嵌套块更短、更美观、更容易获得正确的效果:

代码语言:javascript
复制
<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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13622338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档