By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

xslt sort options

P: n/a
I have a section of a stylesheet (below) I am trying to configure.

The part I am having trouble with is the when test.
I have a series of pulldowns that I can select from, some are text and some
are number.

If the sort field is "field1" then I need to sort by number, but if "field2"
I need to sort by text. There are other fields as well.

Do I have to do a <xsl:when test=""> <snip> </xsl:test> for each field or
can't I just do sommething like:

<xsl:when test="$sortfield contains['field1','field3','field5','field7']>
<snip going to sort by number>
</xsl:when>
<xsl:when test="$sortfield contains['field2','field2','field4','field6']>
<snip going to sort by text>
</xsl:when>

Then I only have 2 when tests and not 8.

Thanks,

Mike

****** xslt segment below ******
<xsl:output method="xml"/>
<xsl:param name="sortorder" select="'descending'"/>
<xsl:param name="sortfield" />
<xsl:template match="/">
<root>
<xsl:choose>
<xsl:when test="$sortfield='field1'>
<xsl:for-each select="root/data" >
<xsl:sort select="*[name(.)=$sortfield]" order="{$sortorder}"
data-type="number"/>
<data><xsl:apply-templates select="*"/></data>
</xsl:for-each>
</xsl:when>
<xsl:when test="$sortfield='field2'>
<xsl:for-each select="root/data" >
<xsl:sort select="*[name(.)=$sortfield]" order="{$sortorder}"
data-type="number"/>
<data><xsl:apply-templates select="*"/></data>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</root>
</xsl:template>
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Tempore 05:46:42, die Thursday 13 January 2005 AD, hinc in foro {comp.text.xml} scripsit Michael Hill <hi****@charter.net>:
I have a section of a stylesheet (below) I am trying to configure.

The part I am having trouble with is the when test.
I have a series of pulldowns that I can select from, some are text and some
are number.
If the sort field is "field1" then I need to sort by number, but if "field2"
I need to sort by text. There are other fields as well.


Hi,

Try this stylesheet:
<xsl:output method="xml"/>
<xsl:param name="sortorder" select="'descending'"/>
<xsl:param name="sortfield" />

<xsl:template match="/">
<root>
<xsl:for-each select="root/data" >
<xsl:variable name="data-type">
<xsl:if test="contains('field1 field3 field5 field7', $sortfield)">number</xsl:if>
<xsl:if test="contains('field2 field4 field6 field8', $sortfield)">text</xsl:if>
</xsl:variable>
<xsl:sort select="*[name()=$sortfield]" order="{$sortorder}" data-type="{$data-type}"/>
<data><xsl:apply-templates select="*"/></data>
</xsl:for-each>
</root>
</xsl:template>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Deserta faciunt et innovationem appelant
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.