[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] parameter getting lost in tunnel?


Subject: RE: [xsl] parameter getting lost in tunnel?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sun, 20 Jul 2008 20:26:22 +0100

Within your <xsl:template match="sect_break">, the <xsl:apply-templates>
instruction does nothing, because sect_break has no children. So there's
nothing here that will change prev_depth from its initial value.

You will find code for solving this problem in my 2004 paper

http://www.idealliance.org/proceedings/xml04/papers/111/mhk-paper.html

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Tom Schmitter [mailto:toms@xxxxxxxxxxxxxxxxx] 
> Sent: 20 July 2008 17:43
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] parameter getting lost in tunnel?
> 
> Hi List,
>  
> I've been struggling for more hours than I care to admit on a 
> simple task
> - passing a parameter through a series of templates using 
> tunnel="yes".
> For some reason my parameter seems to be lost along the way 
> and inevitably ends up with the default value (0).
>  
> In the abbreviated "sect_break" template below, the param "prev_depth"
> should start out at 0 then be set to 1 as it's passed through 
> apply-templates the first time. But the next time sect_break 
> is called, prev_depth is 0 (or whatever default is specified 
> in the param's select):
>  
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:fn="http://www.w3.org/2005/xpath-functions">
>     <xsl:output method="xml" version="1.0" encoding="UTF-8" 
> indent="yes"/>
>     <xsl:template match="/">
>             <xsl:apply-templates/>
>     </xsl:template>
>        
>     <xsl:template match="sect_break">
>         <!-- prev_depth is the depth that we were at before 
> encountering this sect_break -->
>         <xsl:param name="prev_depth" select="0" tunnel="yes"/>
>         <!-- depth is the depth indicated in the current 
> sect_break -->
>         <xsl:variable name="depth" select="./@depth" as="xs:integer"/>
>         <xsl:variable name="direction" select="$depth - $prev_depth"
> as="xs:integer"/>
>         <xsl:choose>
>             <xsl:when test="$direction &gt; 0">
>                 <!-- going deeper in depth -->
>                 sect_break depth=<xsl:value-of select="$depth"/>
>                 <xsl:apply-templates>
>                     <xsl:with-param name="prev_depth" select="$depth"
> tunnel="yes" />
>                 </xsl:apply-templates>
>             </xsl:when>
>             <!-- some other cases have been omitted for brevity -->
>         </xsl:choose>
>     </xsl:template>
>    
> </xsl:stylesheet>
>  
> Sample input:
> <doc>
>     <sect_break depth="1"/>
>     <sect_break depth="2"/>
>     <sect_break depth="2"/>
>     <sect_break depth="3"/>
>     <sect_break depth="1"/>
> </doc>
>  
> (fwiw, the objective is to create a nested hierarchy from one 
> that is defined unnested. eg:
>  
> <level 1>
> <level 2>
> <level 2>
> <level 3>
> <level 1>
>  
> becomes:
>  
> <level 1>
>     <level 2>
>     </level>
>     <level 2>
>         <level 3>
>         </level>
>     </level>
> </level>
> <level 1>
> </level>
> )
>  
> Thanks very much for any help!
>  
> --Tom
> (using xmlspy)


Current Thread