Manny0
Manny0

Reputation: 17

why does the XSL style sheet display html table with no XML (info/values)

I'm trying to create an XSL style sheet that displays value from an XML code into an HTML table. The table headers show in the browser but no values at all.

Both xsl:for-each select="" and xsl:value-of select="" are linked to my xml code and I believe it's correct but maybe it's an xml problem only, not really sure.

<xsl:stylesheet version = "1.0"
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

<xsl:output method = "html" doctype-system = "about:legacy-compat"/>
<xsl:template match = "/">
    <html xmlns = "http://www.w3.org/1999/xhtml">
        <head>
            <meta charset = "utf-8"/>        
            <link rel = "stylesheet" type = "text/css" href = "style.css"/>
        </head>
        <body>
            <table border = "1" syle = "background-color:blue">
                <thead>
                    <tr> 
                        <th> Calories</th>
                        <th> Fat - Calories</th>
                        <th> Fat - Grams </th>
                        <th> Saturated Fat</th>
                    </tr>
                </thead>
                <xsl:for-each select="nutrition/item">
                    <tr>
                        <td><xsl:value-of select="calories/amount"/></td>
                        <td><xsl:value-of select="caloriesFat/amount"/></td>
                        <td><xsl:value-of select="gramsFat/amount"/></td>
               </tr>                   
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "nutrition.xsl"?>
<nutrition:items xmlns:nutrition = "http://www.grandmascookies.com/nutrition">
<title>Grandma White's Cookies</title>
<item>
        <servingsize> 
                <amount> 1 </amount> 
                <unit> package </unit> 
                </servingsize>  
        <calories> 
                <amount> 260 </amount> 
                <unit> calories </unit> 
                </calories>
        <caloriesFat> 
                <amount> 100 </amount> 
                <unit> grams </unit> 
                </caloriesFat>    
        <gramsFat> 
                <amount> 11 </amount> 
                <unit> grams </unit> 
                </gramsFat>  
</item>
</nutrition:items>

Upvotes: 0

Views: 251

Answers (2)

zx485
zx485

Reputation: 29042

You were missing the namespace in the XSLT, so add it to the root element of the stylesheet

<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:nutrition = "http://www.grandmascookies.com/nutrition">

and then use it in the xsl:for-each:

<xsl:for-each select="nutrition:items/item">       <!-- here -->
  <tr>
    <td><xsl:value-of select="calories/amount"/></td>
    <td><xsl:value-of select="caloriesFat/amount"/></td>
    <td><xsl:value-of select="gramsFat/amount"/></td>
  </tr>  

You had only provided the namespace prefix nutrition without the element items.

Upvotes: 1

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

You have to declare the namespace "xmlns:nutrition" and use it accordingly.

There is no nutrition/item but a nutrition:items/item

You might take the short way and try //item (or use a wildcard like /*:items/*:item or //*:item if there are other namespaces involved)

Upvotes: 1

Related Questions