XSLT indirect variable lookup

Discussion in 'XML' started by Zachary Turner, May 30, 2007.

  1. Let's say I have a variable defined as follows:

    <xsl:variable name="test_variable_1" value="'test_value_A'"/>
    <xsl:variable name="test_variable_2" value="'test_value_B'"/>
    <xsl:variable name="test_variable_3" value="'test_value_C'"/>

    Then, somewhere else in my source document I have some elements like
    this:

    <some-doc-element param="2"/>
    <some-doc-element param="3"/>
    <some-doc-element param="1"/>

    I want to transform this into the following:

    <destination-element value="'test_value_B'"/>
    <destination-element value="'test_value_C'"/>
    <destination-element value="'test_value_A'"/>

    Essentially, this would consist of a couple steps:

    1) Reading the value of the proper source element attribute
    2) Programmatically create a string that specifies the name of the
    variable to look up
    3) Retrieve the 'value' parameter of the variable determined in #2.
    4) Output the value determined in #3 to the document.

    Step 3 is what I don't know how to do. I have the -name- of a
    variable stored in the value of another variable, and I want to use
    that variable to find the variable with that name.

    Is something like this even possible? I can make a huge if statement
    if necessary, but this seems more elegant, and easier to understand.

    Thanks
     
    Zachary Turner, May 30, 2007
    #1
    1. Advertising

  2. * Zachary Turner wrote in comp.text.xml:
    >Let's say I have a variable defined as follows:
    >
    ><xsl:variable name="test_variable_1" value="'test_value_A'"/>
    ><xsl:variable name="test_variable_2" value="'test_value_B'"/>
    ><xsl:variable name="test_variable_3" value="'test_value_C'"/>


    This is not a good idea. I would recommend to use something like

    <my:map xmlns:my='http://example.org/...'>
    <my:item key='1' value='test_value_A' />
    <my:item key='2' value='test_value_B' />
    ...

    >Then, somewhere else in my source document I have some elements like
    >this:
    >
    ><some-doc-element param="2"/>
    ><some-doc-element param="3"/>
    ><some-doc-element param="1"/>
    >
    >I want to transform this into the following:
    >
    ><destination-element value="'test_value_B'"/>
    ><destination-element value="'test_value_C'"/>
    ><destination-element value="'test_value_A'"/>


    .... then you can simply use something like

    ...
    <xsl:variable name='param' select='@param' />
    <destination-element value="{
    document('')//my:map/my:item[ @key = $param ]/@value
    }"/>
    ...

    The document('') refers to the XSLT document, then it looks up the map
    in it based on the key (the param attribute) and uses the value of the
    value attribute in the map in the output.

    >3) Retrieve the 'value' parameter of the variable determined in #2.


    That is not possible using only XSLT 1.0 features, and poor design.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, May 30, 2007
    #2
    1. Advertising

  3. Zachary Turner wrote:
    > Let's say I have a variable defined as follows:
    >
    > <xsl:variable name="test_variable_1" value="'test_value_A'"/>
    > <xsl:variable name="test_variable_2" value="'test_value_B'"/>
    > <xsl:variable name="test_variable_3" value="'test_value_C'"/>
    >
    > Then, somewhere else in my source document I have some elements like
    > this:
    >
    > <some-doc-element param="2"/>
    > <some-doc-element param="3"/>
    > <some-doc-element param="1"/>
    >
    > I want to transform this into the following:
    >
    > <destination-element value="'test_value_B'"/>
    > <destination-element value="'test_value_C'"/>
    > <destination-element value="'test_value_A'"/>


    <xsl:template match="some-doc-element[@param = '1'">
    <destination-element value="{$test_variable_1}"/>
    </xsl:template>

    <xsl:template match="some-doc-element[@param = '2'">
    <destination-element value="{$test_variable_2}"/>
    </xsl:template>

    <xsl:template match="some-doc-element[@param = '3'">
    <destination-element value="{$test_variable_3}"/>
    </xsl:template>


    That will output e.g.
    <destination-element value="test_value_B"/>
    If you really want
    <destination-element value="'test_value_B'"/>
    then you need

    <xsl:template match="some-doc-element[@param = '1'">
    <destination-element value="'{$test_variable_1}'"/>
    </xsl:template>

    <xsl:template match="some-doc-element[@param = '2'">
    <destination-element value="'{$test_variable_2}'"/>
    </xsl:template>

    <xsl:template match="some-doc-element[@param = '3'">
    <destination-element value="'{$test_variable_3}'"/>
    </xsl:template>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 30, 2007
    #3
  4. Zachary  Turner

    Ixa Guest

    Ixa, May 30, 2007
    #4
  5. Yes, it is possible and it is not a good design.

    Cheers,
    Dimitre Novatchev.


    "Zachary Turner" <> wrote in message
    news:...
    > Let's say I have a variable defined as follows:
    >
    > <xsl:variable name="test_variable_1" value="'test_value_A'"/>
    > <xsl:variable name="test_variable_2" value="'test_value_B'"/>
    > <xsl:variable name="test_variable_3" value="'test_value_C'"/>
    >
    > Then, somewhere else in my source document I have some elements like
    > this:
    >
    > <some-doc-element param="2"/>
    > <some-doc-element param="3"/>
    > <some-doc-element param="1"/>
    >
    > I want to transform this into the following:
    >
    > <destination-element value="'test_value_B'"/>
    > <destination-element value="'test_value_C'"/>
    > <destination-element value="'test_value_A'"/>
    >
    > Essentially, this would consist of a couple steps:
    >
    > 1) Reading the value of the proper source element attribute
    > 2) Programmatically create a string that specifies the name of the
    > variable to look up
    > 3) Retrieve the 'value' parameter of the variable determined in #2.
    > 4) Output the value determined in #3 to the document.
    >
    > Step 3 is what I don't know how to do. I have the -name- of a
    > variable stored in the value of another variable, and I want to use
    > that variable to find the variable with that name.
    >
    > Is something like this even possible? I can make a huge if statement
    > if necessary, but this seems more elegant, and easier to understand.
    >
    > Thanks
    >
     
    Dimitre Novatchev, Jun 1, 2007
    #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. George Ter-Saakov

    Indirect use of COM

    George Ter-Saakov, Aug 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    358
    Natty Gur
    Aug 27, 2003
  2. plaztik via DotNetMonster.com

    Enterprise Library - Indirect/Inherited Configuration??

    plaztik via DotNetMonster.com, May 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    523
    Scott Allen
    May 6, 2005
  3. Yu Lianqing
    Replies:
    1
    Views:
    385
    John Harrison
    Jul 31, 2003
  4. Replies:
    18
    Views:
    2,779
    Joseph Kesselman
    Oct 4, 2006
  5. NickC
    Replies:
    5
    Views:
    371
    jhermann
    Nov 17, 2009
Loading...

Share This Page