E
Eric Anderson
Got a quick question trying to figure out the XSLT way of implementing
the functionality of associative arrays. Basically want I want to do is
the following:
<xsl:template name="foo">
<xslaram name="bar"/>
<xsl:choose>
<xsl:when test="$bar = 'a'">b</xsl:when>
<xsl:when test="$bar = 'c'">d</xsl:when>
<xsl:when test="$bar = 'e'">f</xsl:when>
....
...
..
..
<xsl:choose>
<xsl:template>
Now imagine bar is could be one of a hundred values. Also imagine that
we are not just printing out a simple value. Imagine we have some
complex code that just uses that simple value. We don't want to
continually repeat that complex code all down the page. So what I would
really like is something like:
<xsl:variable name="a" select="'b'"/>
<xsl:variable name="c" select="'d'"/>
<xsl:variable name="e" select="'f'"/>
<xsl:variable name="g" select="'h'"/>
.....
....
...
<xsl:template name="foo">
<xslaram name="bar">
<xsl:value-of select="${$bar}"/>
</xsl:template>
What I am trying to indicate here is that $bar is resolved to 'a', 'c',
'e', 'g' or any other sort of value. Then that value is used as a
variable to resolve to 'b', 'd', 'f', 'h' or any other sort of
associated variable. Now obviously my made up syntax doesn't work. My
question is what is the proper way to do this in XSLT? Basically how do
you create and use an associative array in XSLT? This would all be
equivalent to the following in Perl.
baz{a} = 'b';
baz{c} = 'd';
baz{e} = 'f';
baz{g} = 'h';
.....
....
...
sub foo {
my ( $bar ) = @_;
print $baz{$bar};
}
Any suggestions?
Eric
the functionality of associative arrays. Basically want I want to do is
the following:
<xsl:template name="foo">
<xslaram name="bar"/>
<xsl:choose>
<xsl:when test="$bar = 'a'">b</xsl:when>
<xsl:when test="$bar = 'c'">d</xsl:when>
<xsl:when test="$bar = 'e'">f</xsl:when>
....
...
..
..
<xsl:choose>
<xsl:template>
Now imagine bar is could be one of a hundred values. Also imagine that
we are not just printing out a simple value. Imagine we have some
complex code that just uses that simple value. We don't want to
continually repeat that complex code all down the page. So what I would
really like is something like:
<xsl:variable name="a" select="'b'"/>
<xsl:variable name="c" select="'d'"/>
<xsl:variable name="e" select="'f'"/>
<xsl:variable name="g" select="'h'"/>
.....
....
...
<xsl:template name="foo">
<xslaram name="bar">
<xsl:value-of select="${$bar}"/>
</xsl:template>
What I am trying to indicate here is that $bar is resolved to 'a', 'c',
'e', 'g' or any other sort of value. Then that value is used as a
variable to resolve to 'b', 'd', 'f', 'h' or any other sort of
associated variable. Now obviously my made up syntax doesn't work. My
question is what is the proper way to do this in XSLT? Basically how do
you create and use an associative array in XSLT? This would all be
equivalent to the following in Perl.
baz{a} = 'b';
baz{c} = 'd';
baz{e} = 'f';
baz{g} = 'h';
.....
....
...
sub foo {
my ( $bar ) = @_;
print $baz{$bar};
}
Any suggestions?
Eric