【问题标题】:XSLT nested conditionals - how to do thisXSLT 嵌套条件 - 如何执行此操作
【发布时间】:2012-08-06 09:21:19
【问题描述】:

我对 xslt 很陌生

我有以下 xslt sn-p (为了清楚起见是伪的):

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

真的有两个问题:

为什么它不能按原样工作?

我怎样才能实现我的目标?

【问题讨论】:

    标签: xslt nested conditional


    【解决方案1】:

    因为您的 XSLT 格式不正确,所以它会爆炸。当您在xsl:choose 之外打开第一个div 时,您必须在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>
    

    【讨论】:

    • 是的,我刚醒来时有一个“durrhhh时刻”。感谢澄清
    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多