XSLT "associative" arrays

Discussion in 'XML' started by Audun Røe, Apr 14, 2004.

  1. Audun Røe

    Audun Røe Guest

    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
    Audun Røe, Apr 14, 2004
    #1
    1. Advertising

  2. 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 |
    -----------------------
    Philippe Poulard, Apr 14, 2004
    #2
    1. Advertising

  3. Philippe Poulard <> 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, Apr 14, 2004
    #3
  4. Philippe Poulard <> 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, Apr 14, 2004
    #4
  5. Philippe Poulard <> 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, Apr 14, 2004
    #5
  6. Ben Edgington <> writes:

    ....stuff three times...

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

    Ben
    Ben Edgington, Apr 14, 2004
    #6
  7. "Ben Edgington" <> wrote in message
    news:...
    > Ben Edgington <> 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
    Mike Conmackie, Apr 25, 2004
    #7
  8. 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/
    Martin Honnen, Apr 25, 2004
    #8
  9. Thanks for the clarification.

    Mike
    Mike Conmackie, Apr 25, 2004
    #9
  10. My news server has deleted the previous messages in this thread. Would
    someone be kind enought to repost Ben's solution? Thanks.

    Mike
    Mike Conmackie, Apr 25, 2004
    #10
  11. Greetings,

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

    Mike
    Mike Conmackie, Apr 26, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    7,407
    Mark Bole
    Mar 4, 2005
  2. Eric Anderson

    XSLT associative arrays

    Eric Anderson, Oct 8, 2004, in forum: XML
    Replies:
    9
    Views:
    20,501
    Eric Anderson
    Oct 12, 2004
  3. lordy
    Replies:
    4
    Views:
    11,476
    Piotr Kobzda
    Jul 20, 2006
  4. desktop
    Replies:
    5
    Views:
    383
    James Kanze
    Jun 26, 2007
  5. shashi
    Replies:
    17
    Views:
    413
    Guest
    Apr 13, 2006
Loading...

Share This Page