This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
sort,count,number,group solved
- To: XSL-List at mulberrytech dot com
- Subject: sort,count,number,group solved
- From: quagly <quagly at home dot com>
- Date: Mon, 20 Mar 2000 20:23:36 -0800
- Reply-To: xsl-list at mulberrytech dot com
Over a week later, and I have solved the puzzle I posed here
earlier. Thanks to everyone who replied. Hope this is useful to
someone.
Here is my original posting, solution follows.
" 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?
Thanks,
-Quagly"
My solution:
<xsl:template match="/">
<DL>
<xsl:apply-templates select="//bar[not(. = preceding::bar)]">
<xsl:sort select="bar"/>
</xsl:apply-templates>
</DL>
</xsl:template>
<xsl:template match="bar">
<DT>
<xsl:number value="position()" format="1."/>
<xsl:value-of select="."/>-
<xsl:value-of select="count(//bar[.=current()])"/>
</DT>
</xsl:template>
</xsl:stylesheet>
It is amazing to me how easy this is now that I see it. I got very
caught up in for-eaches and variable assignments.
Please critique this for me. Can it be improved? Is there some
ingenious principle I have tapped into that can be articulated?
Or let me know if this is helpful to you.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list