XSLT嵌套条件 - 如何执行此操作

时间:2012-08-06 00:47:42

标签: xslt nested conditional

我对xslt

很新

我有以下xslt片段(为了清晰起见,伪'):

<xsl:for-each select="fields/field">
  <xsl:if test="field=someThing">
    <div>
      <!--some content output-->
      <!--here by doing-->
      <!--some complex stuff-->
    </div>
  </xsl:if>
</xsl:for-each>

但是基于循环中的字段值,我可以选择在结束div之前做一些其他的事情。

所以(手持闪亮的新xsl书)我想我可以这样做:

<xsl:for-each select="fields/field">
  <xsl:if test="field=someThing">
    <div>
      <!--some content output-->
      <!--here by doing-->
      <!--some complex stuff-->
    <xsl:choose>
      <xsl:when test="field=someThingSpecific">
        <div>
        <!--process some additional-->
        <!--stuff here-->
        </div>
        <!--then close div-->
        </div>
      </xsl:when>
      <xsl:otherwise>
        <!--nothing to do here-->
        <!--just close the div-->
        </div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:for-each>

但它爆炸了。

显然因为结束div已进入条件xsl:choose block

真的有两个问题:

为什么它不能正常工作?

我如何实现目标?

1 个答案:

答案 0 :(得分:0)

它正在爆炸,因为你的XSLT格式不正确。当您在div之外打开第一个xsl:choose时,您必须在xsl:choose之外关闭它。

另外,如果您不需要在xsl:otherwise中执行任何操作,只需执行另一个xsl:if

<xsl:for-each select="fields/field">
    <xsl:if test="field=someThing">
        <div>
            <!--some content output-->
            <!--here by doing-->
            <!--some complex stuff-->
            <xsl:if test="field=someThingSpecific">
                <div>
                    <!--process some additional-->
                    <!--stuff here-->
                </div>
            </xsl:if>
            <!--then close div-->
        </div>
    </xsl:if>
</xsl:for-each>