grouping by attribute

Subject: grouping by attribute
From: "Sargeant, Richard (GEIS)" <Richard.Sargeant@xxxxxxxxxxx>
Date: Mon, 23 Aug 1999 13:50:33 +0200
Can anyone suggest a better way to achieve the following... I need to group
the xml elements category and place each category in a separate table. For
example: my xml file is...

<test>
  <item category="aaa">1xxxxxxxaaa</item>
  <item category="bbb">2xxxxxxxbbb</item>
  <item category="zzz">3xxxxxxxzzz</item>
  <item category="bbb">4xxxxxxxbbb</item>
  <item category="yyy">5xxxxxxxyyy</item>
</test>

   Currently I use the following XSL which works but it requires I hardcode
each attribute. Is this where I should use a xsl:variable?

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/TR/WD-xsl";
  xmlns="http://www.w3.org/TR/REC-html40"; 
  result-ns="">
  <xsl:template match="*|/"><xsl:apply-templates/></xsl:template>

  <xsl:template match="test">
  <xsl:sort select="item@category"/>
    <p/><table border="1">
    <xsl:apply-templates select="item[@category='aaa']"/>
    </table>
    <p/><table border="1">
    <xsl:apply-templates select="item[@category='bbb']"/>
    </table>
    <p/><table border="1">
    <xsl:apply-templates select="item[@category='yyy']"/>
    </table>
    <p/><table border="1">
    <xsl:apply-templates select="item[@category='zzz']"/>
    </table>
  </xsl:template>

  <xsl:template match="item[@category='aaa']">
    <tr><td><xsl:value-of select="."/></td></tr>
  </xsl:template>
  <xsl:template match="item[@category='bbb']">
    <tr><td><xsl:value-of select="."/></td></tr>
  </xsl:template>
  <xsl:template match="item[@category='yyy']">
    <tr><td><xsl:value-of select="."/></td></tr>
  </xsl:template>
  <xsl:template match="item[@category='zzz']">
    <tr><td><xsl:value-of select="."/></td></tr>
  </xsl:template>

  <xsl:template match="text()"><xsl:value-of select="."/></xsl:template>
</xsl:stylesheet>


regards   Richard


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread