Search code examples
xquerymarklogicmarklogic-9

Marklogic undefined function fn:string-pad()


I tried to use fn:string-pad() xquery function it throws error undefined function, using Marklogic version 9

https://docs.marklogic.com/fn:string-pad


Solution

  • fn:string-pad() was only defined in an early draft of XQuery and was later removed. That early (May 2003) draft was supported in the 0.9-ml dialect. If you have XQuery modules in this dialect you should really migrate them to either the strict standards-compliant dialect 1.0 or the extended version 1.0-ml. I would recommend the latter, unless you have a particular reason not to. At some point support for 0.9-ml will be removed. In the meantime you can add an explicit version declaration to the top of your module to use the old syntax (and therefore the availability of this obsolete function): xquery version "0.9-ml"

    With respect to this function in particular: it was removed because it is trivial to write, even with all the error checking:

    declare function local:string-pad($pad-string as xs:string?, $pad-count as xs:integer) as xs:string?
    {
      if (empty($pad-string)) then ()
      else if ($pad-count lt 0) then error((),"Invalid pad count")
      else if ($pad-count eq 0) then ""
      else string-join(for $i in 1 to $pad-count return $pad-string,"")
    };