Jump to content

xsl:choose


xander85

Recommended Posts

hello all, and thanks in advance for any help i may receive. im trying to add a choose function to my xsl style sheethere is the code i have <table> <tr> <th>Artist</th> <th>Album</th> <th>Source</th> <th>Kind</th> <th>Added</th> </tr> <xsl:for-each select="library/music"> <xsl:sort select="Added" order="descending" /> <tr> <td><xsl:value-of select="artist"/></td> <td><xsl:value-of select="album"/></td> <td><xsl:value-of select="source"/></td> <td><xsl:value-of select="kind"/></td> <xsl:choose> <xsl:when test="Added > 2007-08-02"> <td bgcolor="#ff00ff"> <xsl:value-of select="Added"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="Added"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table>basically any thing that has the date greater than 2007-08-02 i want to highlight the date with a background color. but i cant seem to get it to work, i'm a complete n00b to xsl and would appreciate your input/advicethe table/site is hereacousticmushroom.com/library/library-date.xmlcheersxander

Link to comment
Share on other sites

XSLT 1.0, which is the only version currently supported by browsers, can't do that. At least not in this way. The reason is that the dade is viewed as a simple string. The processor doesn't undestand that this is a date.There are a few paths which could go to:1. Perform the transformation on the server with some XSLT 2.0 processor. In XSLT 2.0, you can "cast" this string to be a date, so that you could then compare it. This requires an environment in which you can install extensions, or at least ask your host to do so. If you use PHP, you could try out my own API from my signature (IF you can install it!). If not, you'll be best off using SAXON's .NET or JAVA API.2. Perform another transformation that will manipulate the string up to three elements or a single element with attributes. Something that would look like this in the output:

<added><date day="01" month="08" year="2007" /></added>

and then perform another transformation to compare only each of those nodes as numbers. This is needless unless the XML is not under your control or is already in production use.3. Directly change your XML to something like the above.Something tells me the XML is under your control, so you'll probably pick option 3, as that's the easiest.In that case, you could do something like this:

<table><tr><th>Artist</th><th>Album</th><th>Source</th><th>Kind</th><th>Added</th></tr><xsl:for-each select="library/music"><xsl:sort select="Added" order="descending" /><tr><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="album"/></td><td><xsl:value-of select="source"/></td><td><xsl:value-of select="kind"/></td><xsl:choose><xsl:when test="Added/date/@day > 02 and Added/date/@month > 08 and Added/date/@year > 2007"><td bgcolor="#ff00ff"><xsl:value-of select="Added"/></td></xsl:when><xsl:otherwise><td><xsl:value-of select="Added"/></td></xsl:otherwise></xsl:choose></tr></xsl:for-each></table>

Link to comment
Share on other sites

Why are you thanking me when it hasn't worked? :) What happened? What's the complete XSLT code? What's the error message?

Link to comment
Share on other sites

library-date.xsl - full code - http://acousticmushroom.com/library/library-date.xslsince i got your input the code has changed, and the date part has been removed, sry :)

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <link href="library.css" rel="stylesheet" type="text/css" />  <title>acousticmushroom.com .::. music . . .</title><body>	<div id="page">    <div id="header" onclick="location.href='http://www.acousticmushroom.com/blog/';" style="cursor: pointer;"/>   	<div id="content">        <h5>        <xsl:for-each select="library/stats">        	Xander's Music Collection<br/><xsl:value-of select="updated"/>       	</xsl:for-each>        </h5>		<h6>        <xsl:for-each select="library/stats">            <p>            artists - <xsl:value-of select="artists"/> . . .         	albums - <xsl:value-of select="albums"/> . . .         	songs - <xsl:value-of select="songs"/></p>            <p><xsl:value-of select="playback"/></p>            <p><xsl:value-of select="size"/></p>            <p>mp3 albums - <xsl:value-of select="mp3"/> . . .             aac albums - <xsl:value-of select="aac"/> . . .             lossless albums - <xsl:value-of select="lossless"/></p>       	            </xsl:for-each>        <p>to search the library use your browsers find function (CTRL+F)</p>		<p>        click <a href="http://www.acousticmushroom.com/blog">here</a> to return home</p>        <p>sort by<br/>        <a href="library-album.xml">album</a>,         <a href="library-artist.xml">artist</a>,         <a href="library-date.xml">date added</a>,         <a href="http://www.acousticmushroom.com/assets/files/library.pdf">pdf version</a>        </p>        </h6>	<table>    <tr>      <th>Artist</th>      <th>Album</th>      <th>Source</th>      <th>Kind</th>      <th>Added</th>    </tr>    <xsl:for-each select="library/music">    <xsl:sort select="Added" order="descending" />    <tr>      <td><xsl:value-of select="artist"/></td>      <td><xsl:value-of select="album"/></td>      <td><xsl:value-of select="source"/></td>      <td><xsl:value-of select="kind"/></td>      <td><xsl:value-of select="Added"/></td>    </tr>    </xsl:for-each>    </table>    </div>	</div>  </body>  </html></xsl:template></xsl:stylesheet>

library.xml - part code - http://acousticmushroom.com/library/library-date.xmlhere is just one of each of the entries i have atm 2007-08-06 is the date to be highlighted

<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" href="library-date.xsl"?><library>  <!-- Table music -->    <music>        <artist>oasis</artist>        <album>(what's the story) morning glory?</album>        <source>ash</source>        <kind>aac audio</kind>        <Added>2007-08-01</Added>    </music>    <music>        <artist>green day</artist>        <album>1,039/smoothed out slappy hours</album>        <source>eon</source>        <kind>mp3 audio</kind>        <Added>2007-08-06</Added>    </music></library>

library.css - incase u need it - http://acousticmushroom.com/library/library.csscheers

Link to comment
Share on other sites

Well, you haven't adjusted the XML as I proposed, so what approach would you like to take? Can you use the server to do the transformation?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...