title: Error using util:get-fragment-between in eXist-Db
body: <p>I'm trying convert a xml document, organized in divisions and paragraph, with page break and line breaks as milestones into a xml document that wraps pages and lines in page and line elements. </p>
  
  <p>To do this I'm trying to use util:get-fragment-between. </p>
  
  <p>To first get all the lines on a page into a fragment and then turn each line into a fragment. </p>
  
  <p>The first step works, but in the second step, I get the following error <code>org.exist.dom.memtree.ElementImpl cannot be cast to org.exist.dom.persistent.StoredNode</code> which I do not understand. </p>
  
  <p>Below is the xquery file and below that of an excerpt of the xml file I'm trying to convert. </p>
  
  <pre><code>xquery version "3.1";
  
  let $doc := doc($docpath)
  
  (: Build first fragment of containing only lines on page:)
  let $begp-node := $doc//tei:pb[@n="15-v"]
  let $endp-node := $doc//tei:pb[@n="16-r"]
  let $p-fragment := util:get-fragment-between($begp-node, $endp-node, $make-fragment, $display-root-namespace)
  let $p-node := util:parse($p-fragment)
  
  (: so far so good, print out of p-node gives me an xml document with just the text on page 15-v :)
  </code></pre>
  
  <p>(: next step. here I attempt to build a fragment for each line in the newly created page fragment :)</p>
  
  <pre><code>let $lines := $p-node//tei:lb
  
          for $line at $pos in $lines
              let $make-fragment1 := true()
              let $display-root-namespace1 := true()
              let $beginning-node := $line
              let $ending-node := $line/following::tei:lb[1]
              let $fragment := util:get-fragment-between($beginning-node, $ending-node, $make-fragment1, $display-root-namespace1)
  
              let $node := util:parse($fragment)
              return $node
  </code></pre>
  
  <p>I would expect $node to be a new xml document that just contains the line fragment. But instead I get the error:</p>
  
  <blockquote>
    <p>org.exist.dom.memtree.ElementImpl cannot be cast to org.exist.dom.persistent.StoredNode</p>
  </blockquote>
  
  <p>Here is an excerpt of the original document:</p>
  
  <pre><code>&lt;p&gt;
        &lt;lb ed="#L"/&gt;dilectio &lt;choice&gt;
        &lt;orig&gt;dependant&lt;/orig&gt;
        &lt;reg&gt;dependant&lt;/reg&gt;
      &lt;/choice&gt; causaliter a cognitione tamen quaelibet obiecti apprehensio vel cognitio
      &lt;lb ed="#L"/&gt;cum voluntatis libertate sufficit dilectionem causare &lt;g ref="#slash"/&gt; prima
      probatur quia si non sequitur quod dilec
      &lt;lb ed="#L"/&gt;tio
      &lt;lb ed="#L"/&gt;posset poni seu elici naturaliter a voluntate seclusa omni cognitione consequens
      est falsum
      &lt;pb ed="#L" n="15-v"/&gt;
      &lt;lb ed="#L" n="1"/&gt; quia tunc voluntas posset diligere in infinitum contra &lt;ref&gt;
        &lt;name ref="#Augustine"&gt;augustinum&lt;/name&gt; in libro 8 2 10 &lt;title ref="#deTrinitate"&gt;de
          trinitate&lt;/title&gt;
      &lt;/ref&gt; patet consequentia quia positis omnibus causis ad productionem &lt;sic&gt;ad productionem&lt;/sic&gt;
      alicuius effectus re
      &lt;lb ed="#L" n="2"/&gt;quisitis
      &lt;lb ed="#L" n="3"/&gt;omni alio secluso talis effectus posset naturaliter poni in esse &lt;g
        ref="#slash"/&gt;2a pars probatur quia
      &lt;lb ed="#L" n="4"/&gt;quia si sola obiecti cognitio etc sequitur quod stante iudicio vel
      apprehensione alicuius
      &lt;lb ed="#L" n="5"/&gt;obiecti sub ratione &lt;corr&gt;
        &lt;del rend="strikethrough"&gt;boni&lt;/del&gt;
        &lt;add place="inLine"&gt;mali&lt;/add&gt;
      &lt;/corr&gt; seclusa omnia existentia vel apparentia bonitatis
      &lt;lb ed="#L" n="6"/&gt;voluntas posset tale obiectum velle vel diligere consequentia nota sed
      consequens est contra &lt;ref&gt;
        &lt;name ref="#Aristotle"&gt;philosophum&lt;/name&gt;
      &lt;/ref&gt; et &lt;ref&gt;
        &lt;name ref="#Averroes"&gt;commentatorem&lt;/name&gt;
        &lt;lb ed="#L" n="7"/&gt;primo &lt;name ref="#Ethics"&gt;ethicorum&lt;/name&gt;
      &lt;/ref&gt; quia omnia bonum appetunt
    &lt;p xml:id="pgb1q2-d1e3692"&gt;
      &lt;g ref="#pilcrow"/&gt;primum corollarium 
      &lt;lb ed="#L" n="8"/&gt;
  </code></pre>
  
  <p>Any advice is much appreciated.</p>