Subject: Re: xsl:template having both name and match
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Thu, 3 Mar 2005 06:04:22 -0800 (PST)
|
Thank you David for reply..
The example you have given works.. But I have a
doubt..
For e.g. if XML file is -
<root>
<foo>xxx</foo>
<bar>yyy</bar>
</root>
And stylesheet is -
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="bar" name="bar">
<span><xsl:apply-templates/></span>
</xsl:template>
<xsl:template match="foo">
<div class="foo"><xsl:call-template
name="bar"/></div>
</xsl:template>
</xsl:stylesheet>
When I run the transform with Saxon 8.1 , I get the
output -
<div class="foo"><span>xxx</span></div>
<span>yyy</span>
When context node is <bar> , the 1st template is
invoked and it generates output <span>yyy</span> ,
which is perfectly fine..
But when context node is <foo> , the 2nd template is
invoked.. which further calls <xsl:call-template
name="bar"/> . My doubt is - when <xsl:call-template
name="bar"/> instruction is executed from 2nd
template, how(and is it supposed to according to
spec..) is the context propagated to the 1st
template.. I am surprised why <xsl:apply-templates/>
is generating output xxx, when called from 2nd
template..
Can you please explain the flow of control in template
<xsl:template match="foo"> ?
Regards,
Mukul
--- David Carlisle <davidc@xxxxxxxxx> wrote:
>
> I want to know in which circumstances such a
> template
> definition is useful.. Can somebody please provide
> an
> example where this has real practical use..?
>
>
> I use it sometimes. suppose you have two elements in
> your source
> <foo>xxx</foo> and <bar>xxx</bar>
> and you want foo to generate the same output as bar
> except that
> it has to be surrounded by <div class="foo">
> ...</div>.
>
> One way is to have
>
> <xsl:template match="bar" name="bar">
> <span><xsl:apply-templates/></span>
> </xsl:template>
>
> <xsl:template match="foo">
> <div class="foo"><xsl:call-template
> name="bar"/></div>
> </xsl:template>
>
> Of course, there are other ways to achieve this, but
> still this idiom
> comes in handy sometimes.
>
> David
__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/
|