Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XSLT "associative" arrays (http://www.velocityreviews.com/forums/t166899-xslt-associative-arrays.html)

Audun Rĝe 04-14-2004 08:02 AM

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

Philippe Poulard 04-14-2004 08:20 AM

Re: XSLT "associative" arrays
 
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 |
-----------------------

Ben Edgington 04-14-2004 11:19 AM

Re: XSLT "associative" arrays
 
Philippe Poulard <Philippe.PoulardNO@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

Ben Edgington 04-14-2004 11:19 AM

Re: XSLT "associative" arrays
 
Philippe Poulard <Philippe.PoulardNO@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

Ben Edgington 04-14-2004 11:21 AM

Re: XSLT "associative" arrays
 
Philippe Poulard <Philippe.PoulardNO@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

Ben Edgington 04-14-2004 11:29 AM

Re: XSLT "associative" arrays
 
Ben Edgington <usenet@edginet.org> writes:

....stuff three times...

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

Ben

Mike Conmackie 04-25-2004 04:09 PM

Re: XSLT "associative" arrays
 

"Ben Edgington" <usenet@edginet.org> wrote in message
news:873c76pzlb.fsf@edginet.org...
> Ben Edgington <usenet@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



Martin Honnen 04-25-2004 04:27 PM

Re: XSLT "associative" arrays
 


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/


Mike Conmackie 04-25-2004 06:06 PM

Re: XSLT "associative" arrays
 
Thanks for the clarification.

Mike



Mike Conmackie 04-25-2004 06:13 PM

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

Mike




All times are GMT. The time now is 11:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.