473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT indirect variable lookup

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

<xsl:variable name="test_vari able_1" value="'test_va lue_A'"/>
<xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
<xsl:variable name="test_vari able_3" value="'test_va lue_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_va lue_B'"/>
<destination-element value="'test_va lue_C'"/>
<destination-element value="'test_va lue_A'"/>

Essentially, this would consist of a couple steps:

1) Reading the value of the proper source element attribute
2) Programmaticall y 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 3098
* Zachary Turner wrote in comp.text.xml:
>Let's say I have a variable defined as follows:

<xsl:variabl e name="test_vari able_1" value="'test_va lue_A'"/>
<xsl:variabl e name="test_vari able_2" value="'test_va lue_B'"/>
<xsl:variabl e name="test_vari able_3" value="'test_va lue_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_val ue_A' />
<my:item key='2' value='test_val ue_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_va lue_B'"/>
<destination-element value="'test_va lue_C'"/>
<destination-element value="'test_va lue_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****@h oehrmann.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_vari able_1" value="'test_va lue_A'"/>
<xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
<xsl:variable name="test_vari able_3" value="'test_va lue_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_va lue_B'"/>
<destination-element value="'test_va lue_C'"/>
<destination-element value="'test_va lue_A'"/>
<xsl:template match="some-doc-element[@param = '1'">
<destination-element value="{$test_v ariable_1}"/>
</xsl:template>

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

<xsl:template match="some-doc-element[@param = '3'">
<destination-element value="{$test_v ariable_3}"/>
</xsl:template>
That will output e.g.
<destination-element value="test_val ue_B"/>
If you really want
<destination-element value="'test_va lue_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.goo glegroups.com.. .
Let's say I have a variable defined as follows:

<xsl:variable name="test_vari able_1" value="'test_va lue_A'"/>
<xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
<xsl:variable name="test_vari able_3" value="'test_va lue_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_va lue_B'"/>
<destination-element value="'test_va lue_C'"/>
<destination-element value="'test_va lue_A'"/>

Essentially, this would consist of a couple steps:

1) Reading the value of the proper source element attribute
2) Programmaticall y 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
4012
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 HTML for documentation purposes. In particular I want XSLT to transform to HTML so that my XML documents can be "live", doing the transform in the browser. In the new system a very short XML document looks like: <database> <table id="PROVIDER">
3
1641
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 different results. Below I've listed my HTML input, my expected XML output, my XSLT, and the actual output from both Saxon and Xalan. Thanks in advance. HTML Input ---------- <html>
4
4294
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. All that I want is to replace node names with a matching value in the lookup table and place the result into an field attribute pair: Example: id to be renamed instrument_id thus <id type="master">asset #132</id> becomes <field...
4
3243
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 name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
5
7627
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 business components that can be consumed by WebForms, WinForms, mobile devices, etc? Is it even fair to compare the such technologies? - How about for cases when you need to display dynamic elements on the form/grid (as compared to knowing data elements...
2
1457
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 regarding what parameters the XSLT file is expecting. Then my program can retrieve the information from the style sheet and prompt the user for the necessary parameters. I'm envisioning the section would be something like this: <myParameters>
18
2066
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 look up what each feature depends on and gerenate a text file. For example, in the following file, I would like to find out feature A depends on A1 and A2 and feature B depends on B1 and B2. And write that to a text file.
3
9583
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 difference between two source documents and output node-sets which are "different" (changed or new) to new XML files using xsl:result-document To describe the problem I have provided some example data below along with my a portion of my current...
11
3986
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 return anything in the output. I would love to be able to query the xref:factor table to retrieve the value of xref:factor/lookup <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE xsl:stylesheet > <xsl:stylesheet...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.