This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sort, count, number, group



try the xsl:number element ...

>From: Michel CASABIANCA <casa@sdv.fr>
>Reply-To: xsl-list@mulberrytech.com
>To: xsl-list@mulberrytech.com
>CC: XSL-List@mulberrytech.com
>Subject: Re: sort, count, number, group
>Date: 12 Mar 2000 14:39:30 +0100
>
>Hello
>
>quagly <quagly@home.com> writes:
> >     After perusing the FAQ I can sort, count, number, and group.
> >     But I cannot do them all at once.  Please help.
> >
> >     Example:
> >
> > xml:
> >
> > <root>
> >     <foo>
> >           <bar>bard</bar>
> >           <bar>bark</bar>
> >     </foo>
> >     <foo>
> >           <bar>bark</bar>
> >           <bar>barb</bar>
> >      </foo>
> > </root>
> >
> > Sample xsl that selects distinct <bar>
> >
> > <xsl:template match="//bar[not(. = following::bar)]">
> >      <xsl:value-of select="."/>
> > </xsl:template>
> >
> > produces:
> >
> > bard bark barb
> >
> > what I want is to number these, sort them, and count the number of times
> > they appear in the xml source
> >
> > Desired output:
> >
> > 1.  barb  -1
> > 2.  bard  -1
> > 3.  bark  -2
> >
> > I can't seem to get there from here.  Do I need to use for-each?
>
>I did it with this stylesheet :
>
>---------------------------
><?xml version="1.0" encoding="iso-8859-1"?>
><xsl:stylesheet version="1.0" 
>xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>   <xsl:output method="text"/>
>
>   <xsl:template match="/">
>     <xsl:for-each select="//bar">
>       <xsl:variable name="value" select="."/>
>       <xsl:if test="count(preceding::bar[.=$value]) = 0">
>	<xsl:value-of select="."/><xsl:text> : </xsl:text>
>	<xsl:value-of select="count(//bar[.=$value])"/>
>	<xsl:text>
</xsl:text>
>       </xsl:if>
>     </xsl:for-each>
>   </xsl:template>
>
></xsl:stylesheet>
>----------------------------
>
>producing the following output :
>
>bard : 1
>bark : 2
>barb : 1
>
>--
>+---------------------------+--------------------------------+
>| Michel CASABIANCA         | http://www.sdv.fr/pages/casa   |
>| mailto:casa@sdv.fr        | Articles sur Java et XML       |
>| Développement Java et XML | Applications et Applets de Jeu |
>+---------------------------+--------------------------------+
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]