title: Xquery ( XDMP-ARGTYPE error ) the expected type of a function input is different from the actual type
body: <p>I'm trying to remove stop words from a text in MarkLogic 8 using this function :</p>
  
  <pre><code>declare function rec:remove-stop-words($string, $stop_words)  {  
    (: This is a recursive function. :)  
    if(not(empty($stop_words))) then  
      rec:remove-stop-words(  
        replace($string, $stop_words[1], '', 'i'),  
        (: This passes along the stop words after 
           the one just evaluated. :)  
        $stop_words[position() &gt; 1]  
      )  
    else normalize-space($string)  
  };  
  </code></pre>
  
  <p>Here where I call it </p>
  
  <pre><code>for $r in /rec:Record
  return
    rec:remove-stop-words(data($r/rec:Abstract), $stop_words}
  </code></pre>
  
  <p>It gives me the following error</p>
  
  <blockquote>
    <p>XDMP-ARGTYPE: (err:XPTY0004)     fn:replace((xs:untypedAtomic("&#10;&#9;&#9;&#9;chapter&#10;&#9;&#9;&#9;utilized asymmetry of n..."), xs:untypedAtomic("&#10;&#9;&#9;&#9;book&#10;&#9;&#9;&#9;interrelationship between ...")), "a", "", "i") -- arg1 is not of type xs:string?</p>
  </blockquote>
  
  <p>The function expects a <code>string</code> type but the actual type is <code>untypedAtomic</code>. I don't know what to do!
  NOTE: (( The problem is not in the function because I've tried to use it for a different text and it worked well )).</p>
  
  <p>I tried to  the code by converting <code>untypedAtomic</code> to <code>string</code> by:</p>
  
  <pre><code>return
    &lt;info&gt;{rec:remove-stop-words(data(xs:string($r/rec:Abstract)), $stop_words)}&lt;/info&gt;
  </code></pre>
  
  <p>but I got this error:</p>
  
  <blockquote>
    <p>XDMP-ARGTYPE: (err:XPTY0004)   fn:replace(("&#10;&#9;&#9;&#9;chapter&#10;&#9;&#9;&#9;utilized asymmetry of n...", "&#10;&#9;&#9;&#9;book&#10;&#9;&#9;&#9;interrelationship between ..."), "a", "", "i") -- arg1 is not of type xs:string</p>
  </blockquote>