[basex-talk] update:apply, Context is undeclared. (Newbie)

2020-02-19 Thread 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:

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


[basex-talk] how to store a result in a database?

2020-02-19 Thread thufir
While I can certainly output the result to a file, and then add to a 
database, how would I actually send the result directly to a database?


xquery version "3.0";


{
for $line in db:open("people.json")//text()
return
  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}
}


Currently I'm running it like so:

thufir@dur:~/flwor/people.json$
thufir@dur:~/flwor/people.json$ basex all.xq > all.xml
thufir@dur:~/flwor/people.json$
thufir@dur:~/flwor/people.json$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
>
> create database people.all
Database 'people.all' created in 234.74 ms.
>
> set parser xml
PARSER: xml
>
> add all.xml
Resource(s) added in 377.43 ms.
>
> exit
Have fun.
thufir@dur:~/flwor/people.json$


but that's a bit cumbersome.




thanks,

Thufir


[basex-talk] how to nest tags using a conditional

2020-02-19 Thread thufir

How can I start a new "record" and then nest tags in that record?


but I'm getting output like:


  
  if (matches($line, "[0-9]"))
  then people
  else people
  
  
  if (matches($line, "[0-9]"))
  then joe
  else joe
  
..

wheras I just want output like:


  joe
  123



the query:

xquery version "3.0";


{
for $line in db:open("foo.txt")//text()
return

  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}

}




I think it's a matter of using the () and {} correctly.  Pardon, yes, 
I'm literally reading a book on this, still trying to understand the syntax.




thanks,

Thufir


Re: [basex-talk] how to store a result in a database?

2020-02-19 Thread Martin Honnen

Am 19.02.2020 um 14:36 schrieb thufir:

While I can certainly output the result to a file, and then add to a
database, how would I actually send the result directly to a database?

xquery version "3.0";


{
for $line in db:open("people.json")//text()
return
  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}
}




See http://docs.basex.org/wiki/Database_Module#db:add, you seem to want

  db:add("people.all",

   
{
for $line in db:open("people.json")//text()
return
  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}
}


)


that is, you simply want to pass your query result as the second
argument to the db:add function.



Currently I'm running it like so:

thufir@dur:~/flwor/people.json$
thufir@dur:~/flwor/people.json$ basex all.xq > all.xml
thufir@dur:~/flwor/people.json$
thufir@dur:~/flwor/people.json$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
>
> create database people.all
Database 'people.all' created in 234.74 ms.
>
> set parser xml
PARSER: xml
>
> add all.xml
Resource(s) added in 377.43 ms.
>
> exit
Have fun.
thufir@dur:~/flwor/people.json$


but that's a bit cumbersome.




thanks,

Thufir





Re: [basex-talk] how to nest tags using a conditional

2020-02-19 Thread Martin Honnen

Am 19.02.2020 um 15:08 schrieb thufir:

How can I start a new "record" and then nest tags in that record?


but I'm getting output like:


  
  if (matches($line, "[0-9]"))
  then people
  else people
  
  
  if (matches($line, "[0-9]"))
  then joe
  else joe
  
..

wheras I just want output like:


  joe
  123



the query:

xquery version "3.0";


{
for $line in db:open("foo.txt")//text()
return
    



Nest any contained expression in further curly braces

  {


  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}



}



}










Re: [basex-talk] update:apply, Context is undeclared. (Newbie)

2020-02-19 Thread Ben Engbers
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)
--
> 
> 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


Re: [basex-talk] how to nest tags using a conditional

2020-02-19 Thread thufir




On 2020-02-19 6:10 a.m., Martin Honnen wrote:

Am 19.02.2020 um 15:08 schrieb thufir:

How can I start a new "record" and then nest tags in that record?


but I'm getting output like:


  
  if (matches($line, "[0-9]"))
  then people
  else people
  
  
  if (matches($line, "[0-9]"))
  then joe
  else joe
  
..

wheras I just want output like:


  joe
  123



the query:

xquery version "3.0";


{
for $line in db:open("foo.txt")//text()
return
    



Nest any contained expression in further curly braces

   {


  if (matches($line, "[0-9]"))
  then {$line}
  else {$line}



}



}






that was quite helpful, thanks.  I'm getting:


joe
  
  
phone1
  
  
phone2
  


and want to only open the new record tab for something like:



joe
phone1
phone2



but get "incomplete if statement" when I try to add open and close 
record tags inside each if statement.


xquery version "3.0";


{
for $line in db:open("foo.txt")//text()
return

{
   if (matches($line, "[0-9]"))
   then {$line}
   else {$line}
}
 
}
 




thanks,

Thufir


[basex-talk] increment a variable only when a conditional is true?

2020-02-19 Thread thufir
How can I increment the x variable only when numerical is false?  (I've 
been reading how xquery isn't iterative...)



current output:


  people
  joe
  phone1
  phone2
  phone3
  sue
  cell4
  home5
  alice
  atrib6
  x7
  y9
  z10


desired output:



  people
  joe
  phone1
  phone2
...


Maybe with a second xquery?  Here's the first:

xquery version "3.0";



{
variable $x:=0;

for $line in db:open("foo.txt")//text()


count $id

returnif (matches($line, "[0-9]"))
 then {$line}
 else numerical="false">{$line}

}




where I'm trying to use attributes because I'm not sure how to 
conditionally nest tags.  But, this is interesting.  Not quite sure on 
syntax to set and then conditionally increment $x, however.



thanks,

Thufir


Re: [basex-talk] increment a variable only when a conditional is true?

2020-02-19 Thread thufir

How do I decrement y?

Pardon, output for a simpler example:











the FLWOR:

xquery version "3.0";
let $y := 99
for $x in (1 to 9)
 let $y := $y - 1
return 


is it not possible to decrement $y without using some external scripting 
function?  That seems odd.



thanks,

Thufir


Re: [basex-talk] increment a variable only when a conditional is true?

2020-02-19 Thread Bridger Dyson-Smith
Hi Thufir -

Maybe something like this will help?

```
xquery version "3.1";

let $y := 99

for $x in (1 to 9)
count $iterator
let $decrease := $y - $iterator
return(
  comment { "iterator = " ||  $iterator },
  comment { "decrease = " || $decrease },
  
)
```

I'm basically ripping Walmsley's book off for this example -- see pages
~135-7 (examples P-6,7). The `count` clause makes this work.

Best,
Bridger

PS Remember, the first step in avoiding a *trap* is knowing of its
existence. :)

On Wed, Feb 19, 2020 at 10:33 AM thufir  wrote:

> How do I decrement y?
>
> Pardon, output for a simpler example:
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> the FLWOR:
>
> xquery version "3.0";
> let $y := 99
> for $x in (1 to 9)
>   let $y := $y - 1
> return 
>
>
> is it not possible to decrement $y without using some external scripting
> function?  That seems odd.
>
>
> thanks,
>
> Thufir
>


Re: [basex-talk] increment a variable only when a conditional is true?

2020-02-19 Thread thufir

Hi Bridger,


Yes, this may very well.  I'll dive into it later today or tomorrow. 
The difficulty for me will be to decrement only if a conditional.  But, 
that may very well suffice.


If so, it will give the data some structure through attributes.

Then, I can use those attributes.


Thanks,

Thufi

On 2020-02-19 7:57 a.m., Bridger Dyson-Smith wrote:

Hi Thufir -

Maybe something like this will help?

```
xquery version "3.1";

let $y := 99

for $x in (1 to 9)
count $iterator
let $decrease := $y - $iterator
return(
   comment { "iterator = " ||  $iterator },
   comment { "decrease = " || $decrease },
   
)
```

I'm basically ripping Walmsley's book off for this example -- see pages 
~135-7 (examples P-6,7). The `count` clause makes this work.


Best,
Bridger

PS Remember, the first step in avoiding a *trap* is knowing of its 
existence. :)


On Wed, Feb 19, 2020 at 10:33 AM thufir > wrote:


How do I decrement y?

Pardon, output for a simpler example:











the FLWOR:

xquery version "3.0";
let $y := 99
for $x in (1 to 9)
   let $y := $y - 1
return 


is it not possible to decrement $y without using some external
scripting
function?  That seems odd.


thanks,

Thufir



[basex-talk] was: increment a variable only when a conditional is true?

2020-02-19 Thread Majewski, Steven Dennis (sdm7g)


> On Feb 19, 2020, at 10:01 AM, thufir  wrote:
> 
> where I'm trying to use attributes because I'm not sure how to conditionally 
> nest tags.  But, this is interesting.  Not quite sure on syntax to set and 
> then conditionally increment $x, however.

Does “conditionally nest tags” mean that you want to make the 
person[@numerical=“true”] elements children of the immediately previous 
person[@numerical=“false”] elements ? 

If that is the case you can use “group by” 


— Steve M. 




smime.p7s
Description: S/MIME cryptographic signature


Re: [basex-talk] was: increment a variable only when a conditional is true?

2020-02-19 Thread thufir

Hi Steve,

yes, it certainly does.  At this point, I'd settle for adding an 
attribute like "recordID", but that's exactly it.  I'll take a closer 
look at "group by" a bit later, thanks for the pointer.  See also:


https://stackoverflow.com/q/60237739/262852


thanks,

Thufir

On 2020-02-19 1:05 p.m., Majewski, Steven Dennis (sdm7g) wrote:



On Feb 19, 2020, at 10:01 AM, thufir > wrote:


where I'm trying to use attributes because I'm not sure how to 
conditionally nest tags.  But, this is interesting.  Not quite sure on 
syntax to set and then conditionally increment $x, however.


Does “conditionally nest tags” mean that you want to make the 
person[@numerical=“true”] elements children of the immediately previous 
person[@numerical=“false”] elements ?


If that is the case you can use “group by”


— Steve M.




Re: [basex-talk] was: increment a variable only when a conditional is true?

2020-02-19 Thread Majewski, Steven Dennis (sdm7g)
The reason I asked was: although XQuery looks more procedural than XSLT, it’s 
still basically a functional and declarative language, and thinking about 
incrementing or decrementing variables is probably the wrong way to think about 
solving your problem in XQuery. 
Variables bind names to values over a particular scope, and they aren’t 
updatable variables that you can increment or decrement. 

If what you want in English is roughly:
  For person element where numerical = true , group by the preceding-sibling 
where numerical = false 

That maps fairly simply into XQuery syntax: 

declare variable $XML := 
 people
 joe
 phone1
 phone2
 phone3
 sue
 cell4
 home5
 alice
 atrib6
 x7
 y9
 z10
 ;

for $P in $XML/person
where $P[@numerical="true"]
let $PREV := $P/preceding-sibling::person[@numerical="false"][1]
group by $PREV
return   { $P } 

Yields:



  phone1
  phone2
  phone3


  cell4
  home5


  atrib6
  x7
  y9
  z10


Once the grouping is working, you can work on tweaking the output format to 
exactly what you want. I tried to work towards something like what I THOUGHT 
you might want. 
I had a bit of trouble understanding that $PREV in the return expression is an 
atomic string value and not a node, and that the other variables in return 
expression are multiple valued ( thus the ‘[1]’s ), but I ended up with this:


 {
for $P in $XML/person
where $P[@numerical="true"]
let $PREV := $P/preceding-sibling::person[@numerical="false"][1]
let $X := count($P[1]/preceding-sibling::person[@numerical="false"])
group by $PREV
return   { for $I in $P return {$I/@id, attribute x {$X[1]-1}, 
> string($I)} }  
} 

Which yields:


  
phone1
phone2
phone3
  
  
cell4
home5
  
  
atrib6
x7
y9
z10
  



Also more functional to think of use count or position expressions instead of 
incrementing or decrementing variables. 

I’m hitting the Walmsley book myself now to try to understand those issues with 
group by / return that were puzzling to me. 


— Steve M. 


> On Feb 19, 2020, at 8:28 PM, thufir  wrote:
> 
> Hi Steve,
> 
> yes, it certainly does.  At this point, I'd settle for adding an attribute 
> like "recordID", but that's exactly it.  I'll take a closer look at "group 
> by" a bit later, thanks for the pointer.  See also:
> 
> https://stackoverflow.com/q/60237739/262852
> 
> 
> thanks,
> 
> Thufir
> 
> On 2020-02-19 1:05 p.m., Majewski, Steven Dennis (sdm7g) wrote:
>>> On Feb 19, 2020, at 10:01 AM, thufir >> > wrote:
>>> 
>>> where I'm trying to use attributes because I'm not sure how to 
>>> conditionally nest tags.  But, this is interesting.  Not quite sure on 
>>> syntax to set and then conditionally increment $x, however.
>> Does “conditionally nest tags” mean that you want to make the 
>> person[@numerical=“true”] elements children of the immediately previous 
>> person[@numerical=“false”] elements ?
>> If that is the case you can use “group by”
>> — Steve M.



smime.p7s
Description: S/MIME cryptographic signature


Re: [basex-talk] was: increment a variable only when a conditional is true?

2020-02-19 Thread thufir

Yes, that's pretty much it:

https://stackoverflow.com/q/60237739/262852


I'm a bit curious what happened to the "people" group, but I'll test and 
adapt.  Did "people" get dropped because it has no following siblings to 
group with?



thanks,

Thufir

On 2020-02-19 6:55 p.m., Majewski, Steven Dennis (sdm7g) wrote:
The reason I asked was: although XQuery looks more procedural than XSLT, 
it’s still basically a functional and declarative language, and thinking 
about incrementing or decrementing variables is probably the wrong way 
to think about solving your problem in XQuery.
Variables bind names to values over a particular scope, and they aren’t 
updatable variables that you can increment or decrement.


If what you want in English is roughly:
   For person element where numerical = true , group by the 
preceding-sibling where numerical = false


That maps fairly simply into XQuery syntax:

declare variable $XML := 
  people
  joe
  phone1
  phone2
  phone3
  sue
  cell4
  home5
  alice
  atrib6
  x7
  y9
  z10
 ;

for $P in $XML/person
where $P[@numerical="true"]
let $PREV := $P/preceding-sibling::person[@numerical="false"][1]
group by $PREV
return   { $P } 

Yields:



   phone1
   phone2
   phone3


   cell4
   home5


   atrib6
   x7
   y9
   z10


Once the grouping is working, you can work on tweaking the output format 
to exactly what you want. I tried to work towards something like what I 
THOUGHT you might want.
I had a bit of trouble understanding that $PREV in the return expression 
is an atomic string value and not a node, and that the other variables 
in return expression are multiple valued ( thus the ‘[1]’s ), but I 
ended up with this:



 {
for $P in $XML/person
where $P[@numerical="true"]
let $PREV := $P/preceding-sibling::person[@numerical="false"][1]
let $X := count($P[1]/preceding-sibling::person[@numerical="false"])
group by $PREV
return 
 { for $I in $P return {$I/@id, attribute x {$X[1]-1}, string($I)} } 
 

} 

Which yields:


   
     phone1
     phone2
     phone3
   
   
     cell4
     home5
   
   
     atrib6
     x7
     y9
     z10
   



Also more functional to think of use count or position expressions 
instead of incrementing or decrementing variables.


I’m hitting the Walmsley book myself now to try to understand those 
issues with group by / return that were puzzling to me.



— Steve M.


On Feb 19, 2020, at 8:28 PM, thufir > wrote:


Hi Steve,

yes, it certainly does.  At this point, I'd settle for adding an 
attribute like "recordID", but that's exactly it.  I'll take a closer 
look at "group by" a bit later, thanks for the pointer.  See also:


https://stackoverflow.com/q/60237739/262852


thanks,

Thufir

On 2020-02-19 1:05 p.m., Majewski, Steven Dennis (sdm7g) wrote:
On Feb 19, 2020, at 10:01 AM, thufir > wrote:


where I'm trying to use attributes because I'm not sure how to 
conditionally nest tags.  But, this is interesting.  Not quite sure 
on syntax to set and then conditionally increment $x, however.
Does “conditionally nest tags” mean that you want to make the 
person[@numerical=“true”] elements children of the immediately 
previous person[@numerical=“false”] elements ?

If that is the case you can use “group by”
— Steve M.