XSLT "associative" arrays

A

Audun Røe

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!
 
P

Philippe Poulard

Audun said:
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!

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 |
-----------------------
 
B

Ben Edgington

Philippe Poulard said:
Audun said:
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
 
B

Ben Edgington

Philippe Poulard said:
Audun said:
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
 
B

Ben Edgington

Philippe Poulard said:
Audun said:
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
 
B

Ben Edgington

....stuff three times...

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

Ben
 
M

Mike Conmackie

Ben Edgington said:
...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
 
M

Martin Honnen

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
 
M

Mike Conmackie

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

Mike
 
M

Mike Conmackie

Greetings,

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

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top