Greetings, I am trying to build simple XML database (inside baseX or existdb - but baseX seems nicer for java APIs), but I have trouble figuring how to modify values in document :

content is simple as this for test :
<p>
</pl>
 <id>6></id>
</pl>
</p>

I am trying to build something like function which would insert element into <pl> if that element is not present or replace it if it is present. But Xquery is giving me troubles yet :

When I try it head-on with if-then-else logic :
if (exists(/p/pl[id=6]/name)=false)
then insert node <name>thenname</name> into /p/pl[id=6]
else replace value  of node /p/pl[id=6]/name with 'elsename'

I get error Error: [XUDY0027] Replace target must not be empty. - clearly I am confused, why the else part is evaluated in both cases, thus the error. When I empty out the else part :
if (exists(/p/pl[id=6]/name)=true)
    then insert node <name>thenname</name> into /p/pl[id=6]
    else <dummy/>

Then I get Error: [XUST0001] If expression: no updating expression allowed.

When I try through declaring updating function, even then it reports error :
declare namespace testa='test';

declare updating function testa:bid($a, $b)
{
if (exists(/p/pl[id=6]/name)=true)
    then insert node <name>thenname</name> into /p/pl[id=6]
    else <dummy/>
};

testa:bid(0,0)

Error: [XUST0001] If expression: no updating expression allowed.

I've got these errors from baseX 6.5.1 package.

So how can I modify values in a simple fashion if possible ? If I call insert straight, the there could be multiple elements of same value. If I call replace it will fail when the node does not exist. If I delete the node before insert/replace then I could destroy sub-nodes which I don't want.

In most SQL databases, these are quite simple task (like MYSQL 'replace' command).

Thanks in advance for any hints and tips how to get basic 'replace' functionality done.

Regards
Petr Vacek