tags:

views:

25

answers:

1

Hi, The following is an excerpt from an XML data set:

<instance>
    <ID>1</ID>
    <start>5.8633333333</start>
    <end>29.8216666667</end>
    <code>Player 1</code>
</instance>
<instance>
    <ID>2</ID>
    <start>28.4566666667</start>
    <end>51.1450000000</end>
    <code>Player 2</code>
</instance>
<instance>
    <ID>3</ID>
    <start>49.8383333333</start>
    <end>71.1150000000</end>
    <code>Player 3</code>
</instance>
<instance>
    <ID>4</ID>
    <start>72.9850000000</start>
    <end>95.3766666667</end>
    <code>Player 1</code>
</instance>
<instance>
    <ID>5</ID>
    <start>93.9250000000</start>
    <end>116.6883333333</end>
    <code>Player 2</code>
</instance>

I am looking to display this data in a table, where each row holds a unique code (as in Player 1, Player 2 etc), and also all the with that specific code. Something like this:

    <tr><td>Player 1</td><td>ID 1; ID 4</td></tr>
    <tr><td>Player 2</td><td>ID 2; ID 5</td></tr>

I've been trying to get this to work for a while now but failing. Any help would be much appreciated!

Thanks! Frank

A: 

I would use something like this:

<xsl:for-each-group select="instance" group-by="code/text()">
  <tr>
  <td><xsl:value-of select="current-grouping-key()" /></td>
    <td><xsl:apply-templates select="current-group()" /></td>
  </tr>
</xsl:for-each-group>

It groups the instance elements together based on the text in the code element. The current-grouping-key() function returns the string that is grouping things together, and current-group() returns the sequence of nodes for that key. You'll still have to fill in the bit to put in the semicolons, but that shouldn't be too difficult.

for-each-group is an XSLT 2.0 construct, so make sure that you use an XSLT processor that understands it. Saxon is a great example of one.

Michael