Re: [xsl] dynamic variable name/implementation question

Subject: Re: [xsl] dynamic variable name/implementation question
From: "Nathan Young" <natyoung@xxxxxxxxx>
Date: Thu, 25 Aug 2005 14:11:16 -0700
Ooops, sorry I was unclear.

I interpreted the path OP had gone down was something like:

<xsl:stylesheet>
	<xsl:param name="p1"/>
	<xsl:param name="p2"/>
	<xsl:param name="p3"/>
	...

in which case the template match could look like:

<xsl:template match="external-vaue">
	<xsl:choose>
		<xsl:when test="@parameter = 'someParam'">
		    <xsl:value-of select="$p1"/>
		</xsl:when>
		<xsl:when test="@parameter = 'someOtherParam'">
		    <xsl:value-of select="$p2"/>
		</xsl:when>
	</xsl:choose>
</xsl:template>

I understand the value of getting a nodeset instead. We use that technique to embed terms in the XML that get localized using an xsl and xml dictionary file. Simplified:

<xsl:stylesheet>
	<xsl:param name="language" select="'en'"/>
	<xsl:param name="country" select="'US'"/>
	<xsl:param name="dictName"/>
	<xsl:variable name="dictionary" select="document($dictName)"/>

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

<xsl:template match="translateterm">
<xsl:variable name="keyToFind" select="@key"/>
<xsl:value-of select="$dictionary/dictionary/bundle[@language=$language]/bundle[@country=$country]/translateterm[@key=$keyToFind]"/>
</xsl:template>

</xsl:stylesheet>



-------------->Nathan




On Thu, 25 Aug 2005 13:39:00 -0700, Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:

On 8/26/05, Nathan Young <natyoung@xxxxxxxxx> wrote:
Hi.

I agree that the nodeset solution offered below is the most flexible
solution (you can pass one in or load one with the document() function).
Parsers differ in the way they handle this; using LibXML with perl I use
the document function but haven't figured out how to pass a DOM as a param.


However, the way you describe passing things in using parameters
constrains you to a fixed set of params defined in your stylesheet so you
could go with a choose block in your template match.


Not at all. The passed nodeset may contain elements with arbitrary
names. you get access to whatever parameter you need with something
like this:

$pEnvironment[name() = 'myparamName']

In case you don't want the parameter name ('myparamName') fixed in the
stylesheet, it itself may be passed as a parameter to the stylesheet.

One way to implement dynamically created variables (such as in a
simple calculator) is described here:
     http://www.mulberrytech.com/Extreme/Proceedings/html/2003/Novatchev01/EML2003Novatchev01.html#t3.6

Cheers,
Dimitre Novatchev



--



.:||:._.:||:._.:||:._.:||:._.:||:._.:||:._.:||:._.:||:._.:||:._.:||:._.:||:.


Nathan Young
A: ncy1717
E: natyoung@xxxxxxxxx

Current Thread