xslt - Selecting the lowest value from and xml without sorting -
i'm transforming xml document html table using xslt, , want change background color of cell containing lowest "price" value without sorting list.
i'm new on this, i'm doing following w3c schools examples. xml file looks this:
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited xmlspy® --> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>black angel</title> <artist>savage rose</artist> <country>eu</country> <company>mega</company> <price>11.90</price> <year>1995</year> </cd> <cd> <title>for times</title> <artist>kenny rogers</artist> <country>uk</country> <company>mucik master</company> <price>8.70</price> <year>1995</year> </cd> </catalog>
and want obtain similar without ordering elements in list price. want maintain original order in xml document.
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited xmlspy® --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> <th>price</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="price" order="ascending" data-type="number"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> <xsl:choose> <xsl:when test="(position() = 1)"> <td bgcolor="#ff00ff"> <xsl:value-of select="price"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="price"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
thanks in advance help.
edit: think have found solution question:
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited xmlspy® --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> <th>price</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> <xsl:choose> <xsl:when test="price=/catalog/cd/price[not(. > ../../cd/price)][1]"> <td bgcolor="#ff00ff"> <xsl:value-of select="price"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="price"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
you seem have answered question already, throw in couple of other things in here.
(1) can avoid re-calculating minimum price @ each iteration through <cd/>
elements adding variable stylesheet or template calculation once:
<xsl:variable name="least" select="math:min(/catalog/cd/price)"/>
(2) "$least" variable declaration uses exslt math:min function (namespace xmlns:math="http://exslt.org/math"), may available xslt 1.0 processor. if have 2.0 processor, can use built-in "min()".
if nothing else, min() function more readable next person.
(3) purely stylistic, can reduce branching bit using xsl:attribute.
<td> <xsl:if test="price=$lowest"> <xsl:attribute name="bgcolor">#ff00ff</xsl:attribute> </xsl:if> <xsl:value-of select="price"/> </td>
Comments
Post a Comment