update:apply, Context is undeclared. (Newbie)

Wednesday, 19 February 2020 · 3 messages · last reply 20 Feb 2020
BE
Ben Engbers19 Feb 2020 12:08 #
Hi, I have a database that contains several thousand records with elements that I will never need so I want to remove them. The following code snippet returns the expected element:
import module namespace functx = 'http://www.functx.com';
let $p := collection("Patterns/nl-verbs.csv")/csv/record[1]
let $remove := ("onbekend1", "onbekend2", "onbekend3", "onbekend4")
return functx:remove-elements($p, $remove)
I tried to use update:apply to update the database but when I execute the following function, I get this message: [XPDY0002] element(functx:remove-elements): Context is undeclared
import module namespace functx = 'http://www.functx.com';
declare %updating function local:clean_verbs(
  $old      as node(),
  $rem      as xs:string*
) as empty-sequence() {
  update:apply(functx:remove-elements, [$old, $rem])
};

let $p := collection("Patterns/nl-verbs.csv")/csv/record[1]
let $remove := ("onbekend1", "onbekend2", "onbekend3", "onbekend4")
return local:clean_verbs($p, $remove) I have two questions: 1: If I want to use the update module, how should I provide the context to the query? 2: How can I update all records without making use of update:apply or update:for-each (what is the befit of the update-module)? Cheers, Ben
BE
Ben Engbers19 Feb 2020 15:23 #
Quoted text · Ben Engbers · 24 lines
Op 19-02-2020 om 12:08 schreef Ben Engbers: > Hi, > > I have a database that contains several thousand records with elements > that I will never need so I want to remove them. > The following code snippet returns the expected element: > > I tried to use update:apply to update the database but when I execute > the following function, I get this message: > [XPDY0002] element(functx:remove-elements): Context is undeclared > --------------- > import module namespace functx = 'http://www.functx.com'; > declare %updating function local:clean_verbs( > $old as node(), > $rem as xs:string* > ) as empty-sequence() { > update:apply(functx:remove-elements, [$old, $rem]) > }; > > let $p := collection("TextMining/nl-verbs.csv")/csv/record[1] > let $remove := ("onbekend1", "onbekend2", "onbekend3", "onbekend4") > > return local:clean_verbs($p, $remove)
----------------------
Quoted text · Ben Engbers · 6 lines
> > I have two questions: > 1: If I want to use the update module, how should I provide the context > to the query? > 2: How can I update all records without making use of update:apply or > update:for-each (what is the befit of the update-module)?
With this code, I managed to replace all records: ---------- import module namespace functx = 'http://www.functx.com';
let $old := collection("TextMining/nl-verbs.csv")/csv/record
let $remove := ("onbekend1", "onbekend2", "onbekend3", "onbekend4")

for $o in $old
  return replace node $o with functx:remove-elements($o, $remove)
-----------
Remains my questions: 1: How can I achieve the same task, using functx:remove-elements and update:for-each? 2: What's the benefit of using the update module? Cheers, Ben
CG
Christian Grün20 Feb 2020 12:37 #
Hi Ben,
Quoted text · Ben Engbers · 2 lines
> 1: How can I achieve the same task, using functx:remove-elements and > update:for-each?
It would look like this: import module namespace functx = 'http://www.functx.com';
  let $names := ('db1', 'db2')
  return update:for-each(
    collection("TextMining/nl-verbs.csv")/csv/record,
    function($record) {
      replace node $record with
        functx:remove-elements($record, $names)
    }
  )
Quoted text · Ben Engbers · 1 line
> 2: What's the benefit of using the update module?
The functions in the Update Module have been designed to perform advanced update operations with higher-order functions in complex applications; see [1] for a first introduction. For most trivial update scenarios, the XQuery Update features [2] will be sufficient and will usually be more readable. Hope this helps Christian [1] http://docs.basex.org/wiki/Higher-Order_Functions [2] http://docs.basex.org/wiki/XQuery_Update

⚡Generated with XQuery