954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Applying conditional sorting using a parameter with xslt

I wonder if anyone can advise why the sort param is being ignored in this xslt? The code is always performing the 'otherwise' section regardless of the sort parameter.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

    <xsl:param name="sort"></xsl:param>

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Products">
        <xsl:element name="Products">
            <xsl:choose>
                <xsl:when test="$sort='DATE'">
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="number" order="descending" select="concat(substring(DateAdded,7,4), substring(DateAdded,4,2), substring(DateAdded,1,2))"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:when test="$sort='pricelow'">
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="number" order="ascending" select="FromPrice"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:when test="$sort='pricehigh'">
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="number" order="descending" select="FromPrice"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:when test="$sort='descaz'">
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="text" order="ascending" select="Title"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:when test="$sort='descza'">
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="text" order="descending" select="Title"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="Product">
                        <xsl:sort data-type="number" order="ascending" select="FromPrice"/>
                    </xsl:apply-templates>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Product">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

Thanks

liveland
Newbie Poster
16 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Hi Liveland,

In this XSLT , the value of sort param is blank or null. In used u dont have any which can test for null value of $sort. So it is going to .

#
DATE use this line in place of urs, it'll enter into first .

varun0703
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 

http://www.easytipsandtricks.com/browse/sort-xml-by-date-using-xslt follow this link to find the solution.

works great for me. cheers dss

solutiongiver
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You