This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Re: Re: Adjusting sorted list
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 16 May 2002 10:21:09 -0700 (PDT)
- Subject: [xsl] Re: Re: Re: Adjusting sorted list
- Reply-to: xsl-list at lists dot mulberrytech dot com
Andrew Timberlake <andrew dot lists at ddd dot co dot za> wrote:
>
> I've been playing with this idea and have created an interesting side
> effect.
>
> Using source:
> <root>
> <a>
> <b val="4">four</b>
> <b val="9">nine</b>
> <b val="6">six</b>
> <b val="1">one</b>
> <b val="8">eight</b>
> <b val="6">six</b>
> <b val="4">four</b>
> <b val="7">seven</b>
> </a>
> <a>
> <b val="9">nine</b>
> <b val="5">five</b>
> <b val="5">five</b>
> </a>
> </root>
>
> And style sheet:
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:output omit-xml-declaration="yes" indent="yes"/>
>
> <xsl:key name="kRanking" match="b" use="@val"/>
>
> <xsl:template match="/root/a">
> <a>
> <xsl:for-each select="b[generate-id()
> =
> generate-id(key('kRanking',@val)[1])
> ]">
> <xsl:sort select="@val" data-type="number"/>
>
> <xsl:variable name="vPos" select="position()"/>
>
> <xsl:for-each select="key('kRanking',@val)">
> <b rank="{$vPos}">
> <xsl:value-of select="."/>
> </b>
>
> </xsl:for-each>
> </xsl:for-each>
> </a>
> </xsl:template>
> </xsl:stylesheet>
>
> I now get:
> <a>
> <b rank="1">one</b>
> <b rank="2">four</b>
> <b rank="2">four</b>
> <b rank="3">six</b>
> <b rank="3">six</b>
> <b rank="4">seven</b>
> <b rank="5">eight</b>
> <b rank="6">nine</b>
> <b rank="6">nine</b>
> </a>
> <a>
> <b rank="1">five</b>
> <b rank="1">five</b>
> </a>
>
> What have I done???
>
> Andrew
Just try this one:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kRanking" match="b" use="@val"/>
<xsl:template match="/root/a">
<xsl:variable name="theParentId" select="generate-id()"/>
<a>
<xsl:for-each select="//b[generate-id()
=
generate-id(key('kRanking',@val)[1])
]">
<xsl:sort select="@val" data-type="number"/>
<xsl:variable name="vPos" select="position()"/>
<xsl:for-each select="key('kRanking',@val)">
<xsl:if test="generate-id(ancestor::*[1]) = $theParentId">
<b rank="{$vPos}">
<xsl:value-of select="."/>
</b>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</a>
</xsl:template>
</xsl:stylesheet>
With your new sorce xml it produces the correct result:
<a>
<b rank="1">one</b>
<b rank="2">four</b>
<b rank="2">four</b>
<b rank="4">six</b>
<b rank="4">six</b>
<b rank="5">seven</b>
<b rank="6">eight</b>
<b rank="7">nine</b>
</a>
<a>
<b rank="3">five</b>
<b rank="3">five</b>
<b rank="7">nine</b>
</a>
__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list