Jump to content

Creating an HTML list with for-each


yoshimura
 Share

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 Edited by yoshimura
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…

Edited by yoshimura
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...