Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Problem with XSLT and call-template

Reply
Thread Tools

Problem with XSLT and call-template

 
 
Julien Phalip
Guest
Posts: n/a
 
      08-11-2004
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
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      08-12-2004

<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
 
Reply With Quote
 
 
 
 
Robin Johnson
Guest
Posts: n/a
 
      08-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Julien Phalip) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments