2012-06-27 45 views
0

我在解决如何在div中包含for-each的内容时遇到问题,其中每个div都包含4个元素。包装一个for-each在div中的每个第n个元素

下面,你会发现我的XSLT得多的简化版本:

<xsl:template match="/"> 
     <div class="container"><!-- have to be repeated for every 4 elements from the for-each --> 
      <xsl:for-each select="$currentPage/GalleriListe/descendant::* [@isDoc] [not(self::GalleriListe)]"> 
       <div>...</div> 
      </xsl:for-each> 
     </div> 
    </xsl:template> 

任何想法? 谢谢!

+0

测试这个问题是不是没有源XML文档的完整(但尽可能小)例如太有意义和准确的希望的结果。请编辑问题并添加这些必要的信息 - 不要强迫有意向的人猜测实际给出的内容以及实际需要的内容。 –

回答

1

你没有发布你的XML,所以我用这个答案简化了一个。这有点古怪,可能会有更优雅的方式。您可以在this XMLPlayground session

<!-- declare how many items per container --> 
<xsl:variable name='num_per_div' select='4' /> 

<!-- root - kick things off --> 
<xsl:template match="/"> 
    <xsl:apply-templates select='root/node' mode='container' /> 
</xsl:template> 

<!-- iteration content - containers --> 
<xsl:template match='node' mode='container'> 
    <xsl:if test='position() = 1 or not((position()-1) mod $num_per_div)'> 
     <div> 
      <xsl:variable name='pos' select='position()' /> 
      <xsl:apply-templates select='. | following-sibling::*[count(preceding-sibling::node) &lt; $pos+(number($num_per_div)-1)]' /> 
     </div> 
    </xsl:if> 
</xsl:template> 

<!-- iteration content - individual items --> 
<xsl:template match='node'> 
    <p><xsl:value-of select='.' /></p> 
</xsl:template> 
+0

非常感谢您的回答。它帮助我在正确的方向:) – thilemann

相关问题