An interesting filtering problem

wxdqz

New Member
Hi XML folks,I have a filtering question and a solution.But it seems to me that for such a "simple" requirementthere should be a simple solution. My soultion isn't sosimple --even though it works. I tried many alternativesit did not work. I think I am missing some thing.I have a list of books some with rating and with out.The idea is to simply draw two tables. One that has the listof rated books and the other with the list of books that doesnot have rating. Rating is not an attribute. It is a siblingelement to book's title. Please see the XML below.Can some one point me to an elegent solution. And areference to this topic if possible.Thanks.Here is the source XML document:================================<?xml-stylesheet type="text/xsl" href=http://forums.devx.com/archive/index.php/"books.xsl"?><books><book><title>Rated Book#1</title><rating>3</rating></book><book><title>Un Rated Book#1</title></book><book><title>Rated Book#2</title><rating>7</rating></book><book><title>Rated Book#2</title></book><book><title>Rated Book#3</title><rating>2</rating></book></books>Here is the XSL sheet:======================<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="html" indent="yes"/><xsl:template match="/"><xsl:apply-templates/></xsl:template><xsl:template match="books"><table style="border: 1px solid black;"><th colspan="2">Rated books</th><xsl:apply-templates><xsl:with-param name="rated">yes</xsl:with-param></xsl:apply-templates></table><br/><br/><table style="border:1px solid black;"><th colspan="1">Un rated books</th><xsl:apply-templates><xsl:with-param name="rated"> no</xsl:with-param></xsl:apply-templates></table></xsl:template><xsl:template match="book"><xsl:param name="rated">yes</xsl:param><xsl:choose><xsl:when test="$rated='yes'"><xsl:if test="rating"><tr><xsl:apply-templates/></tr></xsl:if></xsl:when><xsl:otherwise><xsl:if test="not(rating)"><tr><xsl:apply-templates/></tr></xsl:if></xsl:otherwise></xsl:choose></xsl:template><xsl:template match="title | rating"><td><xsl:apply-templates/></td></xsl:template></xsl:stylesheet>And here is the output:=======================<table style="border: 1px solid black;"><th colspan="2">Rated books</th><tr><td>Rated Book#1</td><td>3</td></tr><tr><td>Rated Book#2</td><td>7</td></tr><tr><td>Rated Book#3</td><td>2</td></tr></table><br><br><table style="border:1px solid black;"><th colspan="1">Un rated books</th><tr><td>Un Rated Book#1</td></tr><tr><td>Rated Book#2</td></tr></table>=======Ends============================
 
Back
Top