Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何在xsl中对同质数据进行分组?_Xslt - Fatal编程技术网

Xslt 如何在xsl中对同质数据进行分组?

Xslt 如何在xsl中对同质数据进行分组?,xslt,Xslt,假设我们有以下数据: <all> <item id="1"/> <item id="2"/> ... <item id="N"/> </all> ... 对这些项进行分组的最优雅的xslt方式是什么? 例如,假设我们想要一个每行有两个单元格的表。 在我的脑海里,我可以想象(虽然没有经过测试) 在模板中,匹配项,我可以调用这个项,选择下面的同级项。 但即使在这种情况下,我也应该传递额外的参数,以使递归

假设我们有以下数据:

<all>
    <item id="1"/>
    <item id="2"/>
    ...
    <item id="N"/>
</all>

...
对这些项进行分组的最优雅的xslt方式是什么? 例如,假设我们想要一个每行有两个单元格的表。 在我的脑海里,我可以想象(虽然没有经过测试) 在模板中,匹配项,我可以调用这个项,选择下面的同级项。
但即使在这种情况下,我也应该传递额外的参数,以使递归有限。

因为行计数可以是可变的。。我正在将其作为参数传递给模板:)


使用位置和模式,例如

<xsl:template match="/all">
    <table>
    <xsl:apply-templates name="item" mode="group"/>
    </table>
</xsl:template>

<xsl:template match="item[position() mod 2=1]" mode="group">
<tr>
<td><xsl:apply-templates select="." mode="render"/></td>
<td><xsl:apply-templates select="following-sibling::item[1]" mode="render"/></td>
</tr>
</xsl:template>

<xsl:template match="item[position() mod 2=0]"></xsl:template>

<xsl:template match="item" mode="render">item: <xsl:value-of select="@id"/></xsl:template>

项目:

假设您的示例XML可能存在重复。如果输入XML的结构发生变化,则其
也会发生变化。
<xsl:template match="/all">
    <table>
    <xsl:apply-templates name="item" mode="group"/>
    </table>
</xsl:template>

<xsl:template match="item[position() mod 2=1]" mode="group">
<tr>
<td><xsl:apply-templates select="." mode="render"/></td>
<td><xsl:apply-templates select="following-sibling::item[1]" mode="render"/></td>
</tr>
</xsl:template>

<xsl:template match="item[position() mod 2=0]"></xsl:template>

<xsl:template match="item" mode="render">item: <xsl:value-of select="@id"/></xsl:template>