RE: [xsl] Apply-templates - how to omit top level element tags?

Subject: RE: [xsl] Apply-templates - how to omit top level element tags?
From: "Mike Schinkel" <mikes@xxxxxxxxx>
Date: Thu, 8 Sep 2005 18:38:03 -0400
>> XSLT variables always have the same value once bound

Sorry, I didn't mean "variable" in the literal sense, I meant
'something' changing, not necessarily literally a variable.

For example, you can insert a template that takes otherwise tested and
working code and it totally screws up the output, and there's no way to
guard against that happening (at least as far as I can see.)

Case in point, the following two examples where the first only differs
from the second by inclusion of this one line template:

	<xsl:template match="Name">
		<xsl:apply-templates/>	<!-- THIS CHANGES EVERYTHING!
-->
	</xsl:template>


===[Example 1]==============================================
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="html"/>

	<xsl:variable name="trademark-symbol">&#8482;</xsl:variable>

	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="Trademark">
		<xsl:value-of select="$trademark-symbol"/>
	</xsl:template>

	<xsl:template match="/How-To-Select/Guide">
		<html>
			<head><title>Test</title></head>
			<body>
				<h1>
					<!-- THIS DIDN'T WORK - INCLUDE
<Name/> TAGS -->
					<xsl:apply-templates
select="Name"/>
				</h1>
				<h1>
					<!-- THIS DIDN'T WORK - OMITS
Trademark Symbol -->
					<xsl:value-of select="Name"/>
				</h1>
				<h1>
					<!-- THIS DIDN'T WORK - INCLUDES
<Name/>, <Trademark/>, DOES NOT EXPAND <Trademark/> -->
					<xsl:copy-of select="Name"/>
				</h1>
				<h1>
					<!-- THIS IS WHAT I NEEDED -->
					<xsl:apply-templates
select="Name/node()"/>
				</h1>
				This is some more text just for show.
			</body>
		</html>
	</xsl:template>

</xsl:stylesheet>
===[Example 2]==============================================
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="html"/>

	<xsl:variable name="trademark-symbol">&#8482;</xsl:variable>

	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="Trademark">
		<xsl:value-of select="$trademark-symbol"/>
	</xsl:template>

	<xsl:template match="Name">
		<xsl:apply-templates/>	<!-- THIS CHANGES EVERYTHING!
-->
	</xsl:template>

	<xsl:template match="/How-To-Select/Guide">
		<html>
			<head><title>Test</title></head>
			<body>
				<h1>
					<!-- THIS DIDN'T WORK - INCLUDE
<Name/> TAGS -->
					<xsl:apply-templates
select="Name"/>
				</h1>
				<h1>
					<!-- THIS DIDN'T WORK - OMITS
Trademark Symbol -->
					<xsl:value-of select="Name"/>
				</h1>
				<h1>
					<!-- THIS DIDN'T WORK - INCLUDES
<Name/>, <Trademark/>, DOES NOT EXPAND <Trademark/> -->
					<xsl:copy-of select="Name"/>
				</h1>
				<h1>
					<!-- THIS IS WHAT I NEEDED -->
					<xsl:apply-templates
select="Name/node()"/>
				</h1>
				This is some more text just for show.
			</body>
		</html>
	</xsl:template>

</xsl:stylesheet>
============================================================
-Mike

-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx]
Sent: Thursday, September 08, 2005 6:13 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Apply-templates - how to omit top level element tags?


> Yes, but I don't have to worry about a global variable changing and
> having a tested part of my encapsulated code no longer work.  I have
> seen that happen many times when building my stylesheet, and can find
> nothing that changes this.

XSLT variables always have the same value once bound, people coming from
imperative programming languages sometimes expect that they should be
able to "change" the variable's value, which you can't do. You are the
first person I've seen that has complained that variables _do_ change
value. That never happens in XSLT.Some other problemmust be causing the
effect you see, if you posted a small example...


David


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. For more information on a proactive anti-virus
service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread