Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Problem with XSLT and call-template (http://www.velocityreviews.com/forums/t167644-problem-with-xslt-and-call-template.html)

Julien Phalip 08-11-2004 11:40 PM

Problem with XSLT and call-template
 
Hi everyone!

I am doing an XSL transformation from the following XML file:

<foo>
<attribute>a</attribute>
</foo>


The xslt file is:

<xsl:include href="a.xsl"/> <!-- Defines the template 'a' -->
<xsl:include href="b.xsl"/> <!-- Defines the template 'b' -->
<xsl:include href="c.xsl"/> <!-- Defines the template 'c' -->

<xsl:template name="foo">
<xsl:if test="attribute = 'a'">
<xsl:call-template name="a"/>
</xsl:if>
<xsl:if test="attribute = 'b'">
<xsl:call-template name="b"/>
</xsl:if>
<xsl:if test="attribute = 'c'">
<xsl:call-template name="c"/>
</xsl:if>

etc...

</xsl:template>

But now I am starting to have a lot of templates, and it requires to
add another "if" statement every new template I create.
So I would like to make this XSLT file simpler by doing something
like:

<xsl:template name="foo">
<xsl:call-template name="{./attribute}"/>
</xsl:template>

But this doesn't work. I get the error "Could not find template named:
{./attribute}".

Can anybody help me with that?

Thanks a lot,

Julien

David Carlisle 08-12-2004 09:03 AM

Re: Problem with XSLT and call-template
 

<xsl:template name="a">
....
<xsl:if test="attribute = 'a'">
<xsl:call-template name="a"/>
</xsl:if>
<xsl:if test="attribute = 'b'">
<xsl:call-template name="b"/>
</xsl:if>
...
But now I am starting to have a lot of templates, and it requires to
add another "if" statement every new template I create.

That isn't the way XSLT is intended to be used, Normally you would just
do


<xsl:template match="attribute[.='a']">
...
<xsl:template match="attribute[.='b']">
...



<xsl:template name="foo">
<xsl:apply-templates select="attribute"/>
</xsl:template>


David

Robin Johnson 08-12-2004 10:27 AM

Re: Problem with XSLT and call-template
 
allezlisle@yahoo.fr (Julien Phalip) wrote in message news:<65001b61.0408111540.75cdb94c@posting.google. com>...
[...]
> So I would like to make this XSLT file simpler by doing something
> like:
>
> <xsl:template name="foo">
> <xsl:call-template name="{./attribute}"/>
> </xsl:template>
>
> But this doesn't work. I get the error "Could not find template named:
> {./attribute}".


Right. xsl:call-template/@name doesn't take an expandable attribute
value template, I'm afraid, just a QName, so there's no way to do that
in vanilla XSLT.

Depending on what implementation you're using, though, there may be an
extension to do it. I know Saxon has a saxon:call-template function
which is identical to xsl:call-template except that it lets you do
what you're asking about.

However, wouldn't it be nicer to use apply-templates with a mode, and
multilple templates in that mode with predicates on the match
expression? e.g.

<xsl:template name="foo">
<xsl:apply-templates mode="foo" select="."/>
</xsl:template>

and your subtemplates
<xsl:template mode="foo" match="*[attribute = 'a']">
<!-- ... -->
</xsl:template>

<xsl:template mode="foo" match="*[attribute = 'b']">
<!-- ... -->
</xsl:template>

<!-- and so on -->

Then one template will be chosen depending on the value of @attribute,
and you didn't need to list all the possible values of @attribute in a
big ugly choose, which is what you wanted.

As a rule, xsl:apply-templates good. xsl:call-template bad.

--
Robin Johnson
Lead Developer, enCircle Solutions Ltd.
first initial last name at encircle dot co dot uk


All times are GMT. The time now is 07:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.