473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 20 '05 #1
2 2106

<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="attribut e[.='a']">
...
<xsl:template match="attribut e[.='b']">
...

<xsl:template name="foo">
<xsl:apply-templates select="attribu te"/>
</xsl:template>
David
Jul 20 '05 #2
al********@yaho o.fr (Julien Phalip) wrote in message news:<65******* *************** ****@posting.go ogle.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
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1826
by: Doug Farrell | last post by:
Hi all, I'm trying to build a demonstration website using mod_python and 4Suite to show how we could transform XML documents into HTML with XSLT. Here is a piece of code that causes it to blow up: # import the xml/xslt stuff here from xml.xslt.Processor import Processor # create a xslt processor
3
1700
by: Bill Sneddon | last post by:
Does anyone on this site have an informed guess as to when the standards XSLT 2.0 and XPATH 2.0 will be publish and how long it will take for the common processor will support them?
6
3165
by: Robbie Baldock | last post by:
Hi - I'm a bit of a newbie to the world of XSLTs but am trying to call a Java method on a parameter passed into an XSLT but am having problems. I've stripped the XSLT down to its bare bones: <xsl:stylesheet version="1.0" xmlns:java="http://xml.apache.org/xslt/java"
1
412
by: K.Simon | last post by:
Hello, I'm looking for a possibility to execute a program like a python-script within a xslt-stylesheet. I read sometime ago that there exists a way to do so. Could somebody explain how? I would appreciate any assistance. Kai
7
4118
by: RC | last post by:
First, let me say I couldn't find a group discuss XML/XSLT. So I only choose the closest groups to post this message. Here is part of my *.xsl file <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-javascript-ext="my-ext1" extension-element-prefixes="my-javascript-ext"
3
4825
by: Gordon Moore | last post by:
Hi, I'm new to using xml/xslt and although I can create an xml document using the dataset.WriteXml statement, and I have created an xslt to transform the xml into the output I want, I have to manually add the xsl reference statement to the xml file. i.e. <?xml version="1.0" standalone="yes"?> <?xml:stylesheet type="text/xsl" href="Questions.xsl"?> --> I have to
2
10130
by: Reshma Prabhu | last post by:
hello, I am trying to do an xsl tranformation from an XML file into another xml file. I want the output file to be in MemoryStream so that my dataset can direclty read xml using dataset.ReadXml(memoryStream). But at the time of reading it gives following exception System.Xml.XmlException: The root element is missing. But if i write into a file and then read dataset from that then it works fine.
1
4240
by: geoffblanduk_nospam | last post by:
Using Apache Tomcat 5.0.28 I'm having a problem with the xsl:import command. I have several Tomcat running on the same server (they could be different releases of code so can't share resources). An XSLT script is called via a JSP to convert XML to HTML (<xsl:apply nameXml="routesummary" xsl="/xsl/routesummaryheader.xsl" />). This routesummaryheader.xsl script then needs to call another XSLT
3
3318
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model (and thank goodness for the new grouping functions), but where are the functions which would really have made it useful without taxing the implementers? e.g trigonometry functions for SVG; square root /log function to do simple statistics? hex/ dec...
1
1816
by: shidhincr | last post by:
I have some doubts regarding XML and XSL transfornation using javascript.Hope SomeOne will help me for solving this problem. Actually i Created a javascript for transforming XML and XSL. i made a new class in the javascript and i added the object of that particular class and a namespace into the processor by using the method processor.addObject(object,namespace) . It works fine in Internet Explorer. So that i could call a function of...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.