使用XSL键很容易。
<!-- 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>