192

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>
4

5 に答える 5

365

<xsl:choose>タグを使用して再実装する必要があります。

<xsl:choose>
  <xsl:when test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:when>
  <xsl:otherwise>
    <h2> dooooooooooooo </h2>
  </xsl:otherwise>
</xsl:choose>
于 2012-11-29T09:17:25.917 に答える
77

If ステートメントは、1 つの条件のみをすばやく確認するために使用されます。複数のオプションがある場合は<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 Ifまたはパターンを作成することもできます。Switch

   <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>
   }
于 2012-11-29T09:23:49.410 に答える
42

私がいくつかの提案を提供できる場合(2年後ですが、将来の読者に役立つことを願っています)

  • 共通h2要素を抽出します。
  • 共通oooooooooooooテキストを因数分解します。
  • if/then/elseXSLT 2.0 を使用する場合は、新しい XPath 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>
于 2014-10-21T16:19:12.050 に答える