This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Re: Re: Assignment no, dynamic scoping si (was: Re: RE: Wishes for XSLrevisions ...


At 6:49 PM 1/2/02, Dimitre Novatchev wrote:
>Gunther Schadow <gunther at aurora dot regenstrief dot org> wrote:
>
>> So, in a world with dynamic scoping / implicit parameters,
>> lowlyweb.org would do this:
>> 
>> <xsl:stylesheet id='urn:lowlyweb.org:onlinebook'>
>> 
>> <xsl:import href='http://bigbucksbooks.com/onlinebook.xsl'/>
>> 
>> <xsl:template match='section'>
>>    ...
>>    <xsl:apply-templates>
>>      <xsl:with-implicit-param name='source' select='@source'/>
>>    </xsl:apply-templates>
>>    ...
>> </xsl:template>
>> 
>> <xsl:template match='paragraph'>
>>    <xsl:implicit-param name='source' select='@source'/>
>>    <p class='$source'>
>>      <xsl:apply-templates/>
>>    </p>
>> </xsl:template>
>> 
>> </xsl:stylesheet>
>> 
>> This template would only touch the section and paragraph elements
>> of bigbucksbooks and would leave everything else untouched.
>> The implicit parameter source is bound to the @source value given
>> in the document for everything nested within that section. Now,
>> most of this stuff doesn't care, until we get down to the paragraph
>> element that now has this implicit parameter available no matter
>> how many things were passed through.
>> 
>> I cannot see how to do that with global variables as you suggest,
>> because that book contains many sections and needs to switch
>> between the different sources all the time per each section.
>> 
>> I can see how one can do the same thing with explicit parameters,
>> which however, requires modifying all of bigbucksbooks templates
>> for table, list (and then: frame, box, float, imagetitle,
>> rotated-text, ...).
>> 
>> So, if you could show me how this can be done without changing the
>> whole thing, I am all ears.
>
>Certainly. This is very simple using XSLT. You even don't have to modify the
>"section" template neither do you need a global variable. In the "paragraph"
>template declare and use the following local variable:
>
><xsl:variable name="source" select="ancestor::section[1]/@source"/>

1. This assumes that you know certain constraints about the document, such as that a section never contains a section. Without  this constraint, a template such as below in the imported document would make your work-around unreliable as ancestor::section[1] might give the wrong element in the paragraph template:

<xsl:template match='section//section'>
   <xsl:apply-templates/>
</xsl:template>

2. The work-around may fail if any xsl:apply-templates instructions in the imported stylesheet selects nodes outside the descendants of the 'section' element. If so, you have no guarantee that ancestor::section[1] will give you the section element that was the current node for a 'section' template earlier in the call chain.

3. If the 'section' template contains two xsl:apply-templates where each pass on a different value for the dynamic variable $source (or another template binds the variable) the 'paragraph' template might be instantiated with different values for $source. The same value might be impossible to calculate from the current context in the 'paragraph' template, e.g.:

<xsl:template match='section'>
    <xsl:apply-templates>
      <xsl:with-implicit-param name='source' select='@source'/>
    </xsl:apply-templates>
    <xsl:apply-templates>
      <xsl:with-implicit-param name='source' select='"default"'/>
    </xsl:apply-templates>
 </xsl:template>  

4. Replicating the same xpath expression in each template that want to use the $source isn't exactly good practive with respect to writing maintainable code. Nor does it facilitate efficient execution. 

In conclusion, dynamic variables provide a general solution to problems that work-arounds only can solve in special cases with great dependency on document constraints and assumptions about the imported stylesheet. 

-- Terje <terje@in-progress.com> | Media Design in*Progress 
   Software for Mac Web Professionals at <http://www.in-progress.com>
   Take Advantage of Server Side XML and XSL with Interaction 3.6!



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]