2013-04-25 19 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中缺少的元素。請先糾正它,讓我們知道你的結果。

相關問題