XSLT indirect variable lookup

Z

Zachary Turner

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
 
B

Bjoern Hoehrmann

* 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' />
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.
 
M

Martin Honnen

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

Dimitre Novatchev

Yes, it is possible and it is not a good design.

Cheers,
Dimitre Novatchev.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top