<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!-- Declare a key to identify each group of 3 elements -->
<xsl:key name="positionKey" match="/TestDocument/Element" use="floor((position() - 2) div 3)"/>
<xsl:template match="/TestDocument">
<html>
<!-- Iterate over the first element in each group -->
<xsl:for-each select="Element[(position() - 1) mod 3 = 0]">
<ul>
<!-- Grab all elements from this group -->
<xsl:apply-templates select="key('positionKey', position()-1)"/>
</ul>
</xsl:for-each>
</html>
</xsl:template>
<xsl:template match="Element">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:stylesheet>
2 回答
好问题,1 .
This transformation :
when applied on the following XML document :
produces the wanted, correct result (连续三组中的事物):
Explanation :
Template pattern matching 第一组
thing
中的一组三个thing
元素 .Using modes 处理一组
thing
元素(以不同于处理初始启动thing
的方式),一旦确定 .一个有趣的问题 . 我在下面概述了一个解决方案,它使用
<xsl:key>
来识别每组三个,使用一个小的模运算 .输入文件:
样式表:
结果: