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 :
...
                <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
Par Elnourso - Publié dans : XSM-XSLT
Ecrire un commentaire - Voir les 0 commentaires

Présentation

Archives

Articles récents

Recherche

Calendar

Janvier 2012
L M M J V S D
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
<< < > >>
Créer un blog gratuit sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus