I'm using XSLT for some output formatting, and I want a wrapper element around every N nodes of the output. I've read xslt - adding </tr><tr> every n node?, but my problem is that the source nodes have to come from a lookup:
<xsl:for-each select="key('items-by-product', $productid)">
rather than just a template match. All the examples I've found assume that the nodes you want are all next to each other, and they're just counting siblings.
I have a solution that works for me:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:variable name='num_per_div' select='2' />
<xsl:variable name='productid' select='1' />
<xsl:output method="xml" indent="yes"/>
<xsl:key name="items-by-product" match="item" use="productid"/>
<xsl:template match="data">
<output>
<xsl:for-each select="key('items-by-product', $productid)">
<xsl:variable name='pos' select='position()' />
<xsl:if test="position() = 1 or not((position()-1) mod $num_per_div)">
<outer pos="{$pos}">
<xsl:for-each select="key('items-by-product', $productid)">
<xsl:variable name='ipos' select='position()' />
<xsl:if test="$ipos >= $pos and $ipos < $pos + $num_per_div">
<inner>
<xsl:value-of select="itemid"/>
</inner>
</xsl:if>
</xsl:for-each>
</outer>
</xsl:if>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
with data
<data>
<item>
<productid>1</productid>
<itemid>1</itemid>
</item>
<item>
<productid>1</productid>
<itemid>2</itemid>
</item>
<item>
<productid>2</productid>
<itemid>A</itemid>
</item>
<item>
<productid>1</productid>
<itemid>3</itemid>
</item>
<item>
<productid>2</productid>
<itemid>B</itemid>
</item>
<item>
<productid>1</productid>
<itemid>4</itemid>
</item>
</data>
which produces
<?xml version="1.0" encoding="utf-8"?>
<output>
<outer pos="1">
<inner>1</inner>
<inner>2</inner>
</outer>
<outer pos="3">
<inner>3</inner>
<inner>4</inner>
</outer>
</output>
But this is looping through all the nodes for each node, which strikes me as inefficient.
Is there a better approach that will produce the same output more efficiently? Can the following-sibling techniques work with a filter?
You could start by copying the nodes of interest into a variable; that would make them - and only them - siblings. However, in XSLT 1.0 such variable would contain a result-tree-fragment, which needs to be converted to a node-set before it can be processed further:
XSLT 1.0
You can use an outer loop with
position() mod $num_per_div
to get one "iteration" per chunk, then within that select out the members of that chunk out of the wholekey(...)
node set by their position:The key thing here is to remember that the
position()
function is context-sensitive and means different things at different times. In the definition of the$iter
variable, the current node list is the nodes selected by the outer for-each, i.e. the list with the first, third, fifth, etc. items returned by the key (soposition()
means the chunk number). But in the predicate on theselect
of the inner for-each the current node list is all the nodes returned from thekey
function call (soposition()
is the position of the node-under-test within the list of all nodes with the givenproductid
).