Re: [xsl] xsl:variable inside xsl:for-each

Subject: Re: [xsl] xsl:variable inside xsl:for-each
From: Owen Rees <owen.rees@xxxxxx>
Date: Wed, 12 Apr 2006 16:22:18 +0100
--On 12 April 2006 14:31 +0100 David Carlisle wrote:

Sorry I have no idea what you mean by this. 9 times out of 10 when
people say they "need a variable" the final solution suggested on the
list doesn't use (or doesn't need to use) variables at all.

I suspect that the idea is to create variables dynamically and evaluate strings that start with '$' as references to those dynamically created variables. Unless the object of the exercise really is to invent a new language and write an interpreter for it in XSLT, a more direct approach is much easier.

Here is an example that when applied to the example input (when wrapped in some element) delivers the requested output and perhaps uses the intended indirections. Note that it uses variables but not for-each or choose, and I think you could eliminate the variables by using current().

<xsl:stylesheet version="1.0" xmlns:xsl="";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<table border="1">
<xsl:template match="sxa|sya|ffA">
<xsl:variable name="v" select="substring(@position,2)"/>
<xsl:variable name="name" select="substring(../../ec[@name = $v]/@value,2)"/>
<xsl:variable name="value" select="../../../../ec[@name = $name]/@value"/>
<xsl:value-of select="$name"/>
<xsl:value-of select="$value"/>

Owen Rees
Hewlett Packard Laboratories, Bristol, UK

Current Thread