views:

59

answers:

1

I have a variable containing:

<col p1="Newman" p2="Paul"/>
...
<col p1="Newman" p2="Paolo"/>
<col p1="Newman" p2="Paul"/>

i wold in output a table with in the first column the value of p2 and in the second the number of time it appear. For each value of p2 should i have only a row.

<table>
<tr><td>p2</td><td>num</td></tr>
<tr><td>Pault</td><td>2</td>
...
<tr><td>Paolo</td><td>1</td>
</table>
A: 

Easy with an XSL key.

<!-- index all <col> elements by their @p2 attribute -->
<xsl:key name="kColByFirstname" match="col" use="@p2" />

<xsl:template match="/RootElement">
  <table>
    <tr>
      <td>p2</td><td>num</td>
    </tr>
    <!-- select the respective first <col> of each group -->
    <xsl:apply-templates select="col[
      generate-id() = generate-id(key('kColByFirstname', @p2)[1])
    ]" />
  </table>
</xsl:template>

<xsl:template match="col">
  <tr>
    <!-- output the current value of @p2 and its group count -->
    <td><xsl:value-of select="@p2"/></td>
    <td><xsl:value-of select="count(key('kColByFirstname', @p2))" /></td>
  </tr>
</xsl:template>
Tomalak
i try it but for each value of p2 there ara more then one rowexample<tr><td>Paul</td><td>2</td></tr><tr><td>Paul</td><td>2</td></tr>but i need it only one
Erick
@Erick: The code works for me.
Tomalak