[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sorting Problem

Subject: Re: Sorting Problem
From: Jörg Heinicke <joerg.heinicke@xxxxxx>
Date: Wed, 12 Sep 2001 23:46:17 +0200
getschool records.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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.