I.このXSLT1.0変換:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="foo[position() mod 5 = 1]">
<branch>
<xsl:copy-of select=
". | following-sibling::*[not(position() > 4)]"/>
</branch>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
提供されたXMLドキュメントに適用された場合(整形式に修正):
<root>
<branch>
<foo>bar</foo>
<foo>bar2</foo>
<foo>bar3</foo>
<foo>bar4</foo>
<foo>bar5</foo>
<foo>bar6</foo>
<foo>bar7</foo>
</branch>
</root>
必要な正しい結果を生成します。
<root>
<branch>
<foo>bar</foo>
<foo>bar2</foo>
<foo>bar3</foo>
<foo>bar4</foo>
<foo>bar5</foo>
</branch>
<branch>
<foo>bar6</foo>
<foo>bar7</foo>
</branch>
</root>
説明:
これは「位置グループ化」の場合であり、グループのすべての開始要素が5タプルの最初の要素です(したがって、その位置は次の条件を満たすposition() mod 5 = 1
。
II。XSLT 2.0ソリューション:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<root>
<xsl:for-each-group select="*/*"
group-adjacent="(position() -1) idiv 5">
<branch>
<xsl:sequence select="current-group()"/>
</branch>
</xsl:for-each-group>
</root>
</xsl:template>
</xsl:stylesheet>
このXSLT2.0変換を同じXMLドキュメント(上記)に適用すると、同じ必要な正しい結果が生成されます。
説明:
属性と関数を使用した<xsl:for-each-group>
XSLT2.0命令の適切な使用。group-adjacent
current-group()