xslt sort options

M

Michael Hill

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:eek:utput 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>
 
J

Joris Gillis

Tempore 05:46:42 said:
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:eek:utput 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top