p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

xslt thread: Counting duplicate nodes only once


Message #1 by "BrianL" <BrianLeary@h...> on Thu, 19 Jul 2001 04:10:36
You've worked out the difficult bit, which is how to get the first element
of each group. You just need to add the easy bit, another loop around all
hte members of the group:

<xsl:template match="group">
   groupOption [<xsl:value-of select="position()"/>] = "<xsl:value-of
   select="name" />";<br/>
     planOption [<xsl:value-of select="position()"/>] 
   <xsl:for-each select="key('groupkey', name)">
       "<xsl:value-of select="planname"/>",
   </xsl:for-each>
</xsl:template>

I'll leave you to sort out the formatting!

Mike Kay



> -----Original Message-----
> From: BrianL [mailto:brianleary@h...]
> Sent: 19 July 2001 22:42
> To: P2P_XSLT
> Subject: [xslt] Re: Counting duplicate nodes only once
>
>
> Thanks Guys,
> Actually It does work with the microsoft parser but I am still having
> problems.
>
> Here is my sample xml;
>
> <grouplist>
> <group><name>Hourly</name><planname>Dental</planname><date>071
> 301</date><fi
> lename>group1.pdf</filename></group>
> <group><name>Salaried</name><planname>medical</planname><date>
> 071201</date>
> <filename>group1.pdf</filename></group>
> <group><name>Hourly</name><planname>life</planname><date>07150
> 1</date><file
> name>group1.pdf</filename></group>
> <group><name>Hourly</name><planname>medical</planname><date>07
> 1201</date><f
> ilename>group1.pdf</filename></group>
> <group><name>Hourly</name><planname>401k</planname><date>07120
> 1</date><file
> name>group1.pdf</filename></group>
> <group><name>Hourly</name><planname>IRA</planname><date>071201
> </date><filen
> ame>group1.pdf</filename></group>
> <group><name>Salaried</name><planname>Dental</planname><date>0
> 71201</date><
> filename>group1.pdf</filename></group>
> </grouplist>
>
> and here is my sample XSLT;
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format">
> 	<xsl:output method="xml" version="1.0" encoding="UTF-8"
> indent="yes"/>
> 	<xsl:key name="groupkey" match="group" use="name" />
> <xsl:template match="grouplist ">
> <xsl:apply-templates select="group[generate-id(.) = generate-id(key
> ('groupkey', name)[1])]" /><br/>
> </xsl:template>
> <xsl:template match="group">
>   groupOption [<xsl:value-of select="position()"/>] = "<xsl:value-of
> select="name" />";<br/>
>
>   </xsl:template>
> </xsl:stylesheet>
>
> My output is
> groupOption[1]="Hourly";
> groupOption[2]="Salaried";
>
> Where I am stuck is when I want to create my next set of options
> (planOptions) based on the value of the groupOptions. So what
> I want to
> get is;
> planOption[1]="Dental";"Life";"Medical";"401k";"IRA" <!--
> Hourly data-->
> planOption[2]="Medical";"Dental" <!-- salaried data -->
>
> Then I want to do the same for yearOption and fileOption.
> It seems that I can only get the planOption value of the
> first groupOption.
> I know this is confusing so I hope I am explaining this clearly.
> Any help would be greatly apreciated !
> Thanks again
> Brian
>
>
>
>
>
> > See Kipp's reply.  I don't know all that much about
> > XSLT and I know nothing about WD-XSL.  :)
> >
> > You can download the 3.0 release from MS:
> >
> http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?
> url=/MSDN-FILES/027/001/662/msdncompositedoc.xml
> >
> > -ben
> >
> > --- BrianL <brianleary@h...> wrote:
> > > Thanks for the reply. Unfortunately this doesn't
> > > work with the microsoft
> > > parser.
> > > Any other ideas ?
> > >
> > >  Brian,
> > > >
> > > > I think you're talking about grouping here... try
> > > > playing with this code - it might help:
> > > >
> > > > XSLT:
> > > > <?xml version="1.0" encoding="UTF-8"?>
> > > > <xsl:stylesheet version="1.0"
> > > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > > > xmlns:fo="http://www.w3.org/1999/XSL/Format">
> > > > 	<xsl:output method="xml" version="1.0"
> > > > encoding="UTF-8" indent="yes"/>
> > > >
> > > > 	<xsl:key name="groupkey" match="type" use="name"
> > > />
> > > >
> > > > <xsl:template match="list">
> > > >   <xsl:apply-templates
> > > >     select="type[generate-id(.) 
> > > > generate-id(key('groupkey', name)[1])]" />
> > > > </xsl:template>
> > > >
> > > > <xsl:template match="type">
> > > >   <br/><b><xsl:value-of select="name" /></b>
> > > > </xsl:template>
> > > >
> > > > </xsl:stylesheet>
> > > >
> > > > XML:
> > > > <?xml version="1.0" encoding="UTF-8"?>
> > > > <list>
> > > > 	<type>
> > > > 		<name>hourly</name>
> > > > 		<plan>dental</plan>
> > > > 		<year>01012000</year>
> > > > 	</type>
> > > > 	<type>
> > > > 		<name>hourly</name>
> > > > 		<plan>Life</plan>
> > > > 		<year>01012001</year>
> > > > 	</type>
> > > > 	<type>
> > > > 		<name>salaried</name>
> > > > 		<plan>dental</plan>
> > > > 		<year>01012000</year>
> > > > 	</type>
> > > > </list>
> > > >
> > > > This type of thing is explained on page 505 of
> > > Kay's
> > > > XSLT book (2nd edition).
> > > >
> > > > -ben
> > > >
> > > >
> > > > --- BrianL <BrianLeary@h...> wrote:
> > > > > Hi,
> > > > > I am having difficulty trying to figure out how
> > > to
> > > > > skip over duplicate
> > > > > nodes and only count them once
> > > > > for example
> > > > >
> > > > > I have employee groups with different plans and
> > > plan
> > > > > years
> > > > > &
> > > > > I am creating arrays out of this data.
> > > > > <list>
> > > > > <type>
> > > > > <name>hourly</name>
> > > > > <plan>dental</plan>
> > > > > <year>01012000</year>
> > > > > </type>
> > > > > <type>
> > > > > <name>hourly</name>
> > > > > <plan>Life</plan>
> > > > > <year>01012001</year>
> > > > > </type>
> > > > > <type>
> > > > > <name>salaried</name>
> > > > > <plan>dental</plan>
> > > > > <year>01012000</year>
> > > > > </type>
> > > > > </list>
> > > > >
> > > > > I want to create an array that gives me the plan
> > > > > detail per name
> > > > > so hourly would count all the hourly name
> > > elements,
> > > > > only display once but
> > > > > show all the plans for all of hourly
> > > > > the same for each other name (ie -salaried,etc)
> > > > > I do not know the names ahead of time so I need
> > > to
> > > > > make this dynamic.
> > > > > Any advice would be greatly apreciated !
> > > > > Thanks
> > > > > Brian
> ---
> When you work at Microsoft, you don't have to wait for the
> latest technologies to be released. You'll create them ? with
> XML, SOAP, and C# -- and help decide what happens next.
> Find out where your career can take you: take a look at our
> software design engineer opportunities at
> http://www.microsoft.com/jobs.
>


  Return to Index