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

[xsl] recursive looping and variable scope

Subject: [xsl] recursive looping and variable scope
From: Brian Newman <brian.newman@xxxxxxxxxxx>
Date: Mon, 4 Jan 2010 14:39:06 -0500

I'm writing a windows service which will transform documents as long as it has
documents to transform.
This requires some sort of looping mechanism and, according to my
understanding, it's better to stay in XSL once you get in XSL rather than keep
bouncing back and forth between imperative and declarative code.
So, I want to do the looping inside XSL.
Therefore, I have a named template which recursively calls itself and all my
document() functions are wrapped in saxon:discard-document().
So, in link1, I'm making a call to a database to get the next scheduled
In link2, I'm applying a transform on that document of all relevant data in
that document.
In link3, I'm applying any necessary outside data (data which largely comes
from the database).
Then I call result-document and output the results.

This works fine when I'm not looping.  But when I recursively call the same
named template and start over, the $filename variable isn't resetting.
Instead, I get an error "Cannot write more than one result document to the
same URI".  My guess is that I'm having trouble with the scope of $filename,
inheriting it's value from an earlier iteration of the template and, since
variables can't be reset, the new value sent to it is ignored.  Is that true?
and how do I get it to change?

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:include href="link1.xsl"/>
	<xsl:include href="link2.xsl"/>
	<xsl:include href="link3.xsl"/>
	<xsl:template match="/">
	<xsl:call-template name="Chain"/>
	<xsl:template name="Chain">
		<xsl:variable name="link1">
			<xsl:call-template name="getSchedule"/>
		<xsl:variable name="link2">
			<xsl:apply-templates mode="link2" select="$link1/jobs/job"/>
		<xsl:variable name="link3">
			<xsl:apply-templates mode="link3" select="$link2/policies"/>
		<xsl:variable name="filename">policy_<xsl:value-of
		<xsl:result-document href="status.xml">
				<xsl:copy-of select="$link3/node()"/>
		<xsl:call-template name="Chain"/>
	<xsl:template match="node()"/>

Current Thread