Re: [xsl] variable scope

Subject: Re: [xsl] variable scope
From: "Greg Fausak" <lgfausak@xxxxxxxxx>
Date: Thu, 21 Aug 2008 23:36:40 -0500
Ken,

You're right, I am too programmer brained for this xsl processing.
Thanks for the pointer!

-g


On Thu, Aug 21, 2008 at 9:25 PM, G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2008-08-21 19:12 -0500, Greg Fausak wrote:
>>
>> I've got a situation where an attribute may or
>> may not be set.  I'm building a postgres ddl from xml source that looks
>> like:
>>
>> ...
>> <views>
>>  <view name="super.av_address" source="super"/>
>>  <view name="brand.av_address" source="brand"/>
>>  <view name="av_address" source="agentviaconsumer" schema="agent" />
>>  <view name="av_address" source="consumer" schema="consumer"/>
>>  <view name="av_address" source="fast_consumer" schema="fast_consumer"/>
>> </views>
>> ...
>>
>> (Warning: I am going to use the term 'schema' in a postgres relational
>> context in
>> this paragraph, not the xml context!).  Sometimes I have a view name which
>> contains the schema in the name attribute, like the first two above.
>> Then sometimes
>> I have the schema defined in the schema attribute, like the last
>> three.  It's easy enough
>> to get the data from the attributes into variables sname and vname like:
>>
>>        <xsl:for-each select="//views/view">
>>                <xsl:choose>
>>                        <xsl:when test="@source = 'super'">
>>                                <xsl:choose>
>>                                        <xsl:when test="@schema">
>>                                                <xsl:variable
>> name="sname" select="@schema"/>
>>                                                <xsl:variable
>> name="vname" select="@name"/>
>>                                                <xsl:variable
>> name="tname" select="str:replace($vname,'av_','at_')"/>
>>                                                <xsl:call-template
>> name="create-super-view">
>>
>> <xsl:with-param name="schemaname" select="$sname"/>
>>
>> <xsl:with-param name="viewname" select="$vname"/>
>>
>> <xsl:with-param name="tablename" select="$tname"/>
>>                                                </xsl:call-template>
>
> The above can be written much more simply as:
>
> <xsl:call-template name="create-super-view">
>  <xsl:with-param name="schemaname" select="@schema"/>
>  <xsl:with-param name="viewname" select="@name"/>
>  <xsl:with-param name="tablename" select="str:replace($vname,'av_','at_')"/>
> </xsl:call-template>
>
> ... because there is no obligation to use variables.
>
>> However, it would be so much more readable if I could do the variable
>> assignment one time, something like:
>
> Then you will have to turn the assignment inside out, because the scope of
> local variables is the following siblings and their descendants.  By
> "turning it inside out", I'm saying that you have to put the choosing on the
> value assigned, rather than on the assignment itself:
>
> <xsl:variable name="sname">
>  <xsl:choose>
>    <xsl:when....
> </xsl:variable>
> <xsl:variable name="vname">
>  <xsl:choose>
>    <xsl:when...
> </xsl:variable>
>
> .... and then the variables are in scope for later use.
>
> But I think you are taking too much of a "programmer's perspective" on the
> constructs available in XSLT and you probably can get away without any (or
> many) of the variables you've shown in your snippet.
>
> I hope this helps.
>
> . . . . . . . . . Ken
>
>
> --
> Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
> Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
> G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
> Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
> Legal business disclaimers:  http://www.CraneSoftwrights.com/legal
>
>



-- 
Greg Fausak
greg@xxxxxxxxxxxx

Current Thread