xsl-list
[Top] [All Lists]

[xsl] running a 'pipeline' as a loop?

2008-10-27 18:00:21
Hello,

I'd like to apply (essentially) the same transformation to an input doc several 
times. In my case, I've got an 'base' doc and several 'update' docs. The update 
docs contain elements that should replace their corresponding elements in the 
base doc. There can be a variable number of update docs. 

The key point is that the output of one update is to become the input to the 
next update.

It seems like the cleanest way to do this would be to initialize a variable 
'$source' with the base doc, and then loop through the update docs, copying the 
$source to a new variable, '$output', making the appropriate substitutions 
along the way. Before the loop iterates to the next update doc, copy $output to 
$source, so the output of one update becomes the input to the next update. 
Conceptually, the algorithm might look like:

$source = base_doc
for each update_doc
   $output = apply-update($source, update_doc)
   $source= $output
next

In xslt:
    <xsl:template match="/">
            <xsl:apply-templates mode="begin" />     
    </xsl:template>
    
    <xsl:template match="service-manual" mode="begin">
        <!-- load all update files for the current doc -->
        <xsl:variable name="updates" 
select="collection($filedir)//service-manual[(_at_)update = 'true']"/>
           <!-- initialize $source with the base document -->
        <xsl:variable name="source"><xsl:copy-of select="."/></xsl:variable>
        <!-- now loop through the update docs, in update order, and create an 
new, updated version in the $output doc -->
        <xsl:for-each select="$updates">
            <xsl:sort select="article[1]/meta/@update" order="ascending"/>
            <xsl:variable name="output">
                <!-- now perform the update by operating on $source -->
                <xsl:apply-templates select="$source">
                    <xsl:with-param name="updates" select="$updates"/>
                </xsl:apply-templates>
            </xsl:variable>
            <!-- copy this output to source, in preparation for applying the 
next update on top of this one -->
            <!-- tried copy-of instead of apply-templates...mode="copy", but it 
did no better... -->
            <xsl:variable name="source"><xsl:apply-templates select="$output" 
mode="copy"/></xsl:variable>
        </xsl:for-each>
        <xsl:copy-of select="$output"/>
    </xsl:template>

After the loop completes, $output should contain the final version of the 
updated doc, but after the loop is exited, $output goes out of scope (I'm 
getting an 'undefined variable' error on the last copy-of select="$output", 
which would be where the final doc is finally outputted). 

Anyone have any ideas about how I might be able to create this kind of 
transform, where the input comes from the output?

Thanks in advance for any help.


      

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>