Re: [xsl] Can i assign value to a variable and used it in different template

Subject: Re: [xsl] Can i assign value to a variable and used it in different template
From: "Anton Triest" <anton@xxxxxxxx>
Date: Tue, 21 Sep 2004 09:59:34 +0200
> <xsl:template name="process-style"> 
> <xsl:if test="normalize-space($name)='border' and self::table">
> <xsl:variable name="bordervalue" select='"$value"'/>
> </xsl:if> 
> </xsl:template>

That won't work because a variable's scope is its parent (xsl:if, in this case)
so if you want it to be available in other templates it must be a global variable.
But then, its value will be fixed throughout the stylesheet, so probably that's
not what you want in this case. Always remember, in XSLT you cannot change
the value of a variable once it has been declared...

What you can do, is declaring local $bordervalue variables inside the templates
where you call the "process-style" template, then you can use the value inside
that template and/or pass it on to other templates as a param:

    <xsl:template match="table">
        ...
        <xsl:variable name="bordervalue">
            <xsl:apply-templates select="@style" mode="get-border"/>
        </xsl:variable>
        <xsl:if test="number($bordervalue) &gt; 0">
            <xsl:call-template name="add-border-attributes">
                <xsl:with-param name="value" select="$bordervalue"/>
            </xsl:call-template>
        </xsl:if>
        ...
    </xsl:template>
    <xsl:template match="@style" mode="get-border">
        ... get border value from style attribute ...
    </xsl:template>

Maybe you can even combine processing the style and adding the
attributes into one template:

    <xsl:template match="table">
        ...
        <xsl:apply-templates select="@style" mode="add-border-attributes"/>
        ...
    </xsl:template>
    <xsl:template match="@style" mode="add-border-attributes">
        <xsl:variable name="bordervalue">
            ... get border value ...
        </xsl:variable>
        <xsl:if test="number($bordervalue) &gt; 0">
            <xsl:attribute name="border-style">solid</xsl:attribute>
            ... add attributes ...
        </xsl:if>
    </xsl:template>

Good luck,
Anton


> 
> Actually i am used this template for processing inline style.(above is a
> part of that).
> name variable contains the attribute name present in style attribute.My aim
> is to
> check whether the name is border,then i store it value to some variable.I
> want to use 
> this variable in  some other templates.
> 
> <xsl:template name="tablechildborder">
> <xsl:if test="number($bordervalue) &gt; 0 "> 
> <xsl:attribute name="border-style">solid</xsl:attribute>
> <xsl:attribute name="border-separation">1pt</xsl:attribute>
> 
> <xsl:attribute name="border-left-width">thin</xsl:attribute>
> <xsl:attribute
> name="border-right-width">thin</xsl:attribute>
> <xsl:attribute name="border-width">thin</xsl:attribute>
> </xsl:if>
> </xsl:template>
> 
> <xsl:template name="tableborder">
> <xsl:when test="number($bordervalue) &gt; 0 ">
> <xsl:attribute name="border">
> <xsl:call-template name="createborder">
> <xsl:with-param name="tborder"
> select="$bordervalue"/>
> </xsl:call-template>
> </xsl:attribute>
> </xsl:when>
> </xsl:template>
> 
> I tried this one.But i could not get the $bordervalue inside tableborder and
> tablechildborder template.First time it shown some error.Then i make the
> variable bordervalue
> as global.
> 
> <xsl:stylesheet 
>   version="1.0" 
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>   xmlns:fo="http://www.w3.org/1999/XSL/Format";
>   xmlns:fox="http://xml.apache.org/fop/extensions";>
> 
> <!-- ==============================================
> Global variable declaration
> ==================================================-->
> <xsl:variable name="bordervalue"/>
> 
> // All my template present here
> 
> </xsl:stylesheet>
> 
> 
> How can i solve this problem?Can i assign a value to variable and use it in
> other template.
> Please help me.
> 
> 
> Thanks in advance
> 
> George

Current Thread