Click to See Complete Forum and Search --> : XSLT: How to use xsl:sort and xsl:choose simultaneously


vinodxx
05-28-2009, 09:28 AM
Hello friends,

I have a newbie question.

How to use xsl:sort and xsl:choose simultaneously on a single stylesheet.

<xsl:for-each select = "CATALOG/CD">
<xsl:sort select = "TITLE" />
<xsl:choose>


<xsl:when test="PRICE &gt; 10">


<tr bgcolor = "#ececec">

<td ><xsl:value-of select="TITLE" /></td>
<td><xsl:value-of select="ARTIST" /></td>
<td><xsl:value-of select="COUNTRY" /></td>
</tr>

</xsl:when>

<xsl:otherwise>

<tr bgcolor = "#ababab">

<td ><xsl:value-of select="TITLE" /></td>
<td><xsl:value-of select="ARTIST" /></td>
<td><xsl:value-of select="COUNTRY" /></td>
</tr>

</xsl:otherwise>

</xsl:choose>
</xsl:for-each>


The above code will not work in Firefox.
The error returned was
Error loading stylesheet: Parsing an XSLT stylesheet failed.

Please give your advice.

JohnBampton
05-28-2009, 10:02 AM
Well it is hard to determine without seeing your full XSLT and your input XML. Other than that the code looks ok except it is a bit verbose. You should be using something like this:


<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="PRICE &gt; 10">#ececec</xsl:when>
<xsl:otherwise>#ababab</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<td ><xsl:value-of select="TITLE" /></td>
<td><xsl:value-of select="ARTIST" /></td>
<td><xsl:value-of select="COUNTRY" /></td>
</tr>

vinodxx
05-28-2009, 09:30 PM
Well it is hard to determine without seeing your full XSLT and your input XML.
Here is the XML file.
http://w3schools.com/xsl/cdcatalog.xml

jkmyoung
05-29-2009, 02:23 PM
We still need your full xsl, as that seems to be where the problem is (according to the error message)
Also, "CATALOG/CD"
will match nothing, as xml is case sensitive. Similarly for all your value-of elements.