Generate variable name from node values in xslt

Discussion in 'XML' started by =?ISO-8859-1?Q?Frederik_S=F8rensen?=, Jan 30, 2005.

  1. 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
    =?ISO-8859-1?Q?Frederik_S=F8rensen?=, Jan 30, 2005
    #1
    1. Advertising

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

    Joris Gillis Guest

    Tempore 21:53:53, die Sunday 30 January 2005 AD, hinc in foro {comp.text.xml} scripsit Frederik Sørensen <>:

    > 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-view.cgi?userid=38041)
    Vincit omnia simplicitas
    Keep it simple
    Joris Gillis, Jan 30, 2005
    #2
    1. Advertising

  3. 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
    =?UTF-8?B?RnJlZGVyaWsgU8O4cmVuc2Vu?=, Jan 30, 2005
    #3
  4. =?ISO-8859-1?Q?Frederik_S=F8rensen?=

    anonymous Guest

    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 ---
    anonymous, Jan 30, 2005
    #4
  5. Frederik Sørensen <> 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
    David Carlisle, Jan 30, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,448
  2. Ralf Wahner
    Replies:
    2
    Views:
    2,401
    Ralf Wahner
    Jul 15, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    3
    Views:
    3,830
    A. Bolmarcich
    Oct 20, 2006
  5. njsimha
    Replies:
    0
    Views:
    777
    njsimha
    Sep 16, 2008
Loading...

Share This Page