RE: [xsl] Reprocess template results in same stylesheet?

Subject: RE: [xsl] Reprocess template results in same stylesheet?
From: "Andrew Welch" <AWelch@xxxxxxxxxxxxxxx>
Date: Mon, 5 Jan 2004 09:53:38 -0000
> Dear XSL experts,
> 
> There is something I find myself wishing I could do in XSLT, but I'm 
> too inexperienced to know if whether it's even sensible.  I 
> wish there 
> were a way to apply  a template, where instead of the 
> processor writing 
> the template result to the output stream, it would push the 
> result tree 
> back onto the input stream where it would be available for continued 
> processing by the same stylesheet.
> 
> My feeling is that this would offer several benefits:
> 
> 1) Enabling data-driven "pull"-style processing that feels more like 
> document-driven "push"-style processing;
> 2) Reducing the number of separate stylesheets (and the number of 
> transformations in a Cocoon pipeline) required to achieve certain 
> things.
> 
> However, this is all on a pretty intuitive level for me... I don't 
> think I can yet converse intelligently enough about XSLT to really 
> articulate why I think it, nor do I have the time to try.  Is there 
> anybody out there who can say, "Yeah, I know exactly what you're 
> talking about, and this would indeed be quite useful"?  Or, "I know 
> just what you mean, but unfortunately it wouldn't work the way you'd 
> expect, and here's why..."?
> 
> If you have no idea what I'm talking about, just don't bother 
> responding, because I've already explained myself as much as I can.  
> That way, if I get zero responses, I will conclude that I was just 
> clueless to begin with, or that I'm getting what I deserve for not 
> bothering to try to explain the idea any better... :-)
> 
> Thanks,
> Mark Lundquist


Hi,

You can do all the transformations in your pipeline in one stylesheet by
performing transformations within variables, with each variable
operating on the previous one.

So, as top-level variables you could have:

<xsl:variable name="firstVar-rtf">
  <xsl:apply-templates/>
</xsl:variable>
<xsl:variable name="firstVar" select="exsl:node-set($firstVar-rtf)"/>

<xsl:variable name="secondVar-rtf">
  <xsl:for-each select="$firstVar">
    <xsl:apply-templates/>
  </xsl:for-each>
</xsl:variable>
<xsl:variable name="secondVar" select="exsl:nodet-set($secondVar-rtf)"/>

Here $firstVar operates on the source xml, and $secondVar works on the
'result' of the apply-templates in $firstVar.

The final link in the chain is of course:

<xsl:template match="/">
  <xsl:for-each select="$lastVar">
    <xsl:apply-templates/>
  </xsl:for-each> 
</xsl:template>

All you need to do is separate out your problem into logical steps and
perform each one in a varaible.

I do this a lot when xslt 1.0 struggles to do a task in one go, such as
finding the average of two percentages written as 45% and 55%.

The first variable would translate() the '%' away, the second variable
would find the average.


Cheers
andrew 

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


Current Thread