[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

effective use of parameters and/or named templates?


Subject: effective use of parameters and/or named templates?
From: Linda van den Brink <lvdbrink@xxxxxxx>
Date: Tue, 27 Jul 1999 12:31:06 +0200

Hi all, 

I'm working on an XSL stylesheet which I think would benefit from the use of
parameters and/or named templates. However, having no programming background
whatsoever, I'm having difficulty in finding out if and how to use them. 

I have an XML document containing a very long list of software items, each
item having child elements like name, description, version, etc, and one or
more categories, like this: 

<software>
	<item>
		<name>Amaya for Windows NT</name>
		<version>2.1</version>
		<desc>Amaya is the name of the World Wide Consortium's own
test-bed browser/authoring tool and is used to demonstrate and test many of
the new developments in Web protocols and data formats. Given the very fast
moving nature of Web technology, Amaya has a central role to play. It is
versatile and extensible: new features can be easily added.</desc>
		<size>4.17 MB</size>
		<url>amaya.exe</url>
		<home>http://www.w3.org/Amaya/</home>
		<license>Freeware</license>
		<install>Install</install>
		<category>web browser</category>
	</item>
</software>

My aim is to transform this list to a whole series of HTML pages in which
the items are arranged by category. I couldn't arrange the XML document by
category, however, because one item can be in more than one category. 

What I know how to do is write a template that matches software, and has a
whole series of xsl:for-each elements, one for each category, like this
(fragment): 
		<xsl:for-each select="item[category='web browser']">
			<xsl:sort select="name"/>
			<xsl:element name="div">
				<xsl:attribute name="id">item<xsl:number
value="position()" format="1"/></xsl:attribute>
				<xsl:attribute
name="class">newsItem</xsl:attribute>
				<h4><xsl:apply-templates
select="name"/>&#160;<xsl:apply-templates select="version"/></h4>
				<p><xsl:apply-templates select="desc"/></p>
			</xsl:element>
		</xsl:for-each>
		<xsl:for-each select="item[category='browser add on']">
			<xsl:sort select="name"/>
			<xsl:element name="div">
				<xsl:attribute name="id">item<xsl:number
value="position()" format="1"/></xsl:attribute>
				<xsl:attribute
name="class">newsItem</xsl:attribute>
				<h4><xsl:apply-templates
select="name"/>&#160;<xsl:apply-templates select="version"/></h4>
				<p><xsl:apply-templates select="desc"/></p>
			</xsl:element>
		</xsl:for-each>
etc.... 

Which allows me to process the entire list of software items. However, since
there are a lot of categories, it seems to me I would be repeating a lot.
(everything between the xsl:for-each elements)

So, my question is how can I reduce the number of lines in my stylesheet by
using xsl:param or named templates (or are there other solutions as well)?
The nicest thing (as far as I can imagine) would be if the stylesheet could
find all the different categories and then apply a standard template once to
all of them. 

I would appreciate any help and explanations!


Linda van den Brink


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords