473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XSLT indirect variable lookup

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

May 30 '07 #1
4 3092
* 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:bj****@hoehrmann.de · 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/
May 30 '07 #2
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/
May 30 '07 #3
Ixa
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

May 30 '07 #4
Yes, it is possible and it is not a good design.

Cheers,
Dimitre Novatchev.
"Zachary Turner" <di***********@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr 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

Jun 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick Reilly | last post by:
I am trying to implement a better method than I already have to document database schemas with XML, and use XSLT to both generate database DDL statements (CREATE TABLE, etc) and to transform to...
3
by: Mike Whittemore | last post by:
I am trying to convert an HTML table into a list of name-value pairs, one pair per field in the table. I believe my XSLT is correct, but I've tried both Xalan and Saxon, which both fail with...
4
by: Adrian Charteris | last post by:
Hi I'm currently trying to use a lookup table for converting one xml doc to another using a XSLT transformation. Ideally I would like my first xml doc to be converted to my second xml doc below. ...
4
by: Frederik Sørensen | last post by:
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...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
2
by: Richard L Rosenheim | last post by:
Is it possible to include addition tags in a XSLT file, that the XSLT processor will, for all practical purposes, ignore? What I'm looking to do is to include a section to contain information...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
11
by: ianoble | last post by:
I've been trying to piece together various code snippets to create a lookup table inside my xslt without the need for a supplemental xml file. Here is what I have so far. As of now, it does not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.