Hi Graydon,

The db:node-id function returns the the IDs of nodes stored in a database. With the "update" keyword, you are creating a main-memory copy of an XML node (see [1] for more details) which is independent of the original document.

You have already found the solution: You can work with the IDs if you omit the 'update' keyword and change the database contents directly. By the way, here is some equivalent 4.0 syntax:

  for key $key value $value in $composedMap
  let $target := db:get-id('ID-DB', $key)
  let $node := attribute { 'id' } { $value }
  return insert node $node into $target

Best,
Christian

[1] https://docs.basex.org/main/Updates#main-memory_updates


Von: Graydon Saunders via BaseX-Talk <basex-talk@mailman.uni-konstanz.de>
Gesendet: Mittwoch, 18. Februar 2026 16:15
An: BaseX <basex-talk@mailman.uni-konstanz.de>
Betreff: [basex-talk] Re: db:node-id() and updating operations
 
So it turns out what works is

for $stable in map:keys($composedMap)
    return insert node attribute {'id'} {$composedMap($stable)?composed} into db:get-id('ID-DB',$stable)

The same pattern does not work in update; I do not understand why the above expression is happening to database nodes and the original attempt does not.

If anyone is able to explain the distinction I am very curious as to whether I would be able to muster enough neurons to comprehend it.

On Tue, Feb 17, 2026, at 18:27, Graydon Saunders via BaseX-Talk wrote:
Hello --

Using db:node-id() is an order of magnitude faster than using generate-id() to relate various nodes to their ancestors when constructing a composite id. This works great.

Now I have a map (the keys are the value returned by db:node-id() for specific elements) and I want to update either a document or the db to have the new, calculated values as id attribute values.

I expected that I could use 

for $thisDoc in db:get('ID-DB')
 ...calculate some stuff...
... $hasIDKey is the map:keys() of the map of calculated values...

  return 
    $thisDoc  update {(//*[db:node-id(.) = $hasIDKey]) 
                 ! (insert node attribute { 'id' } { $composedMap(./db:node-id(.))?composed } into .)}
 
This pattern worked with generated-id() values; with db:node-id() the error I get is:

[db:node] No database node: <publication.../>.

(Publication is the expected document element.)

db:node-id() worked in a function in a library module when $thisDoc was a parameter, so I'm at a bit of a loss; I would have expected that db:node-id() is the stable identifier and could work with updating expressions but perhaps that was optimism.

Is there an appropriate way to relate a db:node-id() value to an element in an updating expression?

Thanks!
Graydon