Topic: XSLT Troubles (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26081" title="Pages that link to Topic: XSLT Troubles (Page 1 of 1)" rel="nofollow" >Topic: XSLT Troubles <span class="small">(Page 1 of 1)</span>\

 
atomaka
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Jun 2005

posted posted 06-21-2005 21:12

Hey all,

I have an XML document that I am trying to convert to XML. My XML document is as follows:

code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<report>
<salescodes>
<code>AAA</code>
<code>BBB</code>
</salescodes>
<title>This is the Title</title>
<headers>
<header>Mo 1 (Accum)</header>
<header>Mo 2</header>
<header>Mo 3</header>

<header>Mo 4</header>
<header>Mo 5</header>
<header>Mo 6</header>
<header>Mo 7</header>
<header>Mo 8</header>
<header>Mo 9</header>
</headers>
<models>
<model>
<plantid>#####</plantid>

<modelyear>Model Year 1</modelyear>
<total>81194</total>
<accum>45484</accum>
<months>
<month>6863</month>
<month>7744</month>
<month>8959</month>
<month>10137</month>
<month>2007</month>

<month>0</month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
</months>
</model>
<model>
<plantid>######</plantid>
<modelyear>Model Year  2</modelyear>

<total>82696</total>
<accum>74858</accum>
<months>
<month>7838</month>
<month>0</month>
<month>0</month>
<month>0</month>
<month>0</month>
<month>0</month>

<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
<month></month>
</months>
</model>
</models>
</report>



I am using the following XSL file to translate my code:

code:
<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="report/title" /></title>
</head>
<body>
	<h2><xsl:value-of select="report/title" /></h2>
	<h3>
	<xsl:for-each select="report/salescodes">
		<xsl:value-of select="code" />, 
	</xsl:for-each>
	</h3>
	<table border="1">
		<tr bgcolor="#9acd32">
			<th align="left">Requirements</th>
			<xsl:for-each select="report/headers">
				<th align="left"><xsl:value-of select="header" /></th>
			</xsl:for-each>
			<th align="left">Total</th>
		</tr>
		<xsl:for-each select="report/models">
		<tr>
			<td><xsl:value-of select="modelyear" /></td>
			<td><xsl:value-of select="accum" /></td>
			<xsl:for-each select="months">
				<td><xsl:value-of select="month" /></td>
			</xsl:for-each>
			<td><xsl:value-of select="total" /></td>
		</tr>
		</xsl:for-each>
	</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>



Unfortunately, I am having some issues with my for-each loops. Most of the loops seem to iterate one time and then they die. I don't understand why this is happening so I thought I would ask you all!

The following is the output of my translation:

code:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title</title>
</head>
<body>
<h2>titel</h2>
<h3>AAA
	</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Requirements</th><th align="left">Mo 1 (Accum)</th><th align="left">Total</th>

</tr>
<tr>
<td></td><td></td><td></td>
</tr>
</table>
</body>
</html>



If anyone has any suggestions, I would love to hear them. Thank you for your time.

(Edited by atomaka on 06-21-2005 22:33)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-21-2005 21:59

You only have one salescodes element, but you have multiple code elements, try

<xsl:for-each select="report/salescodes/code">
<xsl:value-of select="." />,
</xsl:for-each>

The same for your models, you have only one models, but more than one model. You would do your for-each there.

Dan @ Code Town

atomaka
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jun 2005

posted posted 06-21-2005 22:32

Hey Dan,

Thanks a lot for your help. That did the trick. I'm not sure how I got confused with the level I needed to take for-each to, but it's all cleared up now. Once again, thanks for the help!

For reference, the corrected XSL file now looks like:

code:
<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="report/title" /></title>
</head>
<body>
	<h2><xsl:value-of select="report/title" /></h2>
	<h3>
	<xsl:for-each select="report/salescodes/code">
		<xsl:value-of select="." />, 
	</xsl:for-each>
	</h3>
	<table border="1">
		<tr bgcolor="#9acd32">
			<th align="left">Requirements</th>
			<xsl:for-each select="report/headers/header">
				<th align="left"><xsl:value-of select="." /></th>
			</xsl:for-each>
			<th align="left">Total</th>
		</tr>
		<xsl:for-each select="report/models/model">
		<tr>
			<td><xsl:value-of select="modelyear" /></td>
			<td><xsl:value-of select="accum" /></td>
			<xsl:for-each select="months/month">
				<td><xsl:value-of select="." /></td>
			</xsl:for-each>
			<td><xsl:value-of select="total" /></td>
		</tr>
		</xsl:for-each>
	</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu