xslt sort options

Discussion in 'XML' started by Michael Hill, Jan 13, 2005.

  1. Michael Hill

    Michael Hill Guest

    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>
    Michael Hill, Jan 13, 2005
    #1
    1. Advertising

  2. Michael Hill

    Joris Gillis Guest

    Tempore 05:46:42, die Thursday 13 January 2005 AD, hinc in foro {comp.text.xml} scripsit 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.


    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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Deserta faciunt et innovationem appelant
    Joris Gillis, Jan 15, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,022
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    499
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,062
  4. Navin
    Replies:
    1
    Views:
    674
    Ken Schaefer
    Sep 9, 2003
  5. Bryan Richardson
    Replies:
    6
    Views:
    270
    Bryan Richardson
    Feb 25, 2008
Loading...

Share This Page