XSL Variables: dynamic access?

Based on the occurence attribute, I would like to select the matching range name. In this case, occurence 1 would be "British Columbia". Is this possible in XSL? If so, could someone enlighten me on the syntax to use?

XML:
<occurence id="1" />
<occurence id="2" />

XSL:
<xsl:param name="language"/>

<xsl:variable name="ranges">
<range id="1">
<name xml:lang="en">British Columbia</name>
<name xml:lang="fr">Colombie-Britannique</name>
</range>
...
</xsl:variable>

<xsl:template match="occurence">
<xsl:value-of select="$ranges/range[id(occurence/@id)]/name lang($language)]" />
</xsl:template>
[773 byte] By [SuperVelcroMan] at [2007-11-19 7:54:37]
# 1 Re: XSL Variables: dynamic access?
Getting closer...

<xsl:template match="occurence">
<xsl:value-of select="msxml:node-set($ranges)/range/name[lang($language)]"/>
</xsl:template>

This will loop through properly but only give me the first entry of the variable. I need to reference the matching IDs...
SuperVelcroMan at 2007-11-10 3:27:40 >
# 2 Re: XSL Variables: dynamic access?
I don't think you can have variables with XML data like that.
Another option is to put the range element into a seperate xml document, and refer to that using the document function.
And just to make things easier I would use a variable for the id attribute.

Your template could then look something like this

<xsl:template match="occurence">
<xsl:variable name="id" select="@id"/>
<xsl:value-of select="document('ranges.xml')/range[@id=$id]/name[@lang=$language]"/>
</xsl:template>
khp at 2007-11-10 3:28:34 >
# 3 Re: XSL Variables: dynamic access?
Perfect! I had to modify it slightly for it to work. Notice the difference when accessing the language variable. For some reason, your version didn't work on my end until I changed it to that.

<xsl:template match="occurence">
<xsl:variable name="id" select="@id"/>
<xsl:value-of select="document('ranges.xml')/range[@id=$id]/name[lang($language)]"/>
</xsl:template>

Now, this is purely a design question. I have a recordset that is created in the database that has some data. This same recordset is linked via linking tables to other foreign key tables. I could replicate those foreign key tables as external XML files and include those like you mention. Or, I could just output the foreign keys used in the XML file instead.

Thoughts on either approaches?
SuperVelcroMan at 2007-11-10 3:29:33 >
# 4 Re: XSL Variables: dynamic access?
Perfect! I had to modify it slightly for it to work. Notice the difference when accessing the language variable. For some reason, your version didn't work on my end until I changed it to that.

Ahh sorry, I made a mistake I should have written @xml:lang instead of @lang, but using the lang() function is probably more flexible.

Now, this is purely a design question. I have a recordset that is created in the database that has some data. This same recordset is linked via linking tables to other foreign key tables. I could replicate those foreign key tables as external XML files and include those like you mention. Or, I could just output the foreign keys used in the XML file instead.

That's really difficult for me to comment on without knowing alot more details of your setup.
khp at 2007-11-10 3:30:41 >
# 5 Re: XSL Variables: dynamic access?
Well...

- Things are stored on SQL Server.
- I have one main table and one foreign key table (4 records)
- This main table feeds into 2 linking tables (2 fields composed primary key)
- Two other tables feed into these linking tables. They have 16, 10 records respectively.

It's the last 2 tables that are causing me grief. Either I put their selected records into the XML (in both languages) or I create an external XML file with all their records.

This is probably not enough is it?
SuperVelcroMan at 2007-11-10 3:31:41 >
# 6 Re: XSL Variables: dynamic access?
This is probably not enough is it?

No, not by a long shot. This is really up to you to decide.
I have no idear how the data is used, what data can be expected to stay the same, what will change, how the data is to be displayed, and a million other things.
I generally don't like giving advice on something unless I'am sure I've got all the relevant facts, which I probably never will in this case.

But look at it this way, having multipe choices is a blessing. There are always multiple solutions to any given problem, when you realize there is more than one solution, you are one step closer to finding the best solution.

OK that's probably enough existenzialism for today.
khp at 2007-11-10 3:32:40 >