2009-12-11 127 views
0

我正在嘗試使用xslt在Sitecore網站中創建一個簡單的項目清單。問題是,我無法找到一種方法來測試某件物品是否具有其下的後代物品。測試子項目

可以很容易地到達頂級這樣設置:

<xsl:template match="*" mode="main"> 
<table width="100%" class="alternating"> 
    <xsl:for-each select="./item"> 
     <tr> 
<td width="100px"><sc:image field="Image" mh="100" mw="100" /></td> 
<td style="vertical-align:top"><h2><sc:text field="Title"/></h2></td> 
<td style="vertical-align:top"><xsl:value-of select="sc:path(.)" /></td> 
     </tr> 
    </xsl:for-each> 
</table> 
</xsl:template> 

這在主圖像,標題和每個項目的僅低於你的地方開始路徑的一個不錯的,平坦的桌面。問題是我找不到一種方法來輕鬆測試這些物品中是否有後代。這將使代碼看起來像這樣:

<xsl:template match="*" mode="main"> 
<table width="100%" class="alternating"> 
    <xsl:for-each select="./item"> 
     <tr> 
<td width="100px"><sc:image field="Image" mh="100" mw="100" /></td> 
<td style="vertical-align:top"><h2><sc:text field="Title"/></h2></td> 
<td style="vertical-align:top"><xsl:value-of select="sc:path(.)" /></td> 
<td> 
    <!—test whether the item has descendants --> 
    <xsl:if test=?????> 
     <!—loop through descendant items --> 
     <xsl:for-each select="./item"> 
     Render information about each descendant item 
     </xsl:for-each> 
    </xsl:if> 
</td> 
     </tr> 
    </xsl:for-each> 
</table> 
</xsl:template> 

回答

1

有沒有必要測試後代。只需使用:

... 
<td>   
<!-- loop through descendant items if any -->   
<xsl:for-each select="./item">   
<!-- Render information about each descendant item -->  
</xsl:for-each>  
</xsl:if> 
</td> 
... 

如果沒有後代,則不會爲該節點輸出任何內容。

0

Rashmi是正確的,for-each不應該執行,如果沒有孩子。

不過順便說一句,只是爲了回答這個問題,你可以執行測試

<xsl:if test="count(item) != 0"> 
0

您可能要測試的後代,如果你正在渲染列表,不想包裝標籤時AREN」 t任何後代,那麼你可能想要如果:

<xsl:if test="count(./item) &gt; 0"> 
    <ul> 
    <xsl:for-each select="./item"> 
     <li> 
     <!-- content here --> 
     </li> 
    </xsl:for-each> 
    </ul> 
</xsl:if>