Click to See Complete Forum and Search --> : XSLT Param Help


shotsy247
03-09-2010, 08:18 AM
Hi All,

I'm new to XSLT and need some help using parameters (I think).

I am building out a multi-language, multi-product landing page. My thought is that I will be able to add a URL parameter such as:
http:www.mypage.com?lang=uk&prod=ff

I have read that these values should pass to parameters I set up in my XSLT as such:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="lang" select="en" />
<xsl:param name="prod" select="ie" />


Can anyone confirm that this should work? If not how do people pass in dynamic data?

Once I have the parameters from the URL how would I use it to change the template that is used? I have tried a few things like:
<xsl:template match="$lang">

But I get errors.

Thanks in advance for any help.

_t

rnd me
03-11-2010, 07:05 PM
it should work fine, but not as automatic as you might think.
you will have to extract the url params using whatever programming enviroment is running your XSLT (js, asp,etc). You typically have to add each param to the XSLT object using a method like objXSLT.setParameter (name, value), before calling .transform().

once you have the param passed, use ifs to steer your output:
<xsl:if test="$lang= 'en'">

shotsy247
03-12-2010, 07:28 AM
Thanks rnd me,

I get the part about setting the parameter with the programming language, but what is confusing or perhaps disappointing is that I can not use this parameter to help choose the portion of XML I'm interested in without manually writing the value in the XSLT.

From your reply it seems that I would have to write something like:
<xsl:if test="$lang= 'en'">
<xsl:template match="en">
<xsl:apply-templates />
</xsl:template>
</xsl:if>

<xsl:if test="$lang= 'sp'">
<xsl:template match="sp">
<xsl:apply-templates />
</xsl:template>
</xsl:if>

<xsl:if test="$lang= 'uk'">
<xsl:template match="uk">
<xsl:apply-templates />
</xsl:template>
</xsl:if>

If that's how it needs to be done then I guess that's how it needs to be done, but it would seem more efficient if I could just add the parameter as a value for the match and only write the <xsl:template match> and other code once.

Thanks again for your help in understanding this.

_t

rnd me
03-12-2010, 04:01 PM
it depends on your data source actually.
i don't know what you're working on, but i do stuff like this.

let's say you have some tags that had language coed as attribs in your xml data.
<root><data lang="en" /> ...


examples:

<xsl:param name="lang" select="en" />

<xsl:template match=" /root/data[@lang=$lang] ">
or, hard-coded:

<xsl:template match=" /root/data[@lang='en'] ">