...

View Full Version : Table via an xsl doc



babylikesburgh
08-21-2008, 09:58 PM
I'm almost as sure as I can be that I have this xsl doc set up correctly. And, I'm sure I have the xml doc correct, too. All I need help with is determining where I'm going wrong connecting the two. I've Googled "tables in xml stylesheets" and things of the sort, but still I can't find where I'm going wrong. The table header prints out fine (I have the xsl doc saved as an html doc currently so I can "test" it), but that's all I'm getting. I tried href-ing to nutrition.xsl while it was saved to that, too, but it was a no go. So, I think I have everything down, just not how to connect these two. Any suggestions, if only a hint, as to where I'm going wrong? This looks like it should be MUCH easier than I'm making it.. :o I have an idea I shouldn't be using <xsl:for-each select=" = "whitescookie"> in the stylesheet, I've tried a couple other things too that I've found on XML tip sites... ugh... I'm getting frustrated!




<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = nutrition.html"?>


<!--Marking up documents-->


<whitescookie>
<serving>1 package</serving>
<calories>260 calories</calories>
<fatcals>100 fat calories</fatcals>
<fat>11 g of fat</fat>
<saturatedfat>2 g of saturated fat</saturatedfat>
<cholesterol>5 mg of cholesterol</cholesterol>
<sodium>210 mg of sodium</sodium>
<carbohydrates>36 g of carbs</carbohydrates>
<fiber>2 g of fiber</fiber>
<sugar>15 g of sugar</sugar>
<protein>5 g of protein</protein>
</whitescookie>
...and the stylesheet "nutrition.html":




<?xml version = "1.0"?>

<!-- XSL Stylesheet -->

<!-- reference XSL style sheet URI -->
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

<xsl:output method = "html" omit-xml-declaration = "no"
doctype-system =
"http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd"
doctype-public = "-//W3C//DTD XHTML 1.1//EN"/>

<xsl:template match = "/">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Nutritional Information - Grandma White's Cookies</title>
</head>

<body>
<table border = "1" bgcolor = "cream">
<thead>
<tr>
<th>Nutritional Information</th>
</tr>
</thead>


<xsl:for-each select=" = "whitescookie">
<tr>
<td><xsl:value-of select = "@serving"/></td></tr>
<td><xsl:value-of select = "@calories"/></td></tr>
<td><xsl:value-of select = "@fatcals"/></td></tr>
<td><xsl:value-of select = "@saturatedfat"/></td></tr>
<td><xsl:value-of select = "@cholesterol"/></td></tr>
<td><xsl:value-of select = "@sodium"/></td></tr>
<td><xsl:value-of select = "@carbohydrates"/></td></tr>
<td><xsl:value-of select = "@fiber"/></td></tr>
<td><xsl:value-of select = "@sugar"/></td></tr>
<td><xsl:value-of select = "@protein"/></td></tr>
</tr>
</xsl:for-each>
</table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

oesxyl
08-22-2008, 01:36 AM
- missing '"' in xml
- @ is for attributes
- not calling anywhere xsl:apply-template
- invalid html markup, you close a tr tag with </tr> but don't have a start tag <tr>. I remove them because was not clear for me if you want to put all on one line or not. You can put them back.



<?xml version = "1.0"?>
<?xml-stylesheet type="text/xsl" href="nutrition.html"?>
<whitescookie>
<serving>1 package</serving>
<calories>260 calories</calories>
<fatcals>100 fat calories</fatcals>
<fat>11 g of fat</fat>
<saturatedfat>2 g of saturated fat</saturatedfat>
<cholesterol>5 mg of cholesterol</cholesterol>
<sodium>210 mg of sodium</sodium>
<carbohydrates>36 g of carbs</carbohydrates>
<fiber>2 g of fiber</fiber>
<sugar>15 g of sugar</sugar>
<protein>5 g of protein</protein>
</whitescookie>




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

<xsl:output method="html"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd"
doctype-public="-//W3C//DTD XHTML 1.1//EN"/>

<xsl:template match="/">
<html>
<head>
<title>Nutritional Information - Grandma White's Cookies</title>
</head>

<body>
<table border = "1" bgcolor = "cream">
<thead>
<tr>
<th>Nutritional Information</th>
</tr>
</thead>
<tr>
<xsl:apply-templates select="whitescookie"/>
</tr>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="whitescookie">
<td>
<xsl:value-of select = "serving"/>
</td>
<td>
<xsl:value-of select = "calories"/>
</td>
<td>
<xsl:value-of select = "fatcals"/>
</td>
<td>
<xsl:value-of select = "saturatedfat"/>
</td>
<td>
<xsl:value-of select = "cholesterol"/>
</td>
<td>
<xsl:value-of select = "sodium"/>
</td>
<td>
<xsl:value-of select = "carbohydrates"/>
</td>
<td>
<xsl:value-of select = "fiber"/>
</td>
<td>
<xsl:value-of select = "sugar"/>
</td>
<td>
<xsl:value-of select = "protein"/>
</td>
</xsl:template>

</xsl:stylesheet>


best regards

babylikesburgh
08-22-2008, 04:02 PM
oesxyl, you're my hero for the day ^_^

I didn't think I was TOO far off, but had very limited resources... I think I could really handle a good ol' friend or tutor that knows their programming. Your help here allowed me to see how the code/docs tie together and now my xml doc is showing up in the format it's supposed to be showing up in :D No more raw-code. Thanks a bunch for your help, it would have taken me "who-knows-how-long" to keep trial-and-erroring my scipt.

oesxyl
08-22-2008, 04:17 PM
oesxyl, you're my hero for the day ^_^

I didn't think I was TOO far off, but had very limited resources... I think I could really handle a good ol' friend or tutor that knows their programming. Your help here allowed me to see how the code/docs tie together and now my xml doc is showing up in the format it's supposed to be showing up in :D No more raw-code. Thanks a bunch for your help, it would have taken me "who-knows-how-long" to keep trial-and-erroring my scipt.
now probably you can make your schema to be more restrictive and more complex if you need, :)

some link that could help with xml, xslt and xpath:

http://www.jenitennison.com/xslt/index.html

http://www.zvon.org/xxl/XSLTreference/Output/index.html

http://www.zvon.org/

and of course, just ask, :)

best regards

jaci.lewis
04-16-2009, 04:16 AM
Typo Error

oesxyl
04-16-2009, 04:24 AM
please read cf rules:

http://www.codingforums.com/rules.htm

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum