Variable scope

Discussion in 'XML' started by John, Mar 8, 2005.

  1. John

    John Guest

    I'm new to XSLT and I've been trying to figure out a solution for the
    last couple of days. The XML & XSLT stylesheet are below. Basically I
    want to declare some language (English, French, etc.) variables at the
    beginning of the stylesheet, however I keep getting variable scope errors.

    The language strings are specific to this template. There aren't many
    since most of the data is coming from an application through XML. Below
    is a simplified example. I'm trying to accomodate multiple languages
    without having to maintain multiple stylesheets. Any ideas on how to
    accomplish this?

    XML:
    ----
    <root>
    <language>en</language>
    </root>

    XSL:
    ----
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" encoding="UTF-8" indent="yes" />

    <xsl:if test="//language = 'en'">
    <xsl:variable name="language">English</xsl:variable>
    </xsl:if>

    <xsl:if test="//language = 'fr'">
    <xsl:variable name="language">French</xsl:variable>
    </xsl:if>

    <xsl:template match="/">
    <xsl:value-of select="$language" /> <== variable 'language' not found
    </xsl:template>

    </xsl:stylesheet>

    Thanks!
     
    John, Mar 8, 2005
    #1
    1. Advertising

  2. John <> writes:

    > XSL:
    > ----
    > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="html" encoding="UTF-8" indent="yes" />
    >
    > <xsl:if test="//language = 'en'">
    > <xsl:variable name="language">English</xsl:variable>
    > </xsl:if>
    >
    > <xsl:if test="//language = 'fr'">
    > <xsl:variable name="language">French</xsl:variable>
    > </xsl:if>
    >
    > <xsl:template match="/">
    > <xsl:value-of select="$language" /> <== variable 'language' not found
    > </xsl:template>
    >
    > </xsl:stylesheet>


    Try this:

    <xsl:variable name="language">
    <xsl:if test="//language = 'en'">English</xsl:if>
    <xsl:if test="//language = 'fr'">French</xsl:if>
    </xsl:variable>

    The XSL way of doing things takes a little bit of getting
    your head round, but you'll soon get the hang of it.

    Regards,

    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
     
    Ben Edgington, Mar 8, 2005
    #2
    1. Advertising

  3. Ben Edgington wrote:
    > Try this:
    >
    > <xsl:variable name="language">
    > <xsl:if test="//language = 'en'">English</xsl:if>
    > <xsl:if test="//language = 'fr'">French</xsl:if>
    > </xsl:variable>


    or use xsl:choose, xsl:when, xsl:eek:therwise.
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Mar 8, 2005
    #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. Paul Opal
    Replies:
    12
    Views:
    1,003
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    694
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    518
  4. David Filmer
    Replies:
    19
    Views:
    280
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    216
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page