By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

Generate variable name from node values in xslt

P: n/a
I include a xslt stylesheet with variables for all the error messages in
my system.

<xsl:variable name="Banner_error_1">
errormessage 1 for banner
</xsl:variable>

<xsl:variable name="Banner_error_2">
errormessage 2 for banner
</xsl:variable>

If my system finds an error it generates this xml

<error>
<nr>1</nr>
<plugin>Banner</plugin>
</error>
I would then like to select the variable based on my xml
something like

<xsl:value-of select="${plugin}_error_{nr}"/>

This should do the same as $$variable in php.
ie.
<?php
$foo = 'bar';
$bar = 'baz';

echo $$foo; // will echo 'baz'
?>

Is it at all possible to do this in xslt?
Or is there another way that i have not thought about?

I hope you can se the idea.

Frederik Sørensen
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Tempore 21:53:53, die Sunday 30 January 2005 AD, hinc in foro {comp.text.xml} scripsit Frederik Sørensen <he******@NOSPAM.SPAM.patch.dk>:
I include a xslt stylesheet with variables for all the error messages in
my system.

<xsl:variable name="Banner_error_1">
errormessage 1 for banner
</xsl:variable>

<xsl:variable name="Banner_error_2">
errormessage 2 for banner
</xsl:variable>

If my system finds an error it generates this xml

<error>
<nr>1</nr>
<plugin>Banner</plugin>
</error>
I would then like to select the variable based on my xml
something like

<xsl:value-of select="${plugin}_error_{nr}"/> Hi,

First of all, it seems rather bizar to use variables for this problem. You can easily store those variables in a seperate XML and let the XSLT access this resource file.

But if you really like to work with the variables, then this type of solution exists:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="Banner_error_1">
errormessage 1 for banner
</xsl:variable>

<xsl:variable name="Banner_error_2">
errormessage 2 for banner
</xsl:variable>

<xsl:template match="error">
<xsl:value-of select="document('')/xsl:stylesheet/xsl:variable[@name=concat(current()/plugin,'_error_', current()/nr)]"/>
</xsl:template>

</xsl:stylesheet>

(note that it doesn't treat the variables as real XSLT variables, but rather as xml nodes)

or, if you prefer keys, you can do somethink like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:key name="error" match="xsl:variable" use="concat(substring-before(@name,'_'),substring-after(@name,'_error_'))"/>

<xsl:variable name="Banner_error_1">
errormessage 1 for banner
</xsl:variable>

<xsl:variable name="Banner_error_2">
errormessage 2 for banner
</xsl:variable>

<xsl:template match="error">
<xsl:variable name="lookup" select="concat(plugin, nr)"/>
<xsl:for-each select="document('')">
<xsl:value-of select="key('error',$lookup)"/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

echo $$foo; // will echo 'baz'
?>
Is it at all possible to do this in xslt?


No, I that's not possible in XSLT1.0.
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
Jul 20 '05 #2

P: n/a
Joris Gillis wrote:
[snip]

Hi,

First of all, it seems rather bizar to use variables for this problem.
You can easily store those variables in a seperate XML and let the XSLT
access this resource file. [snip] regards,


That sounds like a good idea how would i do that?

Frederik
Jul 20 '05 #3

P: n/a
Frederik Sørensen wrote:
I include a xslt stylesheet with variables for all the error messages in
my system.

<xsl:variable name="Banner_error_1">
errormessage 1 for banner
</xsl:variable>

<xsl:variable name="Banner_error_2">
errormessage 2 for banner
</xsl:variable>

If my system finds an error it generates this xml

<error>
<nr>1</nr>
<plugin>Banner</plugin>
</error>
I would then like to select the variable based on my xml
something like

<xsl:value-of select="${plugin}_error_{nr}"/>

This should do the same as $$variable in php.
ie.
<?php
$foo = 'bar';
$bar = 'baz';

echo $$foo; // will echo 'baz'
?>

Is it at all possible to do this in xslt?
Or is there another way that i have not thought about?

I hope you can se the idea.

Frederik Sørensen


I had the same challenge last year. The only idea I could come up with
was to declare my errormessages as variables like this:

--- snip ---
<xsl:variable name="_11">Postnummeret er ikke gyldigt.</xsl:variable>
<xsl:variable name="_12">Ingen felter må være tomme.</xsl:variable>
--- snip ---

Then have a template with code like this

--- snip ---
<xsl:choose>
<xsl:when test="$msg=1">
<h2>
<xsl:value-of select="$_1"/>
</h2>
<p/>
</xsl:when>
etc etc etc ad nauseam
--- snip ---
Jul 20 '05 #4

P: n/a
Frederik Sørensen <he******@NOSPAM.SPAM.patch.dk> writes:
Is it at all possible to do this in xslt?
No variable names are resolved during compilation in XSLT just as for
most other compiled languages, C Fortran etc.
Or is there another way that i have not thought about? You don't want lots of variables, you just want one variable with a
strutured value.

eg have errors.xml that looks like

<errors>
<error name="Banner" number="1">
errormessage 1 for banner
</error>

<error name="Banner" number="2">
errormessage 2 for banner
</error>
</errors>
Then you can do
<xsl:variable name="errors"
select="document('errors.xml')/errors/error"/>

<xsl:value-of select="$errors[@name=current()/plugin][@number=current()/nr]"/>
....

I hope you can se the idea.

Frederik Sørensen


David



Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.