0

I want to use the function "group by" in a xsl file but I am just getting errors.

This is my code:

<table xsl:version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<tr>
<th>A</th>
<th>B</th>
</tr>
<xsl:for-each-group select="Result/Record" group-by="AC_CPR">
<tr>
  <td><xsl:value-of select="AC_CPR"/></td>
  <td><xsl:value-of select='sum(//AC_LNA/Row/Column[2])'/></td>
</tr>

The error is this one:

msxml3.dll error '80004005'

"Keyword table may not contain xsl:for-each-group".

The xml is this:

 <Result>
<Record code="033007">
<Name>demo</Name>
 <AC_CPR>02080</AC_CPR>
<date>10/06/2009</date>
<AC_LNA ncols="2">
<Row>
<Column>000115</Column>
<Column>9</Column>
</Row>
</AC_LNA>
<AC_FSE>10/06/2009</AC_FSE>
<AC_AV/>
</Record>
</Result>
Fran Rod
  • 586
  • 4
  • 14
  • 26

1 Answers1

0

Write a complete stylesheet with e.g.

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:key name="group" match="Result/Record" use="AC_CPR"/>

<xsl:template match="Result">
<table>
<tr>
<th>A</th>
<th>B</th>
</tr>
<xsl:for-each select="Record[generate-id() = generate-id(key('group', AC_CPR)[1])]">
<tr>
  <td><xsl:value-of select="AC_CPR"/></td>
  <td><xsl:value-of select="sum(key('group', AC_CPR)/AC_LNA/Row/Column[2])"/></td>
</tr>
</xsl:for-each>
</table>

</xsl:template>

</xsl:stylesheet>

For the sum I have guessed that you want to sum for each group and not for the complete set of all nodes.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • hello Martin, I get an error saying that: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML documen... – Fran Rod Mar 11 '13 at 13:21
  • I typed the code in the SO editor and forgot to close a tag, I will edit and correct that. – Martin Honnen Mar 11 '13 at 13:25