Jump to content

Creating an HTML list with for-each


yoshimura

Recommended Posts

Hey there :) I'm trying to create a list of all <Heading> elements.Right now it's not being output correctly.

	<xsl:template match="/">		<html>			<head>				<link rel="stylesheet" type="text/css" href="lexpress.css" />				<title>L'Express — <xsl:value-of select="/Lexpress/Date"/></title>			</head>			<body>				<h1>L'Express</h1><p class="date"><xsl:value-of select="/Lexpress/Date"/></p>				<xsl:for-each select="/Lexpress/*/Heading">					<ul>						 <li>							 <xsl:value-of select="/Lexpress/*/Heading"></xsl:value-of>						 </li>					</ul>				</xsl:for-each>				<xsl:apply-templates select="Lexpress/*[position()!=1]"/>			</body>		</html>	</xsl:template>

Link to comment
Share on other sites

for-each changes the context node so inside you probably simply want

<xsl:value-of select="."/>

But don't go with lots of for-eachs if you want your stylesheets to be well-structured and maintainable, write templates and apply them with apply-templates. If you need to process elements with more than one way then use templates with modes.

Link to comment
Share on other sites

for-each changes the context node so inside you probably simply want
<xsl:value-of select="."/>

But don't go with lots of for-eachs if you want your stylesheets to be well-structured and maintainable, write templates and apply them with apply-templates. If you need to process elements with more than one way then use templates with modes.

Gotcha Martin,As you can see, I'm very new to programming (coming from an artistic background too!).I went with for-each in this case because the number of Headings vary.Regards,Eric
Link to comment
Share on other sites

I'm trying create a table of contents and link them with articles throughout. I've used the mode instead of for-each.The output is messed up though.The table of content is created and has generated hyperlinks.Anchors with name={generate-id()} aren't created.

	<xsl:template match="/">		<html>			<head>				<link rel="stylesheet" type="text/css" href="lexpress.css"/>				<title>L'Express — <xsl:value-of select="/Lexpress/Date"/></title>			</head>			<body>				<h1>L'Express</h1>				<p class="date">					<xsl:value-of select="/Lexpress/Date"/>				</p>				<h2>Table of Contents</h2>				<xsl:apply-templates mode="toc"/>				<hr/>				<xsl:apply-templates/>			</body>		</html>	</xsl:template>	<xsl:template match="Article" mode="toc">		<ul>			<li><a href="#{generate-id()}"><xsl:value-of select="Heading"/></a></li>		</ul>	</xsl:template>	<xsl:template match="Article">		<h1><a name="{generate-id()}"><xsl:value-of select="Heading"/></a></h1>		<xsl:apply-templates/>	</xsl:template>

XML:

<Lexpress>   <Article>	  <Heading   <Article…

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...