Reputation: 1
I am somewhat new to using groups and want to be able to select a subset of teh group by using position().
I already have this code working as I want (creating the groups as needed and the correct output.)
<xsl:template match="Locations">
<table>
<tbody>
<xsl:for-each-group select="Location" group-by="Country">
<xsl:sort data-type="text" order="ascending" select="."/>
<xsl:for-each-group select="current-group()" group-by="current-grouping-key()">
</xsl:for-each-group>
<tr>
<td class="country"><xsl:value-of select="current-grouping-key()"/></td>
</tr>
<xsl:for-each-group select="current-group()" group-by="City">
<xsl:sort data-type="text" order="ascending" select="current-grouping-key()"/>
<tr>
<td class="city"><xsl:value-of select="fn:current-grouping-key()"/></td>
</tr>
</xsl:for-each-group>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
If I wanted to just get the 2nd through 4th Country tags, how would I go about doing that? I tried position(), but instead of getting a number, I am getting the grouping-key value, e.g., CA.
This is the source XML:
<Locations>
<Location>
<Country>US</Country>
<City>New York</City>
</Location>
<Location>
<Country>US</Country>
<City>Houston</City>
</Location>
<Location>
<Country>MX</Country>
<City>Cancun</City>
</Location>
<Location>
<Country>CH</Country>
<City>Zurich</City>
</Location>
<Location>
<Country>CA</Country>
<City>Toronto</City>
</Location>
<Location>
<Country>DE</Country>
<City>Berlin</City>
</Location>
<Location>
<Country>JP</Country>
<City>Tokyo</City>
</Location>
<Location>
<Country>GB</Country>
<City>London</City>
</Location>
<Location>
<Country>CA</Country>
<City>Edmonton</City>
</Location>
</Locations>
Any help would be greatly appreciated. Thanks in advance.
Upvotes: 0
Views: 87
Reputation: 117102
If I wanted to just get the 2nd through 4th Country tags, how would I go about doing that?
I guess you would do:
<xsl:template match="Locations">
<table border="1">
<tbody>
<xsl:for-each-group select="Location" group-by="Country">
<xsl:sort select="." data-type="text" order="ascending"/>
<xsl:if test="position() ge 2 and position() le 4">
<tr>
<td class="country">
<xsl:value-of select="current-grouping-key()"/>
</td>
</tr>
<xsl:for-each-group select="current-group()" group-by="City">
<xsl:sort select="." data-type="text" order="ascending"/>
<tr>
<td class="city">
<xsl:value-of select="current-grouping-key()"/>
</td>
</tr>
</xsl:for-each-group>
</xsl:if>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
Upvotes: 1