xsl-list
[Top] [All Lists]

Re: [xsl] Multi level Grouping using xslt1.0

2009-02-23 13:33:48
Bafna, Kamlesh wrote:

The requirement is to group by Company, then by Business, then by
Department & finally the Account.

The output expected is -

<Message>
  <Company>
    <Code>491</Code>
    <Business>
      <Code>0000</Code>
      <Department>
        <Code>0000</Code>
<Account>30010</Account> </Department>
      <Department>
        <Code>0001</Code>
<Account>30010</Account> </Department>
    </Business>
  </Company>
  <Company>
    <Code>498</Code>
    <Business>
      <Code>0000</Code>
      <Department>
        <Code>0000</Code>
        <Account>30010</Account>
      </Department>
    </Business>
    <Business>
      <Code>0001</Code>
      <Department>
        <Code>0000</Code>
        <Account>30011</Account>
      </Department>
    </Business>
  </Company>
</Message>

This should do:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="by-company"
           match="Row"
           use="Company"/>

  <xsl:key name="by-business"
           match="Row"
           use="concat(Company, '|', Business)"/>

  <xsl:key name="by-department"
           match="Row"
           use="concat(Company, '|', Business, '|', Department)"/>

  <xsl:key name="by-account"
           match="Row"
use="concat(Company, '|', Business, '|', Department, '|', Account)"/>

  <xsl:template match="Data">
    <Message>
<xsl:apply-templates select="Rows/Row[generate-id() = generate-id(key('by-company', Company)[1])]" mode="company"/>
    </Message>
  </xsl:template>

  <xsl:template match="Row" mode="company">
    <Company>
      <Code>
        <xsl:value-of select="Company"/>
      </Code>
<xsl:apply-templates select="key('by-company', Company)[generate-id() = generate-id(key('by-business', concat(Company, '|', Business))[1])]" mode="business"/>
    </Company>
  </xsl:template>

  <xsl:template match="Row" mode="business">
    <Business>
      <Code>
        <xsl:value-of select="Business"/>
      </Code>
<xsl:apply-templates select="key('by-business', concat(Company, '|', Business))[generate-id() = generate-id(key('by-department', concat(Company, '|', Business, '|', Department))[1])]" mode="department"/>
    </Business>
  </xsl:template>

  <xsl:template match="Row" mode="department">
    <Department>
      <Code>
        <xsl:value-of select="Department"/>
      </Code>
<xsl:apply-templates select="key('by-department', concat(Company, '|', Business, '|', Department))[generate-id() = generate-id(key('by-account', concat(Company, '|', Business, '|', Department, '|', Account))[1])]" mode="account"/>
    </Department>
  </xsl:template>

  <xsl:template match="Row" mode="account">
    <Account>
      <xsl:value-of select="Account"/>
    </Account>
  </xsl:template>

</xsl:stylesheet>


--

        Martin Honnen
        http://JavaScript.FAQTs.com/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>