Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT indirect variable lookup

Reply
Thread Tools

XSLT indirect variable lookup

 
 
Zachary Turner
Guest
Posts: n/a
 
      05-30-2007
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

 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      05-30-2007
* 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 · private.php?do=newpm&u= · 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/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-30-2007
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/
 
Reply With Quote
 
Ixa
Guest
Posts: n/a
 
      05-30-2007
> 3) Retrieve the 'value' parameter of the variable determined in #2.
[...]
> Is something like this even possible?


Yes, but only by an extension.

http://www.exslt.org/dyn/functions/evaluate/
http://saxon.sourceforge.net/saxon7.....html#evaluate

--
Ixa

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      06-01-2007
Yes, it is possible and it is not a good design.

Cheers,
Dimitre Novatchev.


"Zachary Turner" <> wrote in message
news: oups.com...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create object from variable indirect reference? NickC Python 5 11-17-2009 09:26 AM
XSLT question: How to lookup another tag's children in XSLT yinglcs@gmail.com XML 18 10-03-2006 11:22 PM
"indirect" ipsec dt1649651@yahoo.com Cisco 3 05-19-2005 08:17 PM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM
Indirect use of COM George Ter-Saakov ASP .Net 2 08-27-2003 03:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57