I have an XSLT that I have created to group data for a catalog of shoes. The shoes need to be grouped by "line" and by "brand". The Line and Brand titles need to appear at the beginning of each section. Each line has more than one brand. Each brand usually has more than one shoe in it.
Here is some sample XML data:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<shoe>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brand>WALKING</brand>
<style-name>NEW BALANCE-NB 475V2 (WIDE)</style-name>
<color>Black</color>
<price>67.99</price>
</shoe>
<shoe>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brand>WALKING</brand>
<style-name>NEW BALANCE-496 (WIDE)</style-name>
<color>Grey/Pink</color>
<price>64.99</price>
</shoe>
<shoe>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brand>CROSS TRANING</brand>
<style-name>FILA-MEMORY PANACHE</style-name>
<color>Black/Pink</color>
<price>69.99</price>
</shoe>
<shoe>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brand>RUNNING</brand>
<style-name>FILA-VITALITY 2 TRAIL</style-name>
<color>Grey/Prpl/Turq</color>
<price>59.99</price>
</shoe>
<shoe>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brand>CASUAL</brand>
<style-name>LEVI'S-HAMILTON BUCK HI</style-name>
<color>Black/Black</color>
<price>34.99</price>
</shoe>
<shoe>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brand>CASUAL</brand>
<style-name>EVERLAST-EVAN SKATE</style-name>
<color>Black</color>
<price>29.99</price>
</shoe>
<shoe>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brand>RUNNING</brand>
<style-name>SKECHERS-POWER SWITCH (WIDE)</style-name>
<color>Black/White</color>
<price>69.99</price>
</shoe>
<shoe>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brand>RUNNING</brand>
<style-name>SKECHERS-EQUALIZER GEL TOP </style-name>
<color>Black</color>
<price>69.99</price>
</shoe>
</catalog>
Here is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:key name="shoes-by-line" match="shoe" use="line" />
<xsl:key name="shoes-by-brand" match="shoe" use="concat(line,'#',brand )" />
<xsl:template match="catalog">
<catalog>
<shoes>
<xsl:for-each select="shoe[count(. | key('shoes-by-line', line)[1]) = 1]">
<line>
<xsl:value-of select="line"/>
</line>
<brands>
<brand>
<xsl:value-of select="brand"/>
</brand>
<xsl:for-each select="key( 'shoes-by-brand', concat(line,'#',brand ))">
<shoe>
<style-name>
<xsl:value-of select="style-name"/>
</style-name>
<color>
<xsl:value-of select="color"/>
</color>
<price>
<xsl:value-of select="price"/>
</price>
</shoe>
</xsl:for-each>
</brands>
</xsl:for-each>
</shoes>
</catalog>
</xsl:template>
</xsl:stylesheet>
I am getting 1 "line" and 1 "brand" but I'm not getting the additional brands in each line. There should be at least two to three brands in each line. In this XML there are 2 lines.
Here is the XML structure that I want:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<lines>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brands>
<brand>WALKING</brand>
<shoes>
<shoe>
<style-name>NEW BALANCE-NB 475V2 (WIDE)</style-name>
<color>Black</color>
<price>67.99</price>
</shoe>
<shoe>
<style-name>NEW BALANCE-496 (WIDE)</style-name>
<color>Grey/Pink</color>
<price>64.99</price>
</shoe>
</shoes>
</brands>
</lines>
<lines>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brands>
<brand>CROSS TRANING</brand>
<shoes>
<shoe>
<style-name>FILA-MEMORY PANACHE</style-name>
<color>Black/Pink</color>
<price>69.99</price>
</shoe>
<shoe>
<line>LINE 43: WOMENS BRANDED ATHLETIC</line>
<brand>RUNNING</brand>
<style-name>FILA-VITALITY 2 TRAIL</style-name>
<color>Grey/Prpl/Turq</color>
<price>59.99</price>
</shoe>
</shoes>
</brands>
</lines>
<lines>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brands>
<brand>CASUAL</brand>
<shoes>
<shoe>
<style-name>LEVI'S-HAMILTON BUCK HI</style-name>
<color>Black/Black</color>
<price>34.99</price>
</shoe>
<shoe>
<style-name>EVERLAST-EVAN SKATE</style-name>
<color>Black</color>
<price>29.99</price>
</shoe>
</shoes>
</brands>
</lines>
<lines>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brands>
<brand>RUNNING</brand>
<shoes>
<shoe>
<line>LINE 87: MENS BRANDED ATHLETIC</line>
<brands>
<brand>RUNNING</brand>
<shoes>
<shoe>
<style-name>SKECHERS-POWER SWITCH (WIDE)</style-name>
<color>Black/White</color>
<price>69.99</price>
</shoe>
<shoe>
<style-name>SKECHERS-EQUALIZER GEL TOP </style-name>
<color>Black</color>
<price>69.99</price>
</shoe>
</shoes>
</brands>
</lines>
</catalog>