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

RE: [xsl] passing parameters with apply-template and document()


Subject: RE: [xsl] passing parameters with apply-template and document()
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 8 Oct 2004 11:56:51 +0100

Looks odd. Which XSLT processor are you using?

When you get something like this it's worth trying more than one. If they
both give the same unexpected answer, you know it's a bug in your code.

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

> -----Original Message-----
> From: Robert McGrath [mailto:rmcgrath@xxxxxxxxxxx] 
> Sent: 08 October 2004 10:32
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] passing parameters with apply-template and document()
> 
> Hi,
> I noticed somthing unusual when passing parameters to a template.
> If I pass  a parameter to a template using <apply-template> where the
> select is a document() it fails but appears to work in every 
> other case.
> I was wondering if this was the expected behaviour and if so could
> sombody explain why.
>  
> I have included examples of what works and what deoesn't below.
>  
> Thanks
> Rob.
>  
> -----
> Examples
> -----
> <xsl:call-template name=TemplateA>
>   <xsl:with-param name="paramA" select="MYVALUE"/>
> </xsl:call-template>
>  
> <xsl:template name="TemplateA">
>   <xsl:param name="paramA"/>
>   TemplateA.paramA=
>   <xsl:value-of select="$paramA"/>
>     <xsl:for-each select="document('docB.xml')">
>       <xsl:call-template name="TemplateB">
>         <xsl:with-param name="paramB" select="$paramA"/>
>       </xsl:call-template>
>    </xsl:for-each>
> </xsl:template>
> <xsl:template name="TemplateB" match="topElement">
>   <xsl:param name="paramB"/>
>   <xsl:value-of select="$paramB"/>
> </xsl:template>
>  
> <--- OUTPUT --->
> TemplateA.paramA=MYVALUE
> TemplateB.paramB=MYVALUE
> <--- OUTPUT --->
> As expected however it should not be necessary to use the for-each tag
> when calling the template 
>  
> <xsl:for-each select="document('docB.xml')">
>   <xsl:call-template name="TemplateB">
>  
> 
> If I try and do this using an apply-templates it fails
>  
> <xsl:call-template name=TemplateA>
>   <xsl:with-param name="paramA" select="MYVALUE"/>
> </xsl:call-template>
>  
> <xsl:template name="TemplateA">
>   <xsl:param name="paramA"/>
>   TemplateA.paramA=
>   <xsl:value-of select="$paramA"/>
>     
>       <xsl:apply-templates select="document('docB.xml')">
>         <xsl:with-param name="paramB" select="$paramA"/>
>       </xsl:call-template>
>    
> </xsl:template>
> <xsl:template name="TemplateB" match="topElement">
>   <xsl:param name="paramB"/>
>   <xsl:value-of select="$paramB"/>
> </xsl:template>
>  
> <--- OUTPUT --->
> TemplateA.paramA=MYVALUE
> TemplateB.paramB=
> <--- OUTPUT --->
>  
> The paramB value is lost.
> topElement is the top level Element in docB.
> TemplateB is the only template which matches on TopElement.
>  
> I have used the second syntax in several cases where the 
> select did not
> contain a document and it worked fine.
> I have also expanded templateB to print wout some of the data from
> topElement and it also worked fine so it appears that my context is
> correct.
> The difficulty only appears if I pass a parameter and change 
> context to
> a different document in the same apply-templates call.


Current Thread
Keywords