XSLT Conversion from XML to Plain HTML File
If you want to try this file, copy and paste the code into a file
called nutrition_plain.xslt
in the nutrition
directory. (See Linux
and Windows setup instructions.)
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" indent="yes"https://alistapart.com/> <xsl:template match="nutrition"> <html> <head> <title>Nutrition Facts Summary</title> </head> <body> <h2 align="center">Nutrition Facts Summary</h2> <p>Here are summaries of nutrition facts for several foods. They are based on these daily values:</p> <xsl:apply-templates select="daily-values"https://alistapart.com/> <hr width="80%" /> <xsl:apply-templates select="food"https://alistapart.com/> </body> </html> </xsl:template> <xsl:template match="daily-values"> <ul> <li>Fat <ul> <li>Total: <xsl:value-of select="total-fat"https://alistapart.com/><xsl:value-of select="total-fat/@units"https://alistapart.com/></li> <li>Saturated: <xsl:value-of select="saturated-fat"https://alistapart.com/><xsl:value-of select="saturated-fat/@units"https://alistapart.com/></li> </ul> </li> <li>Cholesterol: <xsl:value-of select="cholesterol"https://alistapart.com/><xsl:value-of select="cholesterol/@units"https://alistapart.com/></li> <li>Sodium: <xsl:value-of select="sodium"https://alistapart.com/><xsl:value-of select="sodium/@units"https://alistapart.com/></li> <li>Carbohydrates: <xsl:value-of select="carb"https://alistapart.com/><xsl:value-of select="carb/@units"https://alistapart.com/></li> <li>Fiber: <xsl:value-of select="fiber"https://alistapart.com/><xsl:value-of select="fiber/@units"https://alistapart.com/></li> <li>Protein: <xsl:value-of select="protein"https://alistapart.com/><xsl:value-of select="protein/@units"https://alistapart.com/></li> </ul> </xsl:template> <xsl:template match="food"> <h2><xsl:value-of select="name"https://alistapart.com/><xsl:text> </xsl:text> <i style="font-size:80%">(<xsl:value-of select="mfr"https://alistapart.com/>)</i></h2> <p>Serving size: <xsl:value-of select="serving"https://alistapart.com/><xsl:value-of select="serving/@units"https://alistapart.com/><br /> Calories: <xsl:value-of select="calories/@total"https://alistapart.com/> / Calories from fat: <xsl:value-of select="calories/@fat"https://alistapart.com/></p> <table border="1"> <tr><th>Amount/Serving</th><th>%Daily Value</th></tr> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Total Fat</xsl:with-param> <xsl:with-param name="node" select="total-fat"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Saturated Fat</xsl:with-param> <xsl:with-param name="node" select="saturated-fat"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Cholesterol</xsl:with-param> <xsl:with-param name="node" select="cholesterol"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Sodium</xsl:with-param> <xsl:with-param name="node" select="sodium"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Total Carbohydrates</xsl:with-param> <xsl:with-param name="node" select="carb"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Dietary Fiber</xsl:with-param> <xsl:with-param name="node" select="fiber"https://alistapart.com/> </xsl:call-template> <xsl:call-template name="info-row"> <xsl:with-param name="msg">Protein</xsl:with-param> <xsl:with-param name="node" select="protein"https://alistapart.com/> </xsl:call-template> </table> <p align="center"> Vitamin A <xsl:value-of select="vitamins/a"https://alistapart.com/>% <xsl:text disable-output-escaping="yes">&middot;</xsl:text> Vitamin C <xsl:value-of select="vitamins/c"https://alistapart.com/>% <xsl:text disable-output-escaping="yes">&middot;</xsl:text> Calcium <xsl:value-of select="minerals/ca"https://alistapart.com/>% <xsl:text disable-output-escaping="yes">&middot;</xsl:text> Iron <xsl:value-of select="minerals/fe"https://alistapart.com/>% </p> </xsl:template> <xsl:template name="info-row"> <xsl:param name="msg"https://alistapart.com/> <xsl:param name="node"https://alistapart.com/> <tr><td><b><xsl:value-of select="$msg"https://alistapart.com/><xsl:text> </xsl:text> <xsl:value-of select="$node"https://alistapart.com/><xsl:value-of select="https://alistapart.com/nutrition/daily-values/*[name(.)=name($node)]/@units"https://alistapart.com/></b> </td> <td align="right"> <xsl:value-of select="round(100 * $node div /nutrition/daily-values/*[name(.)=name($node)])"https://alistapart.com/><xsl:text>%</xsl:text> </td></tr> </xsl:template> </xsl:stylesheet>