473,769 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT "associativ e" 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 2616
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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:date="htt p://www.foo.com/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 #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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:date="htt p://www.foo.com/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>

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">Genesi s</arr:book>
<arr:book n="02">Exodus </arr:book>
...
<arr:book n="66">Revelati on</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="documen t('')">
<xsl:value-of select="key('bo ok',$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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:date="htt p://www.foo.com/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>

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">Genesi s</arr:book>
<arr:book n="02">Exodus </arr:book>
...
<arr:book n="66">Revelati on</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="documen t('')">
<xsl:value-of select="key('bo ok',$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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:date="htt p://www.foo.com/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>

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">Genesi s</arr:book>
<arr:book n="02">Exodus </arr:book>
...
<arr:book n="66">Revelati on</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="documen t('')">
<xsl:value-of select="key('bo ok',$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.or g...
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

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

Similar topics

9
12630
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"> <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>
1
1364
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 elements in these containers are sorted. However, the (soon-to-be standard) hash-based containers are also considered associative. In these containers, element order is determined purely by the hash function and the insertion order, not by any...
27
11208
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
2806
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 values). The problem is that if I assign it to something other than an integer, it complains and throws an exception: MyArr = new SomeObject(); I understand the importance of the length property in ordered lists...but it has no usage in...
5
1880
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
10047
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...
1
9995
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
9863
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...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3962
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
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.