代码之家  ›  专栏  ›  技术社区  ›  toomanyairmiles

XSL中的条件语句

  •  19
  • toomanyairmiles  · 技术社区  · 17 年前

    我在一个站点上工作,在XSL中有一些if/or语句,并且有点不熟悉我不确定如何完成的语言:

    如果满足[条件一]或[条件二]则执行[操作]否则执行[替代操作]

    有人能举些例子吗?

    事先谢谢!

    4 回复  |  直到 12 年前
        1
  •  40
  •   Community Mohan Dere    9 年前

    <xsl:if test="some Boolean condition">
      <!-- "if" stuff (there is no "else" here) -->
    </xsl:if>
    

    <xsl:choose>
      <xsl:when test="some Boolean condition">
        <!-- "if" stuff -->
      </xsl:when>
      <xsl:otherwise>
        <!-- "else" stuff -->
      </xsl:otherwise>
    </xsl:choose>
    

    <xsl:when> 你喜欢什么。

    according to a set of rules . 这些(在大多数情况下)归结为“如果有什么东西的话”-> true false

    • (所以) NaN
    • 空节点集为
    • false()
    • 'false' '0'

    编辑:当然有一种更高级(更惯用)的方法来控制程序流,这就是模板匹配:

    <xsl:template match="node[contains(., 'some text')]">
      <!-- output X -->
    </xsl:template>
    
    <xsl:template match="node[not(contains(., 'some text'))]">
      <!-- output Y -->
    </xsl:template>
    
    <xsl:template match="/">
      <xsl:apply-templates select=".//node" />
    </xsl:template>
    

    <xsl:apply-templates> <xsl:if> <xsl:choose>

    <xsl:template match="/">
      <xsl:for-each select=".//node">
        <xsl:choose>
          <xsl:when test="contains(., 'some text')">
            <!-- output X -->
          </xsl:when>
          <xsl:when test="not(contains(., 'some text'))">
            <!-- output Y -->
          </xsl:when>
        <xsl:choose>
      <xsl:for-each>
    </xsl:template>
    

    XSLT初学者倾向于选择后一种形式来熟悉它,但是检查模板匹配而不是使用条件是值得的。(也) see

        2
  •  3
  •   Daniel F. Thornton    17 年前

    <xsl:if> <xsl:choose> <xsl:when> / <xsl:otherwise> here

    <xsl:choose>
        <xsl:when test="[conditionOne] or [conditionTwo]">
            <!-- do [action] -->
        </xsl:when>
        <xsl:otherwise>
            <!-- do [alternative action] -->
        </xsl:otherwise>
    </xsl:choose>
    
        3
  •  1
  •   Flyer1    17 年前

    <xsl:if test="expression">
      ...some output if the expression is true...
    </xsl:if>
    

    不确定XSL是否具有else条件,但您应该能够测试if true,然后测试if false或其他方法。

        4
  •  0
  •   Zack The Human Kunal    17 年前

    xsl:choose . 就像用if/else和final else。

    <xsl:choose>
      <xsl:when test="condition one or condition two">
        <!-- action -->
      </xsl:when>
      <xsl:otherwise>
        <!-- alternative action -->
      </xsl:otherwise>
    </xsl:choose>