473,320 Members | 1,876 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,320 software developers and data experts.

XSLT "associative" arrays

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
this:
<Element T="F">.....</Element>

...and I want it transformed into the code's corresponding descriptive
name, like so (F ==> Foo):
<NewElement DN="Foo">.....</NewElement>
All sugestions appreciated!
--
Regards.
Audun
Jul 20 '05 #1
10 2572
Audun Røe wrote:
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
this:
<Element T="F">.....</Element>

..and I want it transformed into the code's corresponding descriptive
name, like so (F ==> Foo):
<NewElement DN="Foo">.....</NewElement>
All sugestions appreciated!
--
Regards.
Audun


hi,

the better way, i think, is to define something like this snippet
stylesheet, and import it in your master stylesheet :

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

<date:month-names>
<date:month short="jan">january</date:month>
<date:month short="feb">february</date:month>
<date:month short="mar">march</date:month>
<date:month short="apr">april</date:month>
<date:month short="may">may</date:month>
<date:month short="jun">june</date:month>
<date:month short="jul">jully</date:month>
<date:month short="aug">august</date:month>
<date:month short="sep">september</date:month>
<date:month short="oct">october</date:month>
<date:month short="nov">november</date:month>
<date:month short="dec">december</date:month>
</date:month-names>

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

</xsl:stylesheet>

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
Jul 20 '05 #2
Philippe Poulard <Ph****************@SPAMsophia.inria.fr> writes:
Audun Røe wrote:
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.


the better way, i think, is to define something like this snippet
stylesheet, and import it in your master stylesheet :

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

<date:month-names>
<date:month short="jan">january</date:month>
<date:month short="feb">february</date:month>
<date:month short="mar">march</date:month>
<date:month short="apr">april</date:month>
<date:month short="may">may</date:month>
<date:month short="jun">june</date:month>
<date:month short="jul">jully</date:month>
<date:month short="aug">august</date:month>
<date:month short="sep">september</date:month>
<date:month short="oct">october</date:month>
<date:month short="nov">november</date:month>
<date:month short="dec">december</date:month>
</date:month-names>

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

</xsl:stylesheet>

Using xsl:key is fast and efficient for this. For example, I
have an "array" of Bible book names and numbers embedded in
a stylesheet:

<arr:books>
<arr:book n="01">Genesis</arr:book>
<arr:book n="02">Exodus</arr:book>
...
<arr:book n="66">Revelation</arr:book>
</arr:books>

<!-- Using keys is an efficient way to access the book data -->
<xsl:key name="book" match="arr:book" use="."/>
I can look up the number of a book with the name $booknam like this:

<!-- change context to this stylesheet -->
<xsl:for-each select="document('')">
<xsl:value-of select="key('book',$booknam)/@n"/>
</xsl:for-each>
Using keys instead of normal XPath searches speeded up this part of
my stylesheet by an order of magnitude.

Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
www.edginet.org
Jul 20 '05 #3
Philippe Poulard <Ph****************@SPAMsophia.inria.fr> writes:
Audun Røe wrote:
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.


the better way, i think, is to define something like this snippet
stylesheet, and import it in your master stylesheet :

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

<date:month-names>
<date:month short="jan">january</date:month>
<date:month short="feb">february</date:month>
<date:month short="mar">march</date:month>
<date:month short="apr">april</date:month>
<date:month short="may">may</date:month>
<date:month short="jun">june</date:month>
<date:month short="jul">jully</date:month>
<date:month short="aug">august</date:month>
<date:month short="sep">september</date:month>
<date:month short="oct">october</date:month>
<date:month short="nov">november</date:month>
<date:month short="dec">december</date:month>
</date:month-names>

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

</xsl:stylesheet>

Using xsl:key is fast and efficient for this. For example, I
have an "array" of Bible book names and numbers embedded in
a stylesheet:

<arr:books>
<arr:book n="01">Genesis</arr:book>
<arr:book n="02">Exodus</arr:book>
...
<arr:book n="66">Revelation</arr:book>
</arr:books>

<!-- Using keys is an efficient way to access the book data -->
<xsl:key name="book" match="arr:book" use="."/>
I can look up the number of a book with the name $booknam like this:

<!-- change context to this stylesheet -->
<xsl:for-each select="document('')">
<xsl:value-of select="key('book',$booknam)/@n"/>
</xsl:for-each>
Using keys instead of normal XPath searches speeded up this part of
my stylesheet by an order of magnitude.

Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
www.edginet.org
Jul 20 '05 #4
Philippe Poulard <Ph****************@SPAMsophia.inria.fr> writes:
Audun Røe wrote:
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.


the better way, i think, is to define something like this snippet
stylesheet, and import it in your master stylesheet :

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

<date:month-names>
<date:month short="jan">january</date:month>
<date:month short="feb">february</date:month>
<date:month short="mar">march</date:month>
<date:month short="apr">april</date:month>
<date:month short="may">may</date:month>
<date:month short="jun">june</date:month>
<date:month short="jul">jully</date:month>
<date:month short="aug">august</date:month>
<date:month short="sep">september</date:month>
<date:month short="oct">october</date:month>
<date:month short="nov">november</date:month>
<date:month short="dec">december</date:month>
</date:month-names>

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

</xsl:stylesheet>

Using xsl:key is fast and efficient for this. For example, I
have an "array" of Bible book names and numbers embedded in
a stylesheet:

<arr:books>
<arr:book n="01">Genesis</arr:book>
<arr:book n="02">Exodus</arr:book>
...
<arr:book n="66">Revelation</arr:book>
</arr:books>

<!-- Using keys is an efficient way to access the book data -->
<xsl:key name="book" match="arr:book" use="."/>
I can look up the number of a book with the name $booknam like this:

<!-- change context to this stylesheet -->
<xsl:for-each select="document('')">
<xsl:value-of select="key('book',$booknam)/@n"/>
</xsl:for-each>
Using keys instead of normal XPath searches speeded up this part of
my stylesheet by an order of magnitude.

Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
www.edginet.org
Jul 20 '05 #5
Ben Edgington <us****@edginet.org> writes:

....stuff three times...

Many apologies for the multiple posts... news-client problems.

Ben
Jul 20 '05 #6

"Ben Edgington" <us****@edginet.org> wrote in message
news:87************@edginet.org...
Ben Edgington <us****@edginet.org> writes:

...stuff three times...

Many apologies for the multiple posts... news-client problems.

Ben


Ben,

The technique that you described would be very useful to me but being an
XSLT neophyte, I have a few questions.

Q1. What is the physical placement of the look-up table in the stylesheet?
I presume after the <xsl:stylesheet> tag and
before the first <xsl:template> tag?

Q2. I am unable to find any references to switching contexts in the XSLT
book that I have (XSLT Programmer's
Reference, 2nd Ed. by Michael Kay). How is this accomplished?

I am using Xalan 1.7.0 should this have any bearing on your answers.
Thanks.

Mike Conmackie
Jul 20 '05 #7


Mike Conmackie wrote:

Q1. What is the physical placement of the look-up table in the stylesheet?
I presume after the <xsl:stylesheet> tag and
before the first <xsl:template> tag?
You should put it somewhere as a child of the document element, it
doesn't matter whether it is before or after or between templates,
although it makes sense to put such data either before or after your
templates just to have a clean structured stylesheet.
Q2. I am unable to find any references to switching contexts in the XSLT
book that I have (XSLT Programmer's
Reference, 2nd Ed. by Michael Kay). How is this accomplished?


I think all Ben is talking about is to make sure you write an XPath
selecting those elements in the stylesheet and not those in the XML
document and as shown
document('')
does that

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #8
Thanks for the clarification.

Mike
Jul 20 '05 #9
My news server has deleted the previous messages in this thread. Would
someone be kind enought to repost Ben's solution? Thanks.

Mike
Jul 20 '05 #10
Greetings,

Please forgive my previous post ... chalk it up to anomolous news reader
behavior.

Mike
Jul 20 '05 #11

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

Similar topics

9
by: Eric Anderson | last post by:
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">...
1
by: Dave | last post by:
Hello all, Why is the term "associative" containter used to describe std::set<>, std::map<>, std::multiset<> and std::multimap<>? I always had the impression that it is related to the fact that...
27
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";
4
by: Kozman | last post by:
I have a problem where I need to use the literal "length" as a subscript in an associative array (I have no control over what is used as a subscript..."length" happens to be one of the uncontrolled...
5
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.