Template Modes

Contact Us or call 1-877-932-8228
Template Modes

Template Modes

Sometimes it will be necessary for a stylesheet to visit a node multiple times outputting different results each time. For example, imagine an XML document for a book made up of many chapters. The XSLT might output the chapter titles in the table of contents as well as at the beginning of each chapter. You could do this by having two templates that match book/chapter. However, the processor has to be able to distinguish between the two templates, so it knows when to use which. This can be accomplished by applying the mode attribute to the relevant xsl:apply-templates and xsl:template tags. The files below illustrate how mode is used.

Code Sample:

TemplatesParametersVariables/Demos/BeatlesMode.xml
<?xml version="1.0"?>
<?xml-stylesheet href="BeatlesMode.xsl" type="text/xsl"?>
<beatles>
<beatle link="http://www.paulmccartney.com" image="Images/Paul.jpg">
	<name>
		<firstname>Paul</firstname>
		<lastname>McCartney</lastname>
	</name>
	<bio src="http://www.imdb.com/name/nm0005200/bio">Played bass guitar for 
The Beatles in the 1960s. Considered the most successful pop composer 
of all time. In the "Guinness Book of World Records" for most records
sold, most #1s (shared) and largest paid audience for a concert (350,000+
people, 1989 in Brazil). After the Beatles, he formed Wings, one of the most 
commercially successful groups of the 1970s. Post-70s solo career has been 
sporadic in both commercial and artistic terms. Successes include albums 
"Tug of War" and "Flowers in the Dirt". Flops include movie Give My Regards 
to Broad Street (1984) and album "Press to Play".</bio>
</beatle>
<beatle link="http://www.johnlennon.com" image="Images/John.jpg">
	<name>
		<firstname>John</firstname>
		<lastname>Lennon</lastname>
	</name>
	<bio src="http://www.imdb.com/name/nm0006168/bio">John Winston 
	(later Ono) Lennon was born on October 9, 1940, in Liverpool England.
	In the mid 1950s he formed his first band The Quarrymen (after Quarry 
	Bank High School, which he attended), who with the addition of Paul 
	McCartney and George Harrison, later became the Beatles. After some 
	years of performing in Liverpool and Hamburg Germany, Beatlemania 
	erupted in England and Europe in 1963 after the release of their singles 
	"Love Me Do" and "Please Please Me". The next year the Beatles 
	flew to America to appear on the Ed Sullivan Show, and 
	Beatlemania spread worldwide...</bio>
</beatle>
<beatle link="http://www.georgeharrison.com" image="Images/George.jpg">
	<name>
		<firstname>George</firstname>
		<lastname>Harrison</lastname>
	</name>
<bio src="http://www.rollingstone.com/artist/bio/_/id/55146/georgeharrison">
	The youngest member of the Beatles, George Harrison was often 
	considered "the shy one." In fact, his strong voice was arguably the 
	most underrated of the four. Lennon and McCartney often passed 
	over many of Harrison's song submissions, recording only a small 
	selection of his masterpieces per album (including "While My Guitar 
	Gently Weeps" and "Here Comes The Sun"). When Harrison's first solo
	recording, All Things Must Pass, was released, many music critics
	felt that the triple album's fruitfulness was a direct result of Lennon 
	and McCartney's neglect. In his work with the Beatles and solo, 
	Harrison blended earthy Roots tones with droning eastern influences 
	and cascading melodies. Compared to Lennon's barbed and bluesy 
	grit or McCartney's jaunty pop sensibilities, the sound was organic 
	and blooming, huge yet worldly. His songs unfolded unpredictably 
	and took the listener to unfamiliar sonic regions. Music enthusiasts 
	credit him for integrating eastern sounds into western music -- Harrison 
	introduced the sitar and Indian ragas to pop music. He also 
	spearheaded the Concert for Bangladesh in 1971, a music festival 
	assembled to benefit Bengali refugees of the India-Pakistan war. 
	Harrison passed away on Nov. 29, 2001, in Los Angeles after 
	losing a prolonged battle with cancer.</bio>
</beatle>
<beatle link="http://www.ringostarr.com" image="Images/Ringo.jpg">
	<name>
		<firstname>Ringo</firstname>
		<lastname>Starr</lastname>
	</name>
	<bio src="http://www.imdb.com/name/nm0823592/bio">Popular drummer, Ringo 
	Starr was born Richard Starkey in Liverpool, Merseyside, England, on 
	July 7, 1940.	His parents were divorced when he was 3 and her mother 
	and Richard moved to another home of the same city. While attending 
	Silas Infants' Schools, he was suffering the many diseases which ruined 
	his education. Growing up, he was suffering from severe abdominal pains. 
	He was also diagnosed with a ruptured appendix that led to an inflamed
	peritoneum, that also led to one of his first surgeries. He was in a coma 
	for a couple of months during which more operations 
	were available, and he was known to be accident prone...</bio>
</beatle>
</beatles>

Code Sample:

TemplatesParametersVariables/Demos/BeatlesMode.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="xml" indent="yes"/>
	<xsl:template match="/">
		<html>
			<head>
				<title>The Beatles</title>
			</head>
			<body>
				<h1 align="center">THE BEATLES</h1>
				<div align="center">
					<xsl:apply-templates mode="links"/>
				</div>
				<hr/>
				<xsl:apply-templates mode="body"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="beatle" mode="links">
		<a href="#{name/firstname}"><img src="{@image}" border="0"
				title="{name/firstname} {name/lastname}"/></a>
	</xsl:template>
	<xsl:template match="beatle" mode="body">
		<a name="{name/firstname}"></a>
		<h2>
			<xsl:value-of select="name/firstname"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="name/lastname"/>
		</h2>
		<p><xsl:value-of select="bio"/></p>
	</xsl:template>
</xsl:stylesheet>

The first xsl:apply-templates instructs the processor to continue to look through the XML source document for nodes that match any templates with the mode set to "links". The second xsl:apply-templates instructs the processor to continue to look through the XML source document again for nodes that match any templates with the mode set to "body". The result is shown in TemplatesParametersVariables/Demos/BeatlesModeOutput.html, which looks like this in a browser.

Let's take a look at one beatle element in the XML document and the two relevant templates in the XSLT and the associated xsl:apply-templates tags.

<beatle link="http://www.paulmccartney.com" image="Images/Paul.jpg"> <name> <firstname>Paul</firstname> <lastname>McCartney</lastname> </name> <bio src="http://www.imdb.com/name/nm0005200/bio"> Played bass guitar... ... ...and album "Press to Play"...</bio> </beatle>
<div align="center"> <xsl:apply-templates mode="links"/> </div> <hr/> <xsl:apply-templates mode="body"/> <xsl:template match="beatle" mode="links"> <a href="#{name/firstname}"><img src="{@image}" border="0" title="{name/firstname} {name/lastname}"/></a> </xsl:template> <xsl:template match="beatle" mode="body"> <a name="{name/firstname}"></a> <h2> <xsl:value-of select="name/firstname"/> <xsl:text> </xsl:text> <xsl:value-of select="name/lastname"/> </h2> <p><xsl:value-of select="bio"/></p> </xsl:template>

Here is how the processor steps through this transformation.

  1. Finds first xsl:apply-templates tag with mode set to "links" and continues to look through the source XML document's nodes starting from the root.
  2. Finds first beatle node in XML document and applies matching template with the mode set to "links". This creates HTML code that outputs a picture of Paul McCartney linked to an anchor on the same page.
  3. Repeats step 2 for all other beatle nodes in the XML document.
  4. Finds second xsl:apply-templates tag with mode set to "body" and continues to look through the source XML document's nodes starting from the root.
  5. Finds first beatle node in XML document and applies matching template with the mode set to "body". This create HTML code that includes an anchor named "Paul", a "Paul McCartney" heading, and Paul's bio.
  6. Repeats step 5 for all other beatle nodes in the XML document.
Next