Jump to content

Help me solve XSL variable please....


kwonvic

Recommended Posts

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:decimal-format name="financial" /> <xsl:template match="/"> <html> <head> <title>Cruisin' Car Audio</title> </head> <body style="font-family:times new roman,helvetica,sans-serif;font-size:12pt; background-color:#FFFFFF"> <h1 style ="text-align:center; text-decoration:underline; color:Blue">Welcome to The Cruisin' Car Audio, choose whatever you like</h1> <xsl:for-each select="PRODUCTS/AUDIO"> <div style="background-color:#CCFF00;color:blue;padding:10px;font-weight:bold"> <xsl:value-of select="ID"/> <span style="font-weight:bold;margin-left:40px;color:#FF6600"> <xsl:value-of select="TITLE"/></span> - <xsl:call-template name="retail" /> </div> <div style="background-color:#CCFF99;padding:15px;font-size:12pt;font-weight:bold"> <span style="font-weight:bold;color:#FF6600;margin-left:30px"> Manufacture: <xsl:value-of select="MANUFACTURER"/></span> - Serial No: <xsl:value-of select="SERIALNO"/> <br/> <br/> <span style="font-weight:bold;color:#FF6600;margin-left:30px"> Weight: <xsl:value-of select="WEIGHT"/> || Dimensions: <xsl:value-of select="DIMENSIONS"/> || Warranty: <xsl:value-of select="WARRANTY"/></span> </div> <div style="background-color:#CCFFFF;font-size:12pt;padding:10px"> <span style="font-weight:bold;margin-left:30px;color:#FF6600"> Availability: <xsl:value-of select="AVAILABILITY"/></span> <br/> <span style="font-weight:bold;color:#FF6600;margin-left:30px"> Stock: <xsl:value-of select="STOCK"/></span> </div> <div style="margin-left:15px;margin-bottom:1em;font-size:8pt;padding:20px"> <span style="margin-left:10px;font-weight:bold;margin-bottom:1em;font-size:12pt"> <xsl:value-of select="DESCRIPTION"/> </span> <br/> <br/> <xsl:choose> <xsl:when test="CATEGORY='Books on cat audio installation'"> *add 20% tax </xsl:when> <xsl:otherwise> *add 15% tax </xsl:otherwise> </xsl:choose> </div> </xsl:for-each> </body></html></xsl:template> <xsl:variable name="audiotax" select="0.15" /> <xsl:variable name="bookstax" select="0.20" /> <xsl:template match="PRICE" name="retail"> <xsl:variable name="cost"> <xsl:value-of select="PRICE"/> </xsl:variable> <xsl:variable name="retail"> <xsl:value-of select="($cost * $audiotax) + $cost"/> </xsl:variable> <div style="font-weight:bold"> $<xsl:value-of select="format-number($retail, '###,###.00', 'financial')"/> </div> </xsl:template></xsl:stylesheet>dat one my code....what i want to ask is.....why when it was display the price show "$NaN" but actually i dont find any mistake in my variable code....anybody can help me solve this pleae??anyway this is my 1st posting.... :) thx before

Link to comment
Share on other sites

This could happen if PRICE is not a number, be it decimal (that follows the same format) or at least an integer. Could you give some sample XML to match that XSLT against?

Link to comment
Share on other sites

<PRODUCTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Assignment2.xsd"> <AUTOMOTIVE> <CATEGORY>Car Seats</CATEGORY> <TITLE>Booster Seat</TITLE> <AVAILABILITY>Available</AVAILABILITY> <STOCK>17</STOCK> <ID>#001</ID> <PRICE>$250</PRICE> <WEIGHT>35kg</WEIGHT> <MANUFACTURER>TOYOTA</MANUFACTURER> <SERIALNO>998877</SERIALNO> <WARRANTY>5 years</WARRANTY> <DIMENSIONS>100x60x80</DIMENSIONS> <DESCRIPTION>Good quality seats made by leather</DESCRIPTION> </AUTOMOTIVE>dats my xml document...

Link to comment
Share on other sites

<PRODUCTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Assignment2.xsd"> <AUTOMOTIVE> <CATEGORY>Car Seats</CATEGORY> <TITLE>Booster Seat</TITLE> <AVAILABILITY>Available</AVAILABILITY> <STOCK>17</STOCK> <ID>#001</ID> <PRICE>$250</PRICE> <WEIGHT>35kg</WEIGHT> <MANUFACTURER>TOYOTA</MANUFACTURER> <SERIALNO>998877</SERIALNO> <WARRANTY>5 years</WARRANTY> <DIMENSIONS>100x60x80</DIMENSIONS> <DESCRIPTION>Good quality seats made by leather</DESCRIPTION> </AUTOMOTIVE>dats my xml document...
Ah, now there's your problem. The $ sign. You need to remove it, because with it, PRICE is no longer convertable to a number. There are many ways of removing it. translate(), substring-after() or just substring(). The list goes on in EXSLT and XSLT 2.0.Replace
<xsl:variable name="cost"><xsl:value-of select="PRICE"/></xsl:variable>

with one of the following:

<xsl:variable name="cost" select="translate(PRICE,'$','')"/>

<xsl:variable name="cost" select="substring-after(PRICE,'$')"/>

<xsl:variable name="cost" select="substring(PRICE,'$')"/>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...