2013-04-25 48 views
0

我很少对xslt感兴趣,并尝试过使用各种方法检查节点是否有孩子。我有以下几点:XSL - 如果节点有孩子,如何做一件事,否则做另一件事

<xsl:if test="child::list"> 

以上的部分工作,但问题是我已经在这个方法中使用whenotherwise试过,但它不工作。它看起来像这样:

<xsl:when test="child::list"> 

,我猜是错误的,因为它不工作。

的代码如下:

<xsl:for-each select="td"> 
<td> 
    <xsl:when test="child::list"> 
     <table cellpadding='0' cellspacing='0'> 
      <thead> 
       <tr> 
        <xsl:for-each select="list/item/table/thead/tr/th"> 
         <th><xsl:value-of select="self::node()[text()]"/></th> 
        </xsl:for-each> 
       </tr> 
       <xsl:for-each select="list/item/table/tbody/tr"> 
        <tr> 
         <xsl:for-each select="td"> 
          <td><xsl:value-of select="self::node()[text()]"/></td> 
         </xsl:for-each> 
        </tr> 
       </xsl:for-each> 
      </thead> 
     </table> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="self::node()[text()]"/> 
    </xsl:otherwise> 
</td> 
</xsl:for-each> 

任何帮助将不胜感激...

回答

3

xsl:whenxsl:otherwise必须是一个xsl:choose内:

<xsl:choose> 
    <xsl:when test="..."> 
    <!-- Do one thing --> 
    </xsl:when> 
    <xsl:otherwise> 
    <!-- Do something else --> 
    </xsl:otherwise> 
</xsl:choose> 

但是,你应该做的是在这里正确使用模板:

<xsl:template match="something"> 
    .... 
    <xsl:apply-templates select="td" mode="list" /> 
    .... 
    </xsl:template> 

    <xsl:template match="td" mode="list"> 
    <xsl:value-of select="."/> 
    </xsl:template> 

    <xsl:template match="td[list]" mode="list"> 
    <table cellpadding='0' cellspacing='0'> 
     <thead> 
     <xsl:apply-templates select='list/item/table/thead/tr' /> 
     <xsl:apply-templates select="list/item/table/tbody/tr" /> 
     </thead> 
    </table> 
    </xsl:template> 

    <xsl:template match="th | td"> 
    <xsl:copy> 
     <xsl:value-of select="." /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="tr"> 
    <xsl:copy> 
     <xsl:apply-templates select="th | td" /> 
    </xsl:copy> 
    </xsl:template> 
+0

哦没关系。感谢您的回应。现在一切正常。 :) – 2013-04-25 11:04:20

0

你创建XSLT是不好的。 xsl:when是xsl的子元素:选择XSLT中缺少的元素。请先纠正它,让我们知道你的结果。

相关问题