This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Sorting Problem
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] Sorting Problem
- From: Jörg Heinicke <joerg dot heinicke at gmx dot de>
- Date: Wed, 12 Sep 2001 23:46:17 +0200
- References: <LPBBINMALFCHOBCMKNMPKEIBCDAA.devc33@yahoo.co.uk>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Dev,
I think I understood: You want to know the "highest" school, which a pupil
with this or that name attends.
I build a dynamical version, I hope it was not to much "fun-work". I added
the ranking-information to the XML like the following:
<records>
<pupil/>
...
<schools>
<school valuation="1">Cambridge</school>
<school valuation="2">Oxford</school>
<school valuation="3">Eton</school>
</schools>
</records>
You can put this information to an extra-file or anywhere you want, you only
must adapt the stylesheet then.
Furthermore I don't know if it's a very nice solution, maybe somebody has a
better one.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="pupils" match="pupil" use="name"/>
<xsl:template match="records">
<xsl:copy>
<xsl:apply-templates
select="pupil[count(.|key('pupils',name)[1])=1]">
<xsl:sort select="count(key('pupils',name))"
order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="pupil">
<xsl:copy>
<xsl:apply-templates select="name"/>
</xsl:copy>
</xsl:template>
<xsl:template match="name">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
<school>
<xsl:call-template name="getSchool">
<xsl:with-param name="schools">
<xsl:for-each select="key('pupils',.)/school">
<xsl:value-of select="text()"/>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
</school>
</xsl:template>
<xsl:template name="getSchool">
<xsl:param name="schools"/>
<xsl:param name="valuation" select="'1'"/>
<xsl:choose>
<xsl:when
test="contains($schools,/records/schools/school[@valuation=$valuation])">
<xsl:value-of
select="/records/schools/school[@valuation=$valuation]"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="getSchool">
<xsl:with-param name="schools" select="$schools"/>
<xsl:with-param name="valuation" select="$valuation +
1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Do you understand the stylesheet? The working with keys, especially the
'count(.|key('pupils',name)[1])=1'-construct? This is the 'grouping using
the muenchian method'. More information on this you can find here:
http://www.jenitennison.com/xslt/grouping/muenchian.html.
Hope this helps for the first.
Joerg
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list