1

I am trying to re-use a XSL template, and place other templates within this template, multiple times.

Here's an example of my code:

<xsl:template name="wrapper">
    <div>
        <xsl:apply-templates/>
    </div>
</xsl:template>

<xsl:template name="template1"></xsl:template>
<xsl:template name="template2"></xsl:template>

So, now i want to apply both template 1 and template 2 inside template 'wrapper', something like this (I know this isn't the right code, but the idea is there):

<xsl:template name="template1">
    <xsl:template match="wrapper">
    <!--code here-->
    </xsl:template>
</xsl:template>
<xsl:template name="template2">
    <xsl:template match="wrapper">
    <!--code here-->
    </xsl:template>
</xsl:template>

Any help on this would be grealty appreciated.

Rinux
  • 815
  • 1
  • 9
  • 18
  • This is not how it works. What are you trying to do? Please supply some input XML and according desired output. – Tomalak Jan 27 '12 at 08:17
  • I can see the edges of what you're trying to achieve but, as @Tomalak says, unless we have some examples to help us understand we can't really answer in any useful fashion – Murph Jan 27 '12 at 08:20
  • You are aware of `` and the fact you can “nest” those, i.e., invoke call-template inside a named template, yes? – Christopher Creutzig Jan 27 '12 at 13:27
  • 2
    @Rinux, without a sample xml its difficult to come up with a solution (that you are expecting), all we could diagnose with your statements is answered by Hon' Tomalak. Provide an XML, it will be an easy bite :) – Rookie Programmer Aravind Jan 27 '12 at 13:32
  • @DimitreNovatchev, The XSL file was part of a huge (existing) framework, with lots and lots of XML and XSL. At the time of asking it was actually my first encounter with XSL, and I totally misunderstood the proper use of XSL. However, i think your answer came closest to what I asked. Thank you (all) for your time=) – Rinux Sep 19 '12 at 09:13

2 Answers2

8

It is syntactically illegal to nest a template definition into another.

As per the W3C XSLT (both 1.0 and 2.0) specification, an xsl:template must be a child of the top element xsl:stylesheet.

This means that all templates in a stylesheet module must be siblings.

The way to invoke a named template is to use the xsl:call-template instruction like this:

<xsl:call-template name="someTemplateName">
 <!-- Possibly place one or more `xsl:with-param` elements here -->
</xsl:call-template>

However, beaware that:

It is a good style and more in the spirit of XSLT to use unnamed templates (that have a match attribute) and to select the best matching template with an xsl:apply-templates instruction.

Most of the answers to SO XSLT questions demonstrate the use of xsl:apply-templates.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

So, now i want to apply both template 1 and template 2 inside template 'wrapper',

If I take this literally:

<xsl:template name="wrapper">
  <xsl:call-template name="template1" />
  <xsl:call-template name="template2" />
</xsl:template>

But I have a strong gut feeling that you're somehow shooting yourself in the foot here.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • I am doing stuff with the xml within template 1 and 2. However, the wrapper has nothing to do with the xml. The idea is this: ` ` I am very new at XSL, and I am starting to believe that I am using it in the wrong way. However, if something like this would be possible, it would come in very handy. – Rinux Jan 27 '12 at 08:29
  • 5
    @Rinux: You're still not telling me what you are trying to do. This is an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You are trying to do X and think Y might work, so you ask about Y but never tell what X is. Please give me actual XML to work with. – Tomalak Jan 27 '12 at 09:30
  • Allright; you're right. here's my X: I have some content that's being rendered with PHP/XML and XLST. I want this content to be rendered in the 'wrapper', which is a table, containing the images for shadows. Why not use CSS? Internet Explorer. However, I did some extensive search and i think I might have found a solution with pure CSS. And - as I said - I think I was using XLST in the wrong way. Next time I'll think in the X, not in the Y. Thanks for your help! – Rinux Jan 27 '12 at 10:24
  • 2
    @Rinux: You *still* did not give me some actual XML to work with. – Tomalak Jan 27 '12 at 10:33
  • 1
    @Rinux: Check http://stackoverflow.com/q/9022980/254252 or http://stackoverflow.com/q/9022119/254252 for good ways of asking this kind of thing. Also have a (deep) look at http://catb.org/esr/faqs/smart-questions.html - seriously, it does help. – Christopher Creutzig Jan 27 '12 at 12:55
  • Sorry everyone;) I was completely misunderstanding the essential of XLST. Thanks for your help anyway. – Rinux Jan 27 '12 at 16:49
  • 2
    @Rinux This is kinda not satisfying, but there you go. *PS. For the sake of the question (and for the fact that you're an XSLT beginner) I still think you might learn a thing or two if you post your XML.* – Tomalak Jan 27 '12 at 16:54