XQuery update: insert or replace depending on whether node exists?

I'm trying to create a simple XML database (inside BaseX or eXist-db), but it's hard for me to figure out how to change the values ​​in the document:

the content is simple like this for a test:

<p>
    <pl>
        <id>6></id>
    </pl>
</p>

I am trying to create something like a function that will insert an element in <pl>if this element is missing or replace it if it is present. But XQuery still doesn't give me problems:

When I try to use it 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 an error Error: [XUDY0027] Replace target must not be empty. It is clear that I am confused why the other part is evaluated in both cases, therefore a mistake. When I release 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 to declare an update function, even then it reports an 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 have these errors from BaseX 6.5.1 package.

, , ? , . , , node . node /, , .

SQL (, MYSQL "replace" ).

+3
1

@Qiqi: @ . if XQuery:

if (exists(/p/pl[id=6]/name))
then insert node <name>thenname</name> into /p/pl[id=6]
else replace value of node /p/pl[id=6]/name with 'elsename'

, XQuery eXist-db eXist-, eXist ( ) 1.4.x 1.5dev :

if (exists(/p/pl[id=6]/name))
then update insert <name>thenname</name> into /p/pl[id=6]
else update value /p/pl[id=6]/name with 'elsename'

XQuery Update, eXist, http://exist-db.org/update_ext.html. , W3C XQuery Update . eXist eXist W3C, , , , , eXist.

, pl id. XML :

<p>
  <pl>
    <id>6</id>
  </pl>
</p>
+5

All Articles