[XQuery] Sharing a value between expressions?
Wednesday, 16 January 2013 · 3 messages
JK
Johannes Krampf16 Jan 2013 11:31 #
Hello,
I would like to assign a sequence to a variable and then share this variable between two expressions. Particularly I want to create a list of documents to delete from the db and then return the number of deleted documents.
Code sketch of what I am trying:
let $nodes := [...]
let $base-uris := for $node in $nodes
return base-uri($node)
let $distinct-base-uris := distinct-values($base-uris)
for $base-uri in $distinct-base-uris
return db:delete("db", $base-uri),return count($distinct-base-uris)
---
The problem with this approach is that $distinct-base-uris is local to the first expressions and not in scope for the count call.
I tried to just duplicate all code, but then I get an error message that "no updating expression [is] allowed".
Is there any way to achieve what I want and put my value in a scope accessible by both expressions?
Regards,
Johannes
LW
Leo Wörteler16 Jan 2013 15:44 #
Hi Johannes,
Quoted text · Johannes Krampf · 5 lines
Am 16.01.2013 11:31, schrieb Johannes Krampf: > I would like to assign a sequence to a variable and then share this > variable between two expressions. Particularly I want to create a list > of documents to delete from the db and then return the number of deleted > documents.
[...]
Quoted text · Johannes Krampf · 2 lines
> Is there any way to achieve what I want and put my value in a scope > accessible by both expressions?
try this:
let $nodes := ...,
$base-uris := $nodes/base-uri(),
$distinct-base-uris := distinct-values($base-uris)
return (
for $base-uri in $distinct-base-uris
return db:delete("db", $base-uri),
db:output(count($distinct-base-uris))
)Hope that helps,
cheers, Leo
JK
Johannes Krampf16 Jan 2013 17:39 #
Hi Leo,
this worked like a charm, thank you! (And is also documented in the wiki, shame on me for not seeing this.)
- Johannes
Quoted text · Leo Wörteler · 26 lines
On 01/16/2013 03:44 PM, Leo Wörteler wrote: > Hi Johannes, > > Am 16.01.2013 11:31, schrieb Johannes Krampf: >> I would like to assign a sequence to a variable and then share this >> variable between two expressions. Particularly I want to create a list >> of documents to delete from the db and then return the number of deleted >> documents. > [...] >> Is there any way to achieve what I want and put my value in a scope >> accessible by both expressions? > > try this: > > let $nodes := ..., > $base-uris := $nodes/base-uri(), > $distinct-base-uris := distinct-values($base-uris) > return ( > for $base-uri in $distinct-base-uris > return db:delete("db", $base-uri), > db:output(count($distinct-base-uris)) > ) > > Hope that helps, > cheers, Leo >
Written for BaseX 7.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery