Jump to content

How to do Unorded List XSL


someone11

Recommended Posts

What I'd like to do is use the data in my xml file with styling via xsl to create an unordered list. I've tried to do this via <xsl:apply-templates> and <xsl:template match=''> to no avail; I've messed around with <xsl:for-each> and <xsl:value-of> but when I have multiple <courses> in the same @category the repetition of <li> inside a single <ul> fails. I also am restricted to client-side transformations. The nav.xsl code snippet below is showing what I would like to happen with my xml, and does not have the proper <xsl:.... > tags.I appreciate any help that you can give me!spring.xml:

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="nav.xsl" ?><spring>	<course category="business" id="BUSS470">		<title>Business Negotiations I</title>				</course>					<course category="business" id="BUSS570">		<title>Business Negotiations II</title>				</course>				<course category="language" id="DEV1E867">		<title>Conversational Spanish I</title>				</course>	<course category="language" id="DEV1E868">		<title>Conversational Spanish II </title>				</course></spring>

nav.xsl:

<?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /> <xsl:template match="/"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Client Side XML Test</title> <link href="../css/nav-left.css" rel="stylesheet" type="text/css" /> </head><body><ul>		<li>XSL TAG FOR CATEGORY='business'				 <ul>							<li>									 XSL TAG FOR TITLE OF business course 1							</li>							<li>									 XSL TAG FOR TITLE OF business course 2							</li>				 </ul>	   </li>		<li>XSL TAG FOR CATEGORY='languages'				 <ul>							<li>									 XSL TAG FOR TITLE OF language course 1							</li>							<li>									 XSL TAG FOR TITLE OF language course 2							</li>				 </ul>	   </li></ul></body></html></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

this seems to work, it uses some advanced techniques that you should study, particularly the muenchian grouping method made popular by Jeni Tennison

<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">	<xsl:output method="html"/>	<xsl:key name="courses" match="course" use="@category" />	<xsl:template match="/">		<ul>			<xsl:for-each select="//course[count(. | key('courses', @category)[1]) = 1]">				<xsl:variable name="cat" select="@category" />				<li>Category: <xsl:value-of select="$cat"/></li>				<xsl:apply-templates select="//course[@category = $cat]" />			</xsl:for-each>		</ul>	</xsl:template>	<xsl:template match="course">		<ul>			<li><xsl:value-of select="title"/></li>		</ul>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Guest Daniel MS

Hello aalbetski, I would be happy if you explain the for-each expession [//course[count(. | key('courses', @category)[1]) = 1], what exactly it does with the key function and the count function?Thanks in advance.

Link to comment
Share on other sites

Jeni's explanation is here: http://www.jenitennison.com/xslt/grouping/muenchian.htmlThe following explanation was found at http://www.velocityreviews.com/forums/t165...an-sorting.html. This seems to say it best"the current context node . is being OR'd (a "set" operation) with the firstnode returned from the key (as you say) to contain a node set with acombination of both nodesIF the current node IS the same node as that returned from the key then thecombined node set will only contain ONE node, therfore count(the combinednodeset) would = 1Therefore the count = 1 is True for that node, the [] predicate is true andthe node is processed by the logic within for-each (and so you get the firstname in the set presumably"

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...