如何实现在XSLT if-else语句?(How to implement if-else stat

2019-06-21 08:16发布

我想实现在XSLT的,如果-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>

Answer 1:

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

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


Answer 2:

如果语句用于快速检查只是一个条件。 当你有多个选项,使用<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>
   }


Answer 3:

如果我可以提供一些建议(两年后,但希望有助于未来的读者):

  • 分解出常见的h2元素。
  • 分解出共同ooooooooooooo文本。
  • 要知道新的XPath 2.0 if/then/else ,如果使用XSLT 2.0构造。

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>


Answer 4:

最直接的方法是做了第二次,如果测试但反转的条件。 这种技术是更短,更容易对眼睛,也更容易得到比一个选择 - 当 - 否则嵌套块权:

<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



文章来源: How to implement if-else statement in XSLT?