Generate variable name from node values in xslt

  • Thread starter =?ISO-8859-1?Q?Frederik_S=F8rensen?=
  • Start date
?

=?ISO-8859-1?Q?Frederik_S=F8rensen?=

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
 
J

Joris Gillis

Tempore 21:53:53 said:
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>

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,
 
G

Guest

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
 
A

anonymous

Frederik said:
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 ---
 
D

David Carlisle

Frederik Sørensen said:
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"/>

I hope you can se the idea.

Frederik Sørensen

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top