This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Sorting outside of a for-each
- To: "Steve Bruce" <casey7_3_99 at hotmail dot com>
- Subject: Re: [xsl] Sorting outside of a for-each
- From: Jeni Tennison <jeni at jenitennison dot com>
- Date: Fri, 12 Oct 2001 17:00:26 +0100
- CC: XSL-List at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <F64g3Sv0YKpDS7hx27k00004648@hotmail.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Steve,
> The country_name tags are not returned in a sorted order. I want to
> loop by the country_sort tags, because they only show 1 instance of
> the items in the country_name tags. If I looped by the country_name,
> I would duplicate my results. For my output, I want to return the
> country_name for the country_sort_id that excludes the currency
> (dollar, pounds, francs, marks). So, what I believe I want to do is
> a for-each on the country_sort tag and then return the first
> country_name field for that id, assuming the country_name tags have
> been sorted in ascending order. This would then ensure the name with
> the currency is always second.
>
> However, I have not been able to determine a process for sorting the
> country_name tag without a corresponding for-each. Is this possible?
You can only sort when you're processing a node set, so within a
xsl:for-each or when applying templates with xsl:apply-templates.
However, you can put the xsl:if to select the first country in sorted
order *within* the xsl:for-each to get what you want. Try this:
<xsl:variable name="country_names" select="country_name" />
<xsl:variable name="country_sorts" select="country_sort" />
<!-- iterate over the country_sort elements -->
<xsl:for-each select="$country_sorts">
<!-- the $id variable holds the ID of the country -->
<xsl:variable name="id" select="@country_sort_id" />
<!-- iterate over those country_name elements that have that ID
-->
<xsl:for-each select="$country_names[@country_id = $id]">
<!-- sort them alphabetically, so the ones with currency names
come last -->
<xsl:sort />
<!-- extract the first of these -->
<xsl:if test="position() = 1">
<!-- give its value -->
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:for-each>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list