Variable scope

J

John

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!
 
B

Ben Edgington

John said:
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,
 
J

Johannes Koch

Ben said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top