2009-12-11 93 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>