Variable scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Variable scope

    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:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:output 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="$langua ge" /> <== variable 'language' not found
    </xsl:template>

    </xsl:stylesheet>

    Thanks!
  • Ben Edgington

    #2
    Re: Variable scope

    John <john@nospam.co m> writes:
    [color=blue]
    > XSL:
    > ----
    > <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    > <xsl:output 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="$langua ge" /> <== variable 'language' not found
    > </xsl:template>
    >
    > </xsl:stylesheet>[/color]

    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.

    Comment

    • Johannes Koch

      #3
      Re: Variable scope

      Ben Edgington wrote:[color=blue]
      > Try this:
      >
      > <xsl:variable name="language" >
      > <xsl:if test="//language = 'en'">English</xsl:if>
      > <xsl:if test="//language = 'fr'">French</xsl:if>
      > </xsl:variable>[/color]

      or use xsl:choose, xsl:when, xsl:otherwise.
      --
      Johannes Koch
      In te domine speravi; non confundar in aeternum.
      (Te Deum, 4th cent.)

      Comment

      Working...