0
votes
declare variable $testseq as item()* := ();

declare function local:insertseq($target as item()*, $position as xs:integer?, $inserts as item()*)
as item()* (:might be great if we have a keyword to represent nothing:)
{
  fn:insert-before($target, 1, $inserts) (:change the global sequence:)
  () (:simulate returning nothing, empty sequence:)
};

element test
{
  attribute haha {"&"},
  local:insertseq($testseq, 1, ('a', 'b')),
  $testseq
}

I need to collect something into a global sequence while the script running. At the end of the script I release the sequence. The function insertseq must return nothing. It is possible with XQuery? Or are there other tricks to do it?

Error from BaseX:

$ basex test.xqy
Stopped at /Users/jack/Documents/SHK/XSD2OWL/Workspace/xqy/test.xqy, 7/4:
[XPTY0004] Item expected, sequence found: ("a", "b").
1
XQuery is a functional language. Even your use of insert-before is incorrect: it doesn't modify anything, it returns a new sequence. You need to rethink what you're doing in terms of input/output; if you need a sequence at the end of your processing, then that sequence will need to be part of a return value. - Jeroen Mostert
Some implementations have a "scripting" extension where you have mutable variables - BeniBela

1 Answers

4
votes

The answer on the title of your original question would actually be:

declare function local:f() as empty-sequence() {
  ()
};

As you probably want to solve a specific problem, you could think about creating a new question with another title and a corresponding problem description (including a tiny example with the expected input and output).

In functional languages, such as XQuery, variables cannot be reassigned once they have been defined (see Referential Transparency). As a consequence, you’ll need to use recursive functions to repeatedly add values to a sequence. fn:fold-left can be used as well: it feels challenging when being used for the first time, but once you understand what it does, you don’t want to miss is.