473,772 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT associative arrays

Got a quick question trying to figure out the XSLT way of implementing
the functionality of associative arrays. Basically want I want to do is
the following:

<xsl:template name="foo">
<xsl:param name="bar"/>
<xsl:choose>
<xsl:when test="$bar = 'a'">b</xsl:when>
<xsl:when test="$bar = 'c'">d</xsl:when>
<xsl:when test="$bar = 'e'">f</xsl:when>
....
...
..
..
<xsl:choose>
<xsl:template >

Now imagine bar is could be one of a hundred values. Also imagine that
we are not just printing out a simple value. Imagine we have some
complex code that just uses that simple value. We don't want to
continually repeat that complex code all down the page. So what I would
really like is something like:

<xsl:variable name="a" select="'b'"/>
<xsl:variable name="c" select="'d'"/>
<xsl:variable name="e" select="'f'"/>
<xsl:variable name="g" select="'h'"/>
.....
....
...
<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="${$bar} "/>
</xsl:template>

What I am trying to indicate here is that $bar is resolved to 'a', 'c',
'e', 'g' or any other sort of value. Then that value is used as a
variable to resolve to 'b', 'd', 'f', 'h' or any other sort of
associated variable. Now obviously my made up syntax doesn't work. My
question is what is the proper way to do this in XSLT? Basically how do
you create and use an associative array in XSLT? This would all be
equivalent to the following in Perl.

baz{a} = 'b';
baz{c} = 'd';
baz{e} = 'f';
baz{g} = 'h';
.....
....
...

sub foo {
my ( $bar ) = @_;
print $baz{$bar};
}

Any suggestions?

Eric

Jul 20 '05 #1
9 12630
On Fri, 08 Oct 2004 11:59:44 -0400, Eric Anderson <er**@afaik.u s> wrote:
Got a quick question trying to figure out the XSLT way of implementing
the functionality of associative arrays. Basically want I want to do is
the following:

<xsl:variable name="a" select="'b'"/>
<xsl:variable name="c" select="'d'"/>
<xsl:variable name="e" select="'f'"/>
<xsl:variable name="g" select="'h'"/>
....
...
..
<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="${$bar} "/>
</xsl:template>

Hi,

I'd suggest you use xml nodes to store the 'array'.

The following tree could represent the array:

<xsl:variable name="array">
<i ref="a">b</i>
<i ref="c">d</i>
<i ref="e">f</i>
<i ref="g">h</i>
</xsl:variable>

The code to access it would be something like:

<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="$array[@ref=$bar]"/>
</xsl:template>

In stead of using node-tree variables you could also store this array in a
seperate xml file
suppose the file's name is 'array.xml', template foo would be like:

<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="documen t('array.xml')//i[@ref=$bar]"/>
</xsl:template>

regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2
Eric Anderson wrote:
Got a quick question trying to figure out the XSLT way of implementing
the functionality of associative arrays. Basically want I want to do is
the following:

<xsl:template name="foo">
<xsl:param name="bar"/>
<xsl:choose>
<xsl:when test="$bar = 'a'">b</xsl:when>
<xsl:when test="$bar = 'c'">d</xsl:when>
<xsl:when test="$bar = 'e'">f</xsl:when>
....
...
..
..
<xsl:choose>
<xsl:template >

Now imagine bar is could be one of a hundred values. Also imagine that
we are not just printing out a simple value. Imagine we have some
complex code that just uses that simple value. We don't want to
continually repeat that complex code all down the page. So what I would
really like is something like:

<xsl:variable name="a" select="'b'"/>
<xsl:variable name="c" select="'d'"/>
<xsl:variable name="e" select="'f'"/>
<xsl:variable name="g" select="'h'"/>
....
...
..
<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="${$bar} "/>
</xsl:template>

What I am trying to indicate here is that $bar is resolved to 'a', 'c',
'e', 'g' or any other sort of value. Then that value is used as a
variable to resolve to 'b', 'd', 'f', 'h' or any other sort of
associated variable. Now obviously my made up syntax doesn't work. My
question is what is the proper way to do this in XSLT? Basically how do
you create and use an associative array in XSLT? This would all be
equivalent to the following in Perl.

baz{a} = 'b';
baz{c} = 'd';
baz{e} = 'f';
baz{g} = 'h';
....
...
..

sub foo {
my ( $bar ) = @_;
print $baz{$bar};
}

Any suggestions?

Eric


hi,

hereafter is a sample code that should help you ; in this case, the key
is the position (an integer), but you can also easily match the
structure with something else, a string in your case

document('') is used to load the stylesheet itself

you MUST involve a prefix when you define your own structure

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:date="*** Processing dates ***">

<date:month-names>
<date:month short="jan">jan uary</date:month>
<date:month short="feb">feb ruary</date:month>
<date:month short="mar">mar ch</date:month>
<date:month short="apr">apr il</date:month>
<date:month short="may">may </date:month>
<date:month short="jun">jun e</date:month>
<date:month short="jul">jul ly</date:month>
<date:month short="aug">aug ust</date:month>
<date:month short="sep">sep tember</date:month>
<date:month short="oct">oct ober</date:month>
<date:month short="nov">nov ember</date:month>
<date:month short="dec">dec ember</date:month>
</date:month-names>

<xsl:template name="date:mont h-name">
<!--returns the name of the month from its number-->
<xsl:param name="month" select="0"/>
<xsl:value-of
select="documen t('')/*/date:month-names/date:month[$month]"/>
</xsl:template>
</xsl:stylesheet>

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
Jul 20 '05 #3
Joris Gillis wrote:
I'd suggest you use xml nodes to store the 'array'.


What you suggested is exactly what I was looking for. You are an immense
help. Thank you so much.

Eric

Jul 20 '05 #4
Philippe Poulard wrote:
hereafter is a sample code that should help you


What you and Joris Gills sent is exactly what I was looking for. I knew
there had to be a good way to do what I wanted. It is funny your example
deals with calendars. That is actually what this is all related to. Once
again thanks for your help.

Eric

Jul 20 '05 #5

I guess you want an XML document rather than an array.
Given
a file.xml with
<x>
<foo x="a">b</foo>
<foo x="c">d</foo>
....

then your

<xsl:param name="bar">
<xsl:value-of select="${$bar} "/>
is, I believe,

select="documen t('file.xml')/x/foo[@x=$bar]"

David
Jul 20 '05 #6
Joris Gillis wrote:
<xsl:template name="foo">
<xsl:param name="bar">
<xsl:value-of select="$array[@ref=$bar]"/>
</xsl:template>


I am trying a variation on what both you and Philippe Poulard suggested.
Basically what I am trying to do is store some calendar information as
a variable that I can use in some of my XSLT templates. I have created
the following variable:

<xsl:variable name="calendar_ info">
<cal:month name="January">
<cal:abbrev>Jan </cal:abbrev>
<cal:prev>Decem ber</cal:prev>
<cal:next>Febur ary</cal:next>
</cal:month>
<cal:month name="Feburary" >
<cal:abbrev>Feb </cal:abbrev>
<cal:prev>Janua ry</cal:prev>
<cal:next>March </cal:next>
</cal:month>
<cal:month name="March">
<cal:abbrev>Mar </cal:abbrev>
<cal:prev>Febur ary</cal:prev>
<cal:next>April </cal:next>
</cal:month>
<cal:month name="April">
<cal:abbrev>Apr </cal:abbrev>
<cal:prev>March </cal:prev>
<cal:next>May </cal:next>
</cal:month>
<cal:month name="May">
<cal:abbrev>May </cal:abbrev>
<cal:prev>April </cal:prev>
<cal:next>Jun e</cal:next>
</cal:month>
<cal:month name="June">
<cal:abbrev>Jun </cal:abbrev>
<cal:prev>May </cal:prev>
<cal:next>Jul y</cal:next>
</cal:month>
<cal:month name="July">
<cal:abbrev>Jul </cal:abbrev>
<cal:prev>Jun e</cal:prev>
<cal:next>Augus t</cal:next>
</cal:month>
<cal:month name="August">
<cal:abbrev>Aug </cal:abbrev>
<cal:prev>Jul y</cal:prev>
<cal:next>Septe mber</cal:next>
</cal:month>
<cal:month name="September ">
<cal:abbrev>Sep </cal:abbrev>
<cal:prev>Augus t</cal:prev>
<cal:next>Octob er</cal:next>
</cal:month>
<cal:month name="October">
<cal:abbrev>Oct </cal:abbrev>
<cal:prev>Septe mber</cal:prev>
<cal:next>Novem ber</cal:next>
</cal:month>
<cal:month name="November" >
<cal:abbrev>Nov </cal:abbrev>
<cal:prev>Octob er</cal:prev>
<cal:next>Decem ber</cal:next>
</cal:month>
<cal:month name="December" >
<cal:abbrev>Dec </cal:abbrev>
<cal:prev>Novem ber</cal:prev>
<cal:next>Janua ry</cal:next>
</cal:month>
</xsl:variable>

My main goal right now is to find out the next month so that I can
iterate through a list of months. I figured while I was at it I would
put other information in the structure which might be useful for the
future. This variable is defined in a separate xsl file so that many xsl
templates can use the information. I now am trying to use the
information. So if I do

<xsl:value-of select="$calend ar_info/cal:month[@name='January']/cal:abbrev/>

I get an error. I want this to return the abbreviated version of January
('Jan'). If I do

<xsl:value-of select="$calend ar_info"/>

then I get all the content of the variable but how do I select the nodes
in the variable. Any help is greatly appreciated.

Thanks,

Eric

Jul 20 '05 #7
> So if I do

<xsl:value-of
select="$calend ar_info/cal:month[@name='January']/cal:abbrev/>

I get an error. I want this to return the abbreviated version of January
('Jan').


Hi,

To deal with the error:
First, look if the typo in your mail appears in your xsl.
(the select attribute should have an ending quote)
Secondly, make sure you defined the 'cal' namespace in both xsl files.
Finally, make sure you're using xsl 1.1 and a processor that supports it.
(
If your processor does not support version 1.1, you could still use:
<xsl:value-of
select="documen t('templates.xs l')//cal:month[@name='January']/cal:abbrev"/>
)

<xsl:value-of
select="$calend ar_info/cal:month[@name='January']/cal:abbrev"/>
returns 'Jan'. Tested with saxon.
regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #8
Eric Anderson <er**@afaik.u s> writes:
Philippe Poulard wrote:
hereafter is a sample code that should help you


What you and Joris Gills sent is exactly what I was looking for. I
knew there had to be a good way to do what I wanted. It is funny your
example deals with calendars. That is actually what this is all
related to. Once again thanks for your help.


Just a follow-up to the previous excellent suggestions. Experience
has shown me that combining this with the the xsl:key and key()
functionality is an order-of-magnitude faster on my applications. The
simple XPath lookup is neater but no faster than a sequence of if
statements (sequential search). I believe most, if not all, processors
use hashes to implement the key() stuff.

So if performance matters to you have a look into it.

Here's a snippet from a (non calendar-related) example where I use it.

----
<arr:books>
<arr:book n="01" name="Genesis" osisID="Gen"/>
<arr:book n="02" name="Exodus" osisID="Exod"/>
<arr:book n="03" name="Leviticus " osisID="Lev"/>
<!-- snip -->
<arr:book n="66" name="Revelatio n" osisID="Rev"/>
</arr:books>

<!-- Using keys is an efficient way to access the book data -->
<xsl:key name="num2osis" match="arr:book " use="@n"/>
<xsl:key name="esv2osis" match="arr:book " use="@name"/>

<!-- snip -->

<!-- Example of accessing the "array" -->
<xsl:variable name="book-name">
<xsl:for-each select="documen t('')"> <!-- changes the context node -->
<xsl:value-of select="key('nu m2osis',substri ng($esv-id,1,2))/@osisID"/>
</xsl:for-each>
</xsl:variable>

----

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #9
Joris Gillis wrote:
Finally, make sure you're using xsl 1.1 and a processor that supports it.
Ah! This was the problem. I am having the xslt processor in Mozilla
process it instead of processing server-side. Evidently mozilla only
supports 1.0 since 1.1 seems to be just a working draft and seems to
have been dropped in favor of the upcoming 2.0.
(
If your processor does not support version 1.1, you could still use:
<xsl:value-of
select="documen t('templates.xs l')//cal:month[@name='January']/cal:abbrev"/>
)


I ended up doing the document() method and have it working great now.
Thanks for your help.

Eric

Jul 20 '05 #10

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

Similar topics

11
2549
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something like this:
10
2618
by: Audun Røe | last post by:
Hi, I have a list of codes which I want translated into something "understandable". Is there a mechanism such as hashtables that could handle this? (eg. PHP: array('F' => 'Foo', 'C' => 'Cat')) It just seems as if a page worth of if-tests would be a fairly clumsy solution to this (there are a lot of codes) problem. Basically, I have an XML-document which looks like something like
27
11211
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
2019
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference between London and the previous element is 25 (40-15), London has a 25% chance of being selected. When I call the function getAssocItem to do this I need to send in 2 arguments. Is there a quick way to get the maximum key value in the associative
4
2690
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or strings, not just sequential integers in a fixed range. www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html (n.) A collection of data (an array) where individual items can be indexed (accessed) by a string, rather than by...
8
7698
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using this: sType = "String"
7
39848
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
4980
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
11
2963
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new Array(); teams = "Lakers";
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
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
7461
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
6716
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();...
1
4009
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
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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.