Jeudi 8 octobre 2009
4
08
/10
/Oct
/2009
18:39
Today I had to do something that sounded trivial... and in fact it is !
What if you have some XML data :
...and we nee to group on the Brands/Brand[@Type] (unsorted) to show a grid per type.
"How To...?"
XSLT has a magic function for it : xsl:for-each-group
Let's make a template to rock'n "rule" this :
..and it should do it pretty much.
To resume :
- we loop on each 'Brand' and group by 'Type' attribute (of the Brand).
- in the first cell we put the grouping key ('Type' attribute of the current Brand)
- in the second cell, we loop the Brand of the current group and display their name
A simple example and you're already gone!
Go for it and have some XSL-ent fun!
Cheers
What if you have some XML data :
|
... <Brands> <Brand Id="24416" Type="shirts"> <Name>SHirtie</Name> </Brand> <Brand Id="25061" Type="shorts"> <Name>Shortie</Name> </Brand> <Brand Id="24375"Type ="hats"> <Name>Hattie</Name> </Brand> <Brand Id="27129"Type ="shirts"> <Name>Shirtie 2</Name> </Brand> </Brands> ... |
...and we nee to group on the Brands/Brand[@Type] (unsorted) to show a grid per type.
| shirts |
SHirtie Shirtie 2 |
| shorts | Shortie |
| hats | Hattie |
"How To...?"
XSLT has a magic function for it : xsl:for-each-group
Let's make a template to rock'n "rule" this :
|
<xsl:template name="GroupByBrand"> <xsl:param name="brands"/> <xsl:for-each-group select="$brands/Brand" group-by="@Type"> <TABLE width="100%" BORDER="1"> <TR> <TD><xsl:value-of select="current-grouping-key()" /></TD> <TD> <xsl:for-each select="current-group()"> <xsl:value-of select="Name"/> <BR /> </xsl:for-each> </TD> <TR> </TABLE> </xsl:for-each-group> </xsl:template> |
..and it should do it pretty much.
To resume :
- we loop on each 'Brand' and group by 'Type' attribute (of the Brand).
- in the first cell we put the grouping key ('Type' attribute of the current Brand)
- in the second cell, we loop the Brand of the current group and display their name
A simple example and you're already gone!
Go for it and have some XSL-ent fun!
Cheers