title: XQuery Update: insert or replace depending if node exists not possible?
body: <p>I am trying to build simple XML database (inside BaseX or eXist-db), but I have trouble figuring how to modify values in document :</p>
  
  <p>content is simple as this for test :</p>
  
  <pre><code>&lt;p&gt;
      &lt;pl&gt;
          &lt;id&gt;6&gt;&lt;/id&gt;
      &lt;/pl&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>I am trying to build something like function which would insert element into <code>&lt;pl&gt;</code> if that element is not present or replace it if it is present. But XQuery is giving me troubles yet :</p>
  
  <p>When I try it head-on with if-then-else logic :</p>
  
  <pre><code>if (exists(/p/pl[id=6]/name)=false)
  then insert node &lt;name&gt;thenname&lt;/name&gt; into /p/pl[id=6]
  else replace value  of node /p/pl[id=6]/name with 'elsename'
  </code></pre>
  
  <p>I get error <code>Error: [XUDY0027] Replace target must not be empty</code>. Clearly I am confused, why the else part is evaluated in both cases, thus the error.
  When I empty out the else part :</p>
  
  <pre><code>if (exists(/p/pl[id=6]/name)=true)
      then insert node &lt;name&gt;thenname&lt;/name&gt; into /p/pl[id=6]
      else &lt;dummy/&gt;
  </code></pre>
  
  <p>Then I get <code>Error: [XUST0001] If expression: no updating expression allowed</code>. </p>
  
  <p>When I try through declaring updating function, even then it reports error :</p>
  
  <pre><code>declare namespace testa='test';
  
  declare updating function testa:bid($a, $b)
  {
  if (exists(/p/pl[id=6]/name)=true)
      then insert node &lt;name&gt;thenname&lt;/name&gt; into /p/pl[id=6]
      else &lt;dummy/&gt;
  };
  
  testa:bid(0,0)
  </code></pre>
  
  <p><code>Error: [XUST0001] If expression: no updating expression allowed.</code></p>
  
  <p>I've got these errors from BaseX 6.5.1 package.</p>
  
  <p>So how can I modify values in a simple fashion if possible ?
  If I call <em>insert</em> straight, the there could be multiple elements of same value.
  If I call <em>replace</em> it will fail when the node does not exist.
  If I <em>delete</em> the node before insert/replace then I could destroy sub-nodes which I don't want.</p>
  
  <p>In most SQL databases, these are quite simple task (like MYSQL '<em>replace</em>' command).</p>