2013-08-20 47 views
1

我想用嵌套的if语句来测试一些值,但它似乎不起作用。第一次当块工作正常,但当它碰到否则进入时,测试= $国家,它打破。文字“外部时间和国家是...”正常工作。问题是最后两段没有打印出来。我不确定这是否与嵌套when语句的方式有关,因为我是XSLT新手。如何嵌套when/otherwise语句?

国家是当我更改网站的区域设置为美国或GB。

<xsl:choose> 
<xsl:when test="target = 'FX'"> <!-- Normal page --> 
    <xsl:choose> 
     <xsl:when test="$country = 'US'"> 
      <p>Inside FX US. Country is <xsl:value-of select="$country" /></p> 
     </xsl:when> 
     <xsl:otherwise> 
      <p>Inside FX GB. Country is <xsl:value-of select="$country" /></p> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:when> 
<xsl:otherwise> <!-- Graph page --> 

<p>Outside when and country is <xsl:value-of select="$country" /></p> 
    <xsl:when test="$country = 'US'"> 
     <p>Inside LPBM US. Country is <xsl:value-of select="$country" /></p> 
    </xsl:when> 
    <xsl:otherwise> 
     <p>Inside LPBM GB. Country is <xsl:value-of select="$country" /></p> 
    </xsl:otherwise> 
    </xsl:otherwise> 
</xsl:choose> 
+0

请添加XML输入和期望的输出HTML。嵌套''很可能不是你最好的选择。 – Tomalak

回答

1

您的最后一个块没有打开和关闭<xsl:choose>块。尝试添加这些,看看你是否得到了预期的结果。

你的XSLT看起来应该像下面这样:

<xsl:choose> 
<xsl:when test="target = 'FX'"> <!-- Normal page --> 
    <xsl:choose> 
     <xsl:when test="$country = 'US'"> 
      <p>Inside FX US. Country is <xsl:value-of select="$country" /></p> 
     </xsl:when> 
     <xsl:otherwise> 
      <p>Inside FX GB. Country is <xsl:value-of select="$country" /></p> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:when> 
<xsl:otherwise> <!-- Graph page --> 

<p>Outside when and country is <xsl:value-of select="$country" /></p> 
    <xsl:choose> <!-- Added this line --> 
     <xsl:when test="$country = 'US'"> 
      <p>Inside LPBM US. Country is <xsl:value-of select="$country" /></p> 
     </xsl:when> 
     <xsl:otherwise> 
      <p>Inside LPBM GB. Country is <xsl:value-of select="$country" /></p> 
     </xsl:otherwise> 
    </xsl:choose> <!-- Added this line --> 
    </xsl:otherwise> 
</xsl:choose> 
+0

另外,我确信有更好的方法来做到这一点,但我现在没有时间写这个。我可能会回过头来推荐一个更好的方法。 –