Is there any way to call XSLT templates inline

1.8k views Asked by At

How to call XSLT templates inline? For instance, instead of :

<xsl:call-template name="myTemplate" >
<xsl:with-param name="param1" select="'val'" />
</xsl:call-template>

Can I use XSLT built-in function-call style, like this:

<xls:value-of select="myTeplate(param1)" />
3

There are 3 answers

5
peter.murray.rust On

The syntax of XSLT is correct in the first example. You could also write

<xsl:call-template name="myTemplate" >
<xsl:with-param name="param1">val</xsl:with-param>
</xsl:call-template>

I am not sure what you are trying to do in the second code snippet (the 'val' is missing and there are two typos (xls, and myTeplate)) but it is not valid XSLT.I n

UPDATE If I now understand your question it was not "is there an alternative syntax for XSLT templates?" but "can I write my own functions in XSLT?".

Yes, you can. Here is a useful introduction. Note that you have to provide your Java code in a library and this may not be easy to distribute (e.g. in a browser). Try http://www.xml.com/pub/a/2003/09/03/trxml.html

1
Mads Hansen On

In XSLT 2.0 you can define your own custom functions using xsl:function

An article on XML.com describing how to write your own functions in XSLT 2.0: http://www.xml.com/pub/a/2003/09/03/trxml.html

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:foo="http://whatever">

  <!-- Compare two strings ignoring case, returning same
       values as compare(). -->
  <xsl:function name="foo:compareCI">
    <xsl:param name="string1"/>
    <xsl:param name="string2"/>
    <xsl:value-of select="compare(upper-case($string1),upper-case($string2))"/>
  </xsl:function>

  <xsl:template match="/">
compareCI red,blue: <xsl:value-of select="foo:compareCI('red','blue')"/>
compareCI red,red: <xsl:value-of select="foo:compareCI('red','red')"/>
compareCI red,Red: <xsl:value-of select="foo:compareCI('red','Red')"/>
compareCI red,Yellow: <xsl:value-of select="foo:compareCI('red','Yellow')"/>
  </xsl:template>

</xsl:stylesheet>
0
Paul Sweatte On

Use a processing-instruction and a matching template that applies the parameters to do this:

<?xml version="1.0" encoding="utf-8"?>
<!-- Self-referencing Stylesheet href -->
<?xml-stylesheet type="text/xsl" href="dyn_template_param.xml"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
            >

<!--HTML5 doctype generator-->
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<!--Macro references-->
<?foo param="hi"?>
<?foo param="bye"?>

<!--Self-referencing template call-->
<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
  <!--HTML content-->
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
      <!--Macro template calls-->
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="processing-instruction('foo')">
  <xsl:param name="arg" select="substring-after(.,'=')"/>
  <xsl:if test="$arg = 'hi'">
    <p>Welcome</p>
  </xsl:if>
  <xsl:if test="$arg = 'bye'">
    <p>Thank You</p>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>

References