Tarun Tak
Tarun Tak

Reputation: 421

Need to create a function with argument in XSLT 1.0

Need small help in creating a function with parameter and that can be used in my template. Tried many things but nothing worked out. Below is my sample code :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"
    xmlns:math="http://exslt.org/math"
    xmlns:func="http://exslt.org/functions"
    xmlns:my="http://exslt.org/my"
    exclude-result-prefixes="my"
    extension-element-prefixes="my math func">
    <xsl:output indent="yes" method="xml" />

    
    <xsl:variable name="functionTest11"> 
      <xsl:value-of select="my:calXY('aa')" />
    </xsl:variable>
    
    <xsl:template match="/">

        <svg xmlns="http://www.w3.org/2000/svg">
            <xsl:variable name="functionTest22"> 
                <xsl:value-of select="my:calXY" />
            </xsl:variable>
            <xsl:comment>functionTest <xsl:copy-of
              select="$functionTest22" /></xsl:comment>
        </svg>

    </xsl:template>
    
    <func:function name="my:calXY">
        <xsl:comment>functionHello </xsl:comment>
        <xsl:param name="string1" select="''"/>
        <func:result>
            <xsl:value-of select="concat(10,'|',10)" />
        </func:result>
    </func:function>
</xsl:stylesheet>

Upvotes: 4

Views: 7149

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

I don't have convenient access (that I know of) to Xalan, which appears to be the XSLT processor you are using. So I cannot test any conjectures about the origin of your problem.

But it would surprise me a great deal if putting an output instruction like xsl:comment before the first (and only) xsl:param were correct. (It surprises me that you don't report any error messages from the stylesheet compiler. But it's clear from the error message you quote that Xalan has not successfully compiled the function.) It's not clear what you are trying to accomplish by having your function return both a comment node and the string value '10|10', but this is probably not the way to accomplish it.

On the assumption that the xsl:comment instruction is an attempt to verify that the function has in fact been evaluated, I'd try something like

<func:function name="my:calXY">
    <xsl:param name="string1" select="''"/>
    <func:result select="concat(10,'|',10)" />
</

[In an earlier version of this answer, I speculated that func:result is not needed here; my error. It does appear to be needed.]


You ask for a complete stylesheet. The following stylesheet functions as expected when run with xsltproc. (As I noted above, I don't have a convenient way to run Xalan.)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"
    xmlns:func="http://exslt.org/functions"
    xmlns:my="http://example.org/my"
    exclude-result-prefixes="my"
    extension-element-prefixes="func">
  <xsl:output indent="yes" method="xml" />

  <xsl:template match="/">
    <xsl:message>Result is <xsl:value-of select="my:dummy('foo')"/></xsl:message>
    <output>
      <test><xsl:value-of select="my:dummy('bar')"/></test>
      <test><xsl:value-of select="my:dummy('baz')"/></test>
    </output>    
  </xsl:template>

  <func:function name="my:dummy">
    <xsl:param name="s" select="'foo'"/>
    <func:result select="concat($s,'|',$s)" />
  </func:function>

</xsl:stylesheet>

Upvotes: 5

Related Questions