arrays in xslt - are they possible?

Discussion in 'XML' started by sqad, Dec 16, 2007.

  1. sqad

    sqad Guest

    I want to be able to write a static array in my XSLT 2 stylesheet. Can
    someone please give me an example on how to do it and how to do the
    lookup?

    Thanks,
    sqad
     
    sqad, Dec 16, 2007
    #1
    1. Advertising

  2. sqad

    TOUDIdel Guest

    Uzytkownik "sqad" <> napisal w wiadomosci
    news:...
    >I want to be able to write a static array in my XSLT 2 stylesheet. Can
    > someone please give me an example on how to do it and how to do the
    > lookup?


    it isn't good thinking way. xslt isn't programing language - remember of it.
    --
    td
    xmlguru.net
     
    TOUDIdel, Dec 16, 2007
    #2
    1. Advertising

  3. sqad

    Pavel Lepin Guest

    TOUDIdel <> wrote in
    <fk432l$jer$>:
    > Uzytkownik "sqad" <> napisal:
    >> I want to be able to write a static array in my XSLT 2
    >> stylesheet. Can someone please give me an example on how
    >> to do it and how to do the lookup?

    >
    > it isn't good thinking way. xslt isn't programing language
    > - remember of it.


    The hell it isn't. XSLT is a declarative, rule-based
    programming language. It's a DSL, not a general-purpose
    language, but nevertheless it's actually Turing-complete.
    Google 'xslt turing machine'.

    To answer the OP's question, while XSLT doesn't have an
    array data type, you can implement a static lookup table
    this way:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:lt="http://example.org/lookup-table">
    <xsl:param name="key"/>
    <lt:lookup-table>
    <lt:pair key="foo" value="1"/>
    <lt:pair key="bar" value="2"/>
    <lt:pair key="baz" value="3"/>
    </lt:lookup-table>
    <xsl:template match="/">
    <value>
    <xsl:apply-templates
    select=
    "
    document('')
    /xsl:stylesheet/lt:lookup-table
    /lt:pair[@key=$key]/@value
    "/>
    </value>
    </xsl:template>
    </xsl:stylesheet>

    Let's see:

    pavel@debian:~/dev/xslt$ xsltproc --stringparam key foo
    lookup.xsl lookup.xsl
    <?xml version="1.0"?>
    <value xmlns:lt="http://example.org/lookup-table">1</value>
    pavel@debian:~/dev/xslt$ xsltproc --stringparam key bar
    lookup.xsl lookup.xsl
    <?xml version="1.0"?>
    <value xmlns:lt="http://example.org/lookup-table">2</value>
    pavel@debian:~/dev/xslt$ xsltproc --stringparam key baz
    lookup.xsl lookup.xsl
    <?xml version="1.0"?>
    <value xmlns:lt="http://example.org/lookup-table">3</value>
    pavel@debian:~/dev/xslt$

    Alternatively, using keys:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:lt="http://example.org/lookup-table">
    <xsl:param name="key"/>
    <xsl:key name="key" match="lt:pair" use="@key"/>
    <lt:lookup-table>
    <lt:pair key="foo" value="1"/>
    <lt:pair key="bar" value="2"/>
    <lt:pair key="baz" value="3"/>
    </lt:lookup-table>
    <xsl:template match="/">
    <value>
    <xsl:for-each select="document('')">
    <xsl:apply-templates
    select="key('key',$key)/@value"/>
    </xsl:for-each>
    </value>
    </xsl:template>
    </xsl:stylesheet>

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Dec 17, 2007
    #3
    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. Asfand Yar Qazi
    Replies:
    4
    Views:
    419
    Asfand Yar Qazi
    Nov 12, 2004
  2. Martin M.
    Replies:
    4
    Views:
    360
    Simon Brunning
    Dec 15, 2005
  3. Dave Rudolf
    Replies:
    1
    Views:
    311
    Kai-Uwe Bux
    May 17, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,173
    Philipp
    Jan 20, 2009
  5. David A. Black
    Replies:
    2
    Views:
    237
    Tim Hunter
    Aug 19, 2004
Loading...

Share This Page