Nandha Kumar
Nandha Kumar

Reputation: 126

XSLT: How to declare user defined function

I have a XSLT style sheet for transforming. In this I have to specify user defined function using <xsl:function>. When I am about to do this within this <xsl:stylesheet></xsl:stylesheet> it throws an error.

Here is the function:

<xsl:function name="functx:pad-string-to-length" as="xs:string" 
              xmlns:functx="http://www.functx.com" >
  <xsl:param name="stringToPad" as="xs:string?"/> 
  <xsl:param name="padChar" as="xs:string"/> 
  <xsl:param name="length" as="xs:integer"/> 

  <xsl:sequence select=" 
   substring(
     string-join (
       ($stringToPad, for $i in (1 to $length) return $padChar)
       ,'')
    ,1,$length)
 "/>

</xsl:function>

Upvotes: -3

Views: 4812

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

The function defined in the question is syntactically correct and seems meaningful.

Therefore, the error is in the code that you forgot to show to us -- more specifically how this function is being used with what arguments.

Apart from this, there are obvious refactoring possibilities -- such as replacing the unnecessary string-join() with concat().

Upvotes: 1

Emma Burrows
Emma Burrows

Reputation: 5144

As @John Mitchell said, telling us what the error is might be helpful. However, I see you have the namespace declared on the function - make sure xmlns:functx="http://www.functx.com" is also declared in the <xsl:stylesheet> element at the top of your stylesheet.

Another thing to check, obviously, is that you're entering your arguments correctly in the function call: your first two parameters will need quotes but the third doesn't (eg <xsl:value-of select="functx:pad-string-to-length('boo', '-', 12)"/>).

Upvotes: 0

Related Questions