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

Re: sorting and preceding-sibling - only print *first*

Subject: Re: 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
xsl sort preceding
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

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.