-2

How do I call an xsl template (<xsl:call-template name="myPage">)?

Where 'myPage' is a dynamic name which will vary based on the xml output of any give page. In other words, with page source as follows:

<html>
<page> pageName  </page>   
</html>

I need to <xsl:call-template name="pageName">.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Head on over to http://stackoverflow.com/questions/7473659/xslcalltemplate-with-name-supplied-by-a-parameter and see that answers your needs... – Tim C Oct 31 '13 at 22:58
  • Also see [How to call named templates based on a variable?](http://stackoverflow.com/q/1233702/290085) – kjhughes Oct 31 '13 at 22:59
  • Do you have some example code? –  Oct 31 '13 at 23:43

1 Answers1

1

The dynamic despatch mechanism in XSLT is xsl:apply-templates. You could for example have a set of template rules of the form

<xsl:template match="page[. = 'pageName']"/>
<xsl:template match="page[. = 'someOtherName']"/>
etc

and then call

<xsl:apply-templates select="/html/page"/>

You'll probably want to pass the context item as well, which you can do as a parameter using xsl:with-param.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Where do 'pageName' and 'someOtherName' come from? In other words, how do I pull them in from xml. If it helps, I can play around with the xml code, adding id's and the like as needed. Different pages can have their own xml output, what I really need is one single call-template which can call different templates based on the xml. (Like I said, I can do whatever is needed in the xml code itself. – user2943259 Nov 01 '13 at 16:31
  • And as I said, if you want dynamic despatch you should look to apply-templates rather than call-template. – Michael Kay Nov 02 '13 at 16:09