p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

xslt thread: Multiplie Grouping Problem


Message #1 by "karthikeyan" <karthikeyan@a...> on Fri, 6 Sep 2002 19:21:42 +0530
Dear Micheal,

  You Rock.  Thank you.  Wish you replied bit early cause that report has
been sent with this bug but anyway it will always be helpful for future.

  Heres the Code to do Multiple Grouping for Anyone who wants to do this in
future.

  Output will be Something like this :

  - ProductName(First Grouped by Product Name)
    +ProviderName (and then Grouped by Provider Name)
       + Names (Displaying Names coming under that Product - Provider)

Here's the XML File
~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Report_ExecutionOnly_Client.xsl"?>
<Recordset>
 <row>
  <InvestmentAdvisers>hai</InvestmentAdvisers>
  <ARFirmName>Coningsby Insurance</ARFirmName>
  <Network>Financial Options</Network>
  <Productname>Personal Pension</Productname>
  <ProviderName>Norwich Union</ProviderName>
  <firstlifeforenames>John</firstlifeforenames>
  <firstlifesurname>Thomas</firstlifesurname>
  <Secondlifeforenames></Secondlifeforenames>
  <Secondlifesurname></Secondlifesurname>
  <FromDate>1/1/2000</FromDate>
  <ToDate>1/1/2001</ToDate>
 </row>
 <row>
  <InvestmentAdvisers>hai</InvestmentAdvisers>
  <ARFirmName>Coningsby Insurance</ARFirmName>
  <Network>Financial Options</Network>
  <Productname>Personal Pension</Productname>
  <ProviderName>Scottish Widows</ProviderName>
  <firstlifeforenames>Shelly</firstlifeforenames>
  <firstlifesurname>Adams</firstlifesurname>
  <Secondlifeforenames></Secondlifeforenames>
  <Secondlifesurname></Secondlifesurname>
  <FromDate>1/1/2000</FromDate>
  <ToDate>1/1/2001</ToDate>
 </row>
 <row>
  <InvestmentAdvisers>hai</InvestmentAdvisers>
  <ARFirmName>Coningsby Insurance</ARFirmName>
  <Network>Financial Options</Network>
  <Productname>Personal Pension</Productname>
  <ProviderName>Norwich Union</ProviderName>
  <firstlifeforenames>Peter</firstlifeforenames>
  <firstlifesurname>Evans</firstlifesurname>
  <Secondlifeforenames></Secondlifeforenames>
  <Secondlifesurname></Secondlifesurname>
  <FromDate>1/1/2000</FromDate>
  <ToDate>1/1/2001</ToDate>
 </row>
 <row>
  <InvestmentAdvisers>hai</InvestmentAdvisers>
  <ARFirmName>Coningsby Insurance</ARFirmName>
  <Network>Financial Options</Network>
  <Productname>Whole of Life</Productname>
  <ProviderName>Norwich Union</ProviderName>
  <firstlifeforenames>David</firstlifeforenames>
  <firstlifesurname>Hill</firstlifesurname>
  <Secondlifeforenames>Clare</Secondlifeforenames>
  <Secondlifesurname>Hill</Secondlifesurname>
  <FromDate>1/1/2000</FromDate>
  <ToDate>1/1/2001</ToDate>
 </row>
</Recordset>
~~~~~~~~~~~~~~~~~

Here's the XSL File
~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
]>

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

  <xsl:key name="rows" match="row" use="Productname"/>
  <xsl:key name="pp" match="row" use="concat(Productname, '/',
                                             ProviderName)"/>


  <xsl:template match="/">

    <HTML>
      <HEAD>
        <META http-equiv="CONTENT-TYPE" content="TEXT/HTML;
charset=ISO-8859-1"/>
          <TITLE>
            Misys IFA Services plc :: <xsl:value-of
select="/Recordset/@ReportName"/>
          </TITLE>
      </HEAD>
      <BODY align="CENTER">

        <FONT face="ARIAL">

          <TABLE width="750" border="0" bgcolor="#FFFFFF" cellpadding="0"
                 cellspacing="0">
            <TR>
              <TD align="LEFT" bgcolor="#FFFFFF">
                <H2>Misys IFA Services plc</H2>
              </TD>
              <TD align="RIGHT"><IMG height="55" src="MISYS.GIF"></IMG></TD>
            </TR>
            <TR>
              <TD align="LEFT" bgcolor="#999999" colspan="2">
                <H4><xsl:value-of select="/Recordset/@ReportName"/></H4>
                <H5><i>For the Period:<xsl:value-of
select="/Recordset/row/FromDate"/>&nbsp;to&nbsp;<xsl:value-of
select="/Recordset/row/ToDate"/></i></H5>
              </TD>
            </TR>
          </TABLE>

          <TABLE width="750" border="1" bgcolor="#FFFFFF" cellpadding="2"
                 cellspacing="0">
            <xsl:apply-templates select="Recordset"/>

          </TABLE>

          <TABLE width="750" border="0" bgcolor="#FFFFFF" cellpadding="2"
                 cellspacing="0">
            <TR>
              <TD align="LEFT" bgcolor="#CCCCCC" width="470"><B>Total :
</B></TD>
             <TD align="RIGHT" bgcolor="#CCCCCC">
                <B><xsl:value-of
select="count(/Recordset/row/firstlifeforenames)"/></B> &nbsp;
              </TD>
            </TR>
          </TABLE>


        </FONT>

      </BODY>
    </HTML>

  </xsl:template>

  <xsl:template match="Recordset">
    <TR>
      <TD align="LEFT" valign="TOP" width="300">
        <H4>Network :</H4>
      </TD>
      <TD align="LEFT" valign="TOP" colspan="5">
        <xsl:value-of select="row/Network"/>
      </TD>
    </TR>
    <TR>
      <TD align="LEFT" valign="TOP">
        <H4>AR Firm Name : </H4>
      </TD>
      <TD align="LEFT" valign="TOP" colspan="5">
        <xsl:value-of select="row/ARFirmName"/>
      </TD>
    </TR>
    <TR>
      <TD align="LEFT" valign="TOP">
        <H4>Investment Adviser : </H4>
      </TD>
      <TD align="LEFT" valign="TOP" colspan="5">
        <xsl:value-of select="row/InvestmentAdvisers"/>
      </TD>
    </TR>
    <TR>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="150">Product
Name</TH>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="200">Provider
Name</TH>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="150">1st Life
Forename</TH>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="150">1st Life
Surname</TH>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="150">2nd Life
Forename</TH>
      <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC" width="150">2nd Life
Surname</TH>
    </TR>

   <xsl:apply-templates
         select="row[generate-id(.) = generate-id(key('rows',
Productname)[1])]" />

  </xsl:template>

  <xsl:template match="row">
    <TR>
        <TD><xsl:value-of select="Productname"/></TD>
        <TD>&nbsp;</TD>
        <TD>&nbsp;</TD>
        <TD>&nbsp;</TD>
        <TD>&nbsp;</TD>
        <TD>&nbsp;</TD>
    </TR>

    <xsl:variable name="product-group"
                  select="key('rows', Productname)"/>

    <xsl:for-each select="$product-group[
                          generate-id(.) = generate-id(key('pp',
concat(Productname, '/',
                          ProviderName))[1])]">

        <TR>
            <TD>&nbsp;</TD>
            <TD><xsl:value-of select="ProviderName"/></TD>
            <TD>&nbsp;</TD>
            <TD>&nbsp;</TD>
            <TD>&nbsp;</TD>
            <TD>&nbsp;</TD>
        </TR>

      <xsl:for-each select="key('pp', concat(Productname, '/',
                                             ProviderName))">


     <TR>
        <TD>&nbsp;</TD>
        <TD>&nbsp;</TD>
       <TD align="RIGHT" valign="TOP"><xsl:value-of
select="firstlifeforenames"/></TD>
     <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
select="firstlifesurname"/></TD>
     <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
select="Secondlifeforenames"/></TD>
     <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
select="Secondlifesurname"/></TD>
   </TR>

      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
~~~~~~~~~~~~~~~~~


----- Original Message -----
From: "Michael Kay" <michael.h.kay@n...>
To: "P2P_XSLT" <xslt@p...>
Sent: Monday, September 09, 2002 2:47 PM
Subject: [xslt] RE: Multiplie Grouping Problem


>
> Presumably you want your output structure to be:
>
> Product X
>   Provider A
>     details
>     details
>   Provider B
>     details
>     details
> Product Y
>  etc.
>
> The best way to do this is to define your second key in the form
>
> <xsl:key name="pp" match="row" select="concat(ProductName, '/',
> ProviderName)"/>
>
> Then, having found the first row for each product, instead of doing
>
> <xsl:for-each select="key('rows', ProductName)">
>
> do
>
> <xsl:variable name="product-group" select="key('rows', ProductName)"/>
> <xsl:for-each select="select="$product-group[
> generate-id(.) = generate-id(key('pp', concat(Productname, '/',
> ProviderName)[1])]"
>   <p>Provider <xsl:value-of select="ProviderName"/></p>
>   <xsl:for-each select="key('pp', concat(ProductName, '/',
> ProvierName)"/>
>     -- generate details --
>
> Michael Kay
> Software AG
> home: Michael.H.Kay@n...
> work: Michael.Kay@s...
>
> > -----Original Message-----
> > From: karthikeyan [mailto:karthikeyan@a...]
> > Sent: 06 September 2002 14:52
> > To: P2P_XSLT
> > Subject: [xslt] Multiplie Grouping Problem
> >
> >
> > Hi All,
> >
> >   I wanted to do multiple grouping.  I have done one level of
> > grouping but wanted to go another level.  Right now i m able
> > to do grouping based on Product Name but i also wanted to do
> > grouping on Provider Name.  I have seen Jeni Tennison  reply
> > for this question before but since i m new to XSLT, XPath
> > Stuff if somebody can point out with respect to my XML, XSL i
> > would be very grateful and i can understand better.
> >
> >   Here is my XML and XSL file (I have tried to make it
> > short).  Right now i have grouped successfully with
> > ProductName but i also want to group by ProviderName.  I have
> > also defined key for that but not sure how to use it.
> >
> >   Have a great day.  Hoping to get some response real soon. I
> > know it can be pain reading length mails but if somebody is
> > willing to help me out then then i can even send them xml and
> > xsl file as attachment personally.
> >
> >   Have a great day.
> >
> > karthikeyan.
> >
> > XML Source File
> > ~~~~~~~~~~~~~
> > <?xml version="1.0" encoding="ISO-8859-1"?>
> > <?xml-stylesheet type="text/xsl"
> > href="U:\karthikeyan\karthik\projects\Mifas\XSL\Job6-09-03-200
> > 2\Report_ExecutionOnly_Client.xsl"?>
> > <Recordset>
> >  <row>
> >   <InvestmentAdvisers>hai</InvestmentAdvisers>
> >   <ARFirmName>Coningsby Insurance</ARFirmName>
> >   <Network>Financial Options</Network>
> >   <Productname>Personal Pension</Productname>
> >   <ProviderName>Norwich Union</ProviderName>
> >   <firstlifeforenames>John</firstlifeforenames>
> >   <firstlifesurname>Thomas</firstlifesurname>
> >   <Secondlifeforenames>Peter</Secondlifeforenames>
> >   <Secondlifesurname>Evans</Secondlifesurname>
> >   <FromDate>1/1/2000</FromDate>
> >   <ToDate>1/1/2001</ToDate>
> >  </row>
> >  <row>
> >   <InvestmentAdvisers>hai</InvestmentAdvisers>
> >   <ARFirmName>Coningsby Insurance</ARFirmName>
> >   <Network>Financial Options</Network>
> >   <Productname>Personal Pension</Productname>
> >   <ProviderName>Scottish Widows</ProviderName>
> >   <firstlifeforenames>Shelly</firstlifeforenames>
> >   <firstlifesurname>Adams</firstlifesurname>
> >   <Secondlifeforenames>Kate</Secondlifeforenames>
> >   <Secondlifesurname>Shaw</Secondlifesurname>
> >   <FromDate>1/1/2000</FromDate>
> >   <ToDate>1/1/2001</ToDate>
> >  </row>
> >  <row>
> >   <InvestmentAdvisers>hai</InvestmentAdvisers>
> >   <ARFirmName>Coningsby Insurance</ARFirmName>
> >   <Network>Financial Options</Network>
> >   <Productname>Whole of Life</Productname>
> >   <ProviderName>Norwich Union</ProviderName>
> >   <firstlifeforenames>David</firstlifeforenames>
> >   <firstlifesurname>Hill</firstlifesurname>
> >   <Secondlifeforenames>Duncan</Secondlifeforenames>
> >   <Secondlifesurname>Franks</Secondlifesurname>
> >   <FromDate>1/1/2000</FromDate>
> >   <ToDate>1/1/2001</ToDate>
> >  </row>
> > </Recordset>
> > ~~~~~~~~~~~~
> >
> > XSL Source File
> > ~~~~~~~~~~~~~~~~~~
> > <?xml version="1.0" encoding="ISO-8859-1"?>
> >
> > <!DOCTYPE xsl:stylesheet [
> > <!ENTITY nbsp "&#160;">
> > ]>
> >
> > <xsl:stylesheet version="1.0"
> >     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> >
> >   <xsl:key name="rows" match="row" use="Productname"/>
> >   <xsl:key name="rows2" match="row" use="ProviderName"/>
> >
> >   <xsl:template match="/">
> >
> >     <HTML>
> >       <HEAD>
> >         <META http-equiv="CONTENT-TYPE" content="TEXT/HTML;
> > charset=ISO-8859-1"/>
> >           <TITLE>
> >             Misys IFA Services plc :: <xsl:value-of
> > select="/Recordset/@ReportName"/>
> >           </TITLE>
> >       </HEAD>
> >       <BODY align="CENTER">
> >
> >         <FONT face="ARIAL">
> >
> >           <TABLE width="750" border="0" bgcolor="#FFFFFF"
> > cellpadding="0"
> >                  cellspacing="0">
> >             <TR>
> >               <TD align="LEFT" bgcolor="#FFFFFF">
> >                 <H2>Misys IFA Services plc</H2>
> >               </TD>
> >               <TD align="RIGHT"><IMG height="55"
> > src="MISYS.GIF"></IMG></TD>
> >             </TR>
> >             <TR>
> >               <TD align="LEFT" bgcolor="#999999" colspan="2">
> >                 <H4><xsl:value-of
> > select="/Recordset/@ReportName"/></H4>
> >                 <H5><i>For the Period:<xsl:value-of
> > select="/Recordset/row/FromDate"/>&nbsp;to&nbsp;<xsl:value-of
> > select="/Recordset/row/ToDate"/></i></H5>
> >               </TD>
> >             </TR>
> >           </TABLE>
> >
> >           <TABLE width="750" border="1" bgcolor="#FFFFFF"
> > cellpadding="2"
> >                  cellspacing="0">
> >             <xsl:apply-templates select="Recordset"/>
> >
> >           </TABLE>
> >
> >           <TABLE width="750" border="0" bgcolor="#FFFFFF"
> > cellpadding="2"
> >                  cellspacing="0">
> >             <TR>
> >               <TD align="LEFT" bgcolor="#CCCCCC"
> > width="470"><B>Total : </B></TD>
> >              <TD align="RIGHT" bgcolor="#CCCCCC">
> >                 <B><xsl:value-of
> > select="count(/Recordset/row/firstlifeforenames)"/></B> &nbsp;
> >               </TD>
> >             </TR>
> >           </TABLE>
> >
> >
> >         </FONT>
> >
> >       </BODY>
> >     </HTML>
> >
> >   </xsl:template>
> >
> >   <xsl:template match="Recordset">
> >     <TR>
> >       <TD align="LEFT" valign="TOP" width="300">
> >         <H4>Network :</H4>
> >       </TD>
> >       <TD align="LEFT" valign="TOP" colspan="5">
> >         <xsl:value-of select="row/Network"/>
> >       </TD>
> >     </TR>
> >     <TR>
> >       <TD align="LEFT" valign="TOP">
> >         <H4>AR Firm Name : </H4>
> >       </TD>
> >       <TD align="LEFT" valign="TOP" colspan="5">
> >         <xsl:value-of select="row/ARFirmName"/>
> >       </TD>
> >     </TR>
> >     <TR>
> >       <TD align="LEFT" valign="TOP">
> >         <H4>Investment Adviser : </H4>
> >       </TD>
> >       <TD align="LEFT" valign="TOP" colspan="5">
> >         <xsl:value-of select="row/InvestmentAdvisers"/>
> >       </TD>
> >     </TR>
> >     <TR>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="150">Product Name</TH>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="200">Provider Name</TH>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="150">1st Life Forename</TH>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="150">1st Life Surname</TH>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="150">2nd Life Forename</TH>
> >       <TH align="LEFT" valign="TOP" bgcolor="#CCCCCC"
> > width="150">2nd Life Surname</TH>
> >     </TR>
> >
> >     <xsl:apply-templates
> >          select="row[generate-id(.) = generate-id(key('rows',
> > Productname)[1])]" />
> >
> >   </xsl:template>
> >
> >   <xsl:template match="row" priority="1">
> >
> >     <TR>
> >         <TD><xsl:value-of select="Productname"/></TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >     </TR>
> >     <TR>
> >         <TD>&nbsp;</TD>
> >         <TD><xsl:value-of select="ProviderName"/></TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >     </TR>
> >
> >     <xsl:for-each select="key('rows', Productname)">
> >    <TR>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >      <TD align="RIGHT" valign="TOP"><xsl:value-of
> > select="firstlifeforenames"/></TD>
> >      <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
> > select="firstlifesurname"/></TD>
> >      <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
> > select="Secondlifeforenames"/></TD>
> >      <TD align="RIGHT" valign="TOP" width="225"><xsl:value-of
> > select="Secondlifesurname"/></TD>
> >    </TR>
> >     </xsl:for-each>
> >
> >     <TR>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >         <TD>&nbsp;</TD>
> >     </TR>
> >   </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > ~~~~~~~~~~~~~~~~~~
> >
> >
> >
> > ---
> > In developing the .NET Framework Microsoft has
> > taken XML to heart, and with ASP.NET representing
> > the cutting edge of Windows based web development,
> > Professional ASP.NET XML with C# has been published
> > to help web developers to appreciate how these
> > technologies can work together to provide
> > functional, powerful applications.
> > http://www.wrox.com/acon11.asp?ISBN=1861007345
> >
>
>
> ---
> In developing the .NET Framework Microsoft has
> taken XML to heart, and with ASP.NET representing
> the cutting edge of Windows based web development,
> Professional ASP.NET XML with C# has been published
> to help web developers to appreciate how these
> technologies can work together to provide
> functional, powerful applications.
> http://www.wrox.com/acon11.asp?ISBN=1861007345
>



  Return to Index