XRX/Autoincrement File ID

Motivation
You want to create a new file and automatically create a new identifier for the file.

Method
Create an XML file with a single counter in it. Use this counter as part of the file name. After you confirm that the file has been saved, increment the counter using the update function.

Sample Code for save-new.xq
The following example works with an incoming HTTP POST request.

Taking into account concurrent access
Same as above but with locking the node holding the identifier.

The following example works with an incoming HTTP POST request.

Adding and Incrementing an Attribute
Note that the syntax for inserting an attribute is the following:

update insert attribute {$attrName} {$attrValue} into expression

The keyword attribute must be used and be followed with the attribute name and value.

If you want to store a new id in an attribute the syntax is:

update insert attribute {'id'} {$next-id} into $doc/root

Note: if the  already exists in the   then   works as  

And if you are updating the id you can use the replace function with the @ in the path expression:

update replace doc/root/@id with ($next-id + 1)

Back: Regular Expression Builder • Next: Move a Resource