问题
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?
回答1:
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 whole key(...)
node set by their position:
<xsl:for-each select="key('items-by-product', $productid)
[position() mod $num_per_div = 1]">
<xsl:variable name="iter" select="position()" />
<xsl:variable name="first" select="($iter - 1) * $num_per_div + 1" />
<xsl:variable name="last" select="$iter * $num_per_div" />
<outer pos="{$first}">
<xsl:for-each select="key('items-by-product', $productid)
[position() >= $first and position() <= $last]">
<inner><xsl:value-of select="itemid"/></inner>
</xsl:for-each>
</outer>
</xsl:for-each>
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 (so position()
means the chunk number). But in the predicate on the select
of the inner for-each the current node list is all the nodes returned from the key
function call (so position()
is the position of the node-under-test within the list of all nodes with the given productid
).
回答2:
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="items-by-product" match="item" use="productid"/>
<xsl:variable name="groupSize" select="2" />
<xsl:variable name="productid" select="1" />
<xsl:variable name="my-items">
<xsl:copy-of select="key('items-by-product', $productid)"/>
</xsl:variable>
<xsl:template match="/">
<output>
<xsl:for-each select="exsl:node-set($my-items)/item[position() mod $groupSize = 1]">
<outer pos="{position()}">
<xsl:for-each select=". | following-sibling::item[position() < $groupSize]" >
<inner>
<xsl:value-of select="itemid"/>
</inner>
</xsl:for-each>
</outer>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/28415867/xslt-every-nth-node-with-a-filter