Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple xsl question 1

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I have an xml document that looks similar to this:

<Works>
<Work day=&quot;Monday&quot; length=&quot;5&quot; time=&quot;now&quot; />
<Work day=&quot;Tuesday&quot; length=&quot;5&quot; time=&quot;now&quot; />
</Works>

I'm trying to create the xsl that will loop through all the works and get the day attribute and put it on the screen. I'm not sure how to do this. Can someone help?

Thanks!
 
Ecannizzo --

<table>
<xsl:for-each select=&quot;/Works/Work&quot;>
<xsl:call-template name=&quot;table-row&quot;/>
</xsl:for-each>
</table>

<xsl:template name=&quot;table-row&quot;>
<tr>
<td><xsl:value-of select=&quot;day&quot;/></td>
<td><xsl:value-of select=&quot;length&quot;/></td>
<td><xsl:value-of select=&quot;time&quot;/></td>
</tr>
</xsl:template>

 
Ecannizzo -- oops... slight error in the value-of statements... left off the &quot;@&quot; --

I'm sorry. The correct format would be...

<table>
<xsl:for-each select=&quot;/Works/Work&quot;>
<xsl:call-template name=&quot;table-row&quot;/>
</xsl:for-each>
</table>

<xsl:template name=&quot;table-row&quot;>
<tr>
<td><xsl:value-of select=&quot;@day&quot;/></td>
<td><xsl:value-of select=&quot;@length&quot;/></td>
<td><xsl:value-of select=&quot;@time&quot;/></td>
</tr>
</xsl:template>

 
Thank you. I will definitely look into templates and how to use them!

Erica
 
Given my above code, should this work? Because it doesn't...

Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] xmlns:fo=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Format&quot;>[/URL]
<xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; omit-xml-declaration=&quot;no&quot; indent=&quot;no&quot; media-type=&quot;text/html&quot;/>
	<!--XSL Stylesheet for generating Rep-->
	<xsl:template match=&quot;/&quot;>
		<html>
			<head><title>Repertoire</title></head>
			<body>
				<table>
					<tr>
						<th>Day</th>
						<th>Time</th>
						<th>#</th>
					</tr>
					<xsl:for-each select=&quot;/Works/Work&quot;>
						<xsl:call-template name=&quot;table-row&quot; />
					</xsl:for-each>
				</table>					
			</body>
		</html>
	</xsl:template>
	
	<xsl:template name=&quot;table-row&quot;>
		<tr>
			<td><xsl:value-of select=&quot;@day&quot;/></td>
			<td><xsl:value-of select=&quot;@time&quot;/></td>
			<td>1</td>
		</tr>
	</xsl:template>


</xsl:stylesheet>
 
I posted your XML and XSL on one of my sites at and it seems to work fine. If you don't see the difference between what I have posted here and your code, let me know. You can use the URL at my site to see if the page works for you there. It did work for me just now.

The exact content of each file I posted on the site is listed below for your reference...

---------------------------------------------
test.xml
---------------------------------------------

<?xml version=&quot;1.0&quot;?>
<?xml-stylesheet href=&quot; type=&quot;text/xsl&quot;?>

<Works>
<Work day=&quot;Monday&quot; length=&quot;5&quot; time=&quot;now&quot; />
<Work day=&quot;Tuesday&quot; length=&quot;5&quot; time=&quot;now&quot; />
</Works>

---------------------------------------------
test.xsl
---------------------------------------------

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; xmlns:fo=&quot;<xsl:eek:utput method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; omit-xml-declaration=&quot;no&quot; indent=&quot;no&quot; media-type=&quot;text/html&quot;/>

<!--XSL Stylesheet for generating Rep-->
<xsl:template match=&quot;/&quot;>
<html>
<head><title>Repertoire</title></head>
<body>
<table>
<tr>
<th>Day</th>
<th>Time</th>
<th>#</th>
</tr>
<xsl:for-each select=&quot;/Works/Work&quot;>
<xsl:call-template name=&quot;table-row&quot; />
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template name=&quot;table-row&quot;>
<tr>
<td><xsl:value-of select=&quot;@day&quot;/></td>
<td><xsl:value-of select=&quot;@time&quot;/></td>
<td>1</td>
</tr>
</xsl:template>


</xsl:stylesheet>
 
When I click on your site all I see is the code. I don't see the actual table with things filled in it. I don't know why I can't get this to work!

Thanks for your help.
Erica
 
I think I just got it working. I had to use //Work instead of /Works/Work.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top