Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT "associative" arrays

Reply
Thread Tools

XSLT "associative" arrays

 
 
Audun Rĝe
Guest
Posts: n/a
 
      04-14-2004
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
 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      04-14-2004
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"
xmlnssl="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-->
<xslaram name="month" select="0"/>
<xsl:value-of
select="document('')/*/date:month-names/date:month[$month]"/>
</xsl:template>

</xsl:stylesheet>

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
Reply With Quote
 
 
 
 
Ben Edgington
Guest
Posts: n/a
 
      04-14-2004
Philippe Poulard <(E-Mail Removed)> 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"
> xmlnssl="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-->
> <xslaram 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
 
Reply With Quote
 
Ben Edgington
Guest
Posts: n/a
 
      04-14-2004
Philippe Poulard <(E-Mail Removed)> 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"
> xmlnssl="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-->
> <xslaram 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
 
Reply With Quote
 
Ben Edgington
Guest
Posts: n/a
 
      04-14-2004
Philippe Poulard <(E-Mail Removed)> 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"
> xmlnssl="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-->
> <xslaram 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
 
Reply With Quote
 
Ben Edgington
Guest
Posts: n/a
 
      04-14-2004
Ben Edgington <(E-Mail Removed)> writes:

....stuff three times...

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

Ben
 
Reply With Quote
 
Mike Conmackie
Guest
Posts: n/a
 
      04-25-2004

"Ben Edgington" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ben Edgington <(E-Mail Removed)> 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


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      04-25-2004


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/

 
Reply With Quote
 
Mike Conmackie
Guest
Posts: n/a
 
      04-25-2004
Thanks for the clarification.

Mike


 
Reply With Quote
 
Mike Conmackie
Guest
Posts: n/a
 
      04-25-2004
My news server has deleted the previous messages in this thread. Would
someone be kind enought to repost Ben's solution? Thanks.

Mike


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments