I'm aware of the Muenchian grouping method for XSLT, but all implementations I've seen rely on a single node as the value to group on. In this case I'd like to group on a node-set. In the input below I'd like to group on outputs/output part ref.
I've tried to construct keys such as
<xsl:key name="refsKey" match="/processes/process" use="outputs/output_part_ref"/>
Of course outputs/output_part_ref matches the first node and doesn't match the node-set.
Input
<?xml version="1.0" encoding="UTF-8"?>
<processes>
<process>
<name>ProcessA</name>
<input_qty>1200</input_qty>
<outputs>
<output_part_ref>1</output_part_ref>
<output_part_ref>2</output_part_ref>
<output_part_ref>3</output_part_ref>
</outputs>
</process>
<process>
<name>ProcessB</name>
<input_qty>1300</input_qty>
<outputs>
<output_part_ref>1</output_part_ref>
<output_part_ref>2</output_part_ref>
<output_part_ref>3</output_part_ref>
</outputs>
</process>
<process>
<name>ProcessC</name>
<input_qty>770</input_qty>
<outputs>
<output_part_ref>1</output_part_ref>
<output_part_ref>2</output_part_ref>
</outputs>
</process>
</processes>
Sample Output
<html>
...
<table>
<tr>
<td>2500</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>770</td>
<td>1</td>
<td>2</td>
</tr>
</table>
...
</html>
If the number of elements forming the key is not fixed then I agree with Michael, we need to compute the key first and use exsl:node-set
or similar in XSLT 1.0:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">
<xsl:output method="html" indent="yes"/>
<xsl:key name="refsKey" match="process" use="key"/>
<xsl:variable name="rtf1">
<xsl:apply-templates select="processes/process" mode="key"/>
</xsl:variable>
<xsl:template match="process" mode="key">
<xsl:copy>
<key>
<xsl:for-each select="outputs/output_part_ref">
<xsl:sort select="." data-type="number"/>
<xsl:if test="position() > 1">|</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</key>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="ns1" select="exsl:node-set($rtf1)/process"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="processes">
<table>
<xsl:apply-templates select="$ns1[generate-id() = generate-id(key('refsKey', key)[1])]"/>
</table>
</xsl:template>
<xsl:template match="process">
<tr>
<td><xsl:value-of select="sum(key('refsKey', key)/input_qty)"/></td>
<xsl:for-each select="outputs/output_part_ref">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.