Re: [xsl] sorting and preceding-sibling - only print *first* in sorted list??

Subject: Re: [xsl] sorting and preceding-sibling - only print *first* in sorted list??
From: Kenneth Stephen <marvin.the.cynical.robot@xxxxxxxxx>
Date: Tue, 12 Oct 2004 11:29:04 -0500
On Tue, 12 Oct 2004 10:38:51 -0400, Hardy Merrill
<hmerrill@xxxxxxxxxxxxxxxx> wrote:
> I like the idea, but I don't know how to do that.  Can you take pity on
> an XSL newbie and kindly show me how to do that?
> 
Hi,

    Using XSL 2.0, in a single program :

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

        <xsl:template match="/">
                <xsl:apply-templates />
        </xsl:template>

        <xsl:template match="legislators">
                <xsl:variable name="sorted">
                        <xsl:for-each select="legislator">
                                <xsl:sort select="district_type" />
                                <xsl:sort select="district_no"
data-type="number" />
                                <xsl:sort
select="legislator_active_date" data-type="number" order="descending"
/>
                                <xsl:copy-of select="." />
                        </xsl:for-each>
                </xsl:variable>
                <xsl:text>
</xsl:text>
                <xsl:for-each select="$sorted/legislator">
                        <xsl:variable name="districtNo" select="district_no" />
                        <xsl:choose>
                                <xsl:when
test="count(preceding-sibling::*[district_no = $districtNo]) = 0">
                                        <xsl:value-of select="$districtNo" />
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:text>   </xsl:text>
                                </xsl:otherwise>
                        </xsl:choose>
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="district_type" />
                        <xsl:text>              </xsl:text>
                        <xsl:value-of select="full_name" />
                        <xsl:text>
</xsl:text>
                </xsl:for-each>
        </xsl:template>

</xsl:stylesheet>

    And this produces :

<?xml version="1.0" encoding="UTF-8"?>
001 A           Doe, John J.
    A           Smith, Jane
002 A           Jones, Daniel

    If you are using an XSLT 1.0 processor, the following line in the
above code :

 <xsl:for-each select="$sorted/legislator">

    ...would need to use the appropriate nodeset extension function
for the processor for this to work. For example, for Xalan, the line
would say :

 <xsl:for-each select="xalan:nodeset($sorted)/legislator">

....assuming of course, that the "xalan" namespace prefix has been
defined correctly.

Regards,
Kenneth


Current Thread