Search code examples
xsltxslt-2.0

Recursive call to function giving error in XSLT


I am writing a function to split string without breaking word. For that i have return function which is doing recursive call to itself. It's giving me below error

  1. When i am calling function with function prefix then getting error as "splitLine function under namespace http://whatever is not defined.".
  2. When i am calling function without function prefix then getting Parse error in the function.

When i Try to use

<xsl:value-of select="fn:splitLine($inString,$length - 1)"/>

in otherwise condition of function get function not defined error.

When i try to use without function prefix :

<xsl:value-of select="fn:splitLine($inString,$length - 1)"/>

I get parse error in Function.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://whatever">
  <xsl:output omit-xml-declaration="no" /> 
  <xsl:output method="xml" /> 
<xsl:function name="fn:splitLine" as="xs:string">
        <xsl:param name="inString" as="xs:string"/>
        <xsl:param name="length" as="xs:numeric"/>
        <xsl:variable name="delimiters"> ,."!?()</xsl:variable>
        <xsl:choose>
            <xsl:when test="0.0 >= $length ">
                <xsl:value-of select="$inString"/>
            </xsl:when>
            <xsl:when test="$length >= string-length($inString)">
                <xsl:value-of select="$inString"/>
            </xsl:when>
            <xsl:when test="contains($delimiters,substring($inString,$length + 1,1))">
                <xsl:value-of select="substring($inString,1,$length)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="fn:splitLine($inString,$length - 1)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>
<xsl:template match="/">
<xsl:value-of select="fn:splitLine('3 ZHANLANGUAN RD XICHENG, , , BEIJING, , CN, ',35)"/>
  </xsl:template>
</xsl:stylesheet>

I am expecting output to be "3 ZHANLANGUAN RD XICHENG, , ," without breaking word.


Solution

  • @Tim Thanks for the suggestion. Instead of using function , i Used named template and it worked perfectly fine :

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://whatever">
      <xsl:output omit-xml-declaration="no" /> 
      <xsl:output method="xml" />
      <xsl:template name="splitLine">
        <xsl:param name="inString"/>
        <xsl:param name="length"/>
        <xsl:variable name="delimiters"> ,."!?()</xsl:variable>
        <xsl:choose>
            <xsl:when test="0.0 >= $length ">
                <xsl:value-of select="$inString"/>
            </xsl:when>
            <xsl:when test="$length >= string-length($inString)">
                <xsl:value-of select="$inString"/>
            </xsl:when>
            <xsl:when test="contains($delimiters,substring($inString,$length + 1,1))">
                <xsl:value-of select="substring($inString,1,$length)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="splitLine">
                    <xsl:with-param name="inString" select="$inString" />
                    <xsl:with-param name="length" select="$length - 1" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:call-template name="splitLine">
            <xsl:with-param name="inString" select="'fullnameofsample supplier in the block" />
            <xsl:with-param name="length" select="35" />
        </xsl:call-template>
    </xsl:template>
    </xsl:stylesheet>