Command Files - string syntax
Monday, 28 June 2021 · 8 messages · last reply 29 Jun 2021
MH
Quoted text · Jonathan Robie · 26 lines
On 28.06.2021 20:54, Jonathan Robie wrote: > I decided to try creating some command files to simplify administration: > > https://docs.basex.org/wiki/Commands#String_Syntax > <https://docs.basex.org/wiki/Commands#String_Syntax> > > I was surprised that this did not work: > > % cat foo.bsx > > CREATE DB test > > ADD TO embedded.xml <root>embedded</root> > > # run query > > XQUERY <hits>{ count(//text()) }</hits> > > CLOSE > > > % basex foo.bsx > > Stopped at /.../foo.bsx, 1/9: > > [XPST0003] Unexpected end of query: 'DB test...'.
I think the suffix of the file needs to be ".bxs" to be treated as a command sequence, otherwise it is treated as XQuery code. So you have the wrong file suffix.
JR
Jonathan Robie28 Jun 2021 22:54 #
I decided to try creating some command files to simplify administration:
https://docs.basex.org/wiki/Commands#String_Syntax
I was surprised that this did not work:
% cat foo.bsx
CREATE DB testADD TO embedded.xml <root>embedded</root>
# run query
XQUERY <hits>{ count(//text()) }</hits>
CLOSE
% basex foo.bsx
Stopped at /.../foo.bsx, 1/9:
[XPST0003] Unexpected end of query: 'DB test...'.
This works fine:
% basex <foo.bsx
BaseX 9.1 [Standalone]
Try 'help' to get more information.
Quoted text · 1 line
> CREATE DB test
Database 'test' created in 196.04 ms.
Quoted text · 1 line
> ADD TO embedded.xml <root>embedded</root>
Resource(s) added in 101.36 ms.
Quoted text · 2 lines
> # run query > XQUERY <hits>{ count(//text()) }</hits>
<hits>1</hits>Query executed in 393.09 ms.
Quoted text · 1 line
> CLOSE
Database 'test' was closed.
What am I missing?
Jonathan
MH
Not sure, https://docs.basex.org/wiki/Command-Line_Options#Standalone suggests there is a -u command line option
Quoted text · 23:58 · 136 lines
Am 28.06.21, 23:58 schrieb Jonathan Robie <jonathan.robie at gmail.com>: > > > > DOH! Thanks for the help, I seem to be a little dsylexic today ;-> > > > > > > > Now it works, but SET WRITEBACK true doesn't seem to be working: > > > > > > > > > DROP DB oshb-morphology > > CREATE DB oshb-morphology > > ADD ./morphhb/wlc > > SET WRITEBACK true > > XQUERY ./xquery/merge-proper-nouns-oshb.xq > > EXPORT ./out > > > > > > > > This query does an update. If I run the same query in the GUI, it works, but not in this ... um ... bxs file. What do I do to enable updates? > > > > > > > > > > > Jonathan > > > > > > > > > > > > > On Mon, Jun 28, 2021 at 3:57 PM Martin Honnen <martin.honnen at gmx.de> wrote: > > > > > > > On 28.06.2021 20:54, Jonathan Robie wrote: > > > > > I decided to try creating some command files to simplify administration: > > > > > > > > > > https://docs.basex.org/wiki/Commands#String_Syntax > > > > > <https://docs.basex.org/wiki/Commands#String_Syntax> > > > > > > > > > > I was surprised that this did not work: > > > > > > > > > > % cat foo.bsx > > > > > > > > > > CREATE DB test > > > > > > > > > > ADD TO embedded.xml <root>embedded</root> > > > > > > > > > > # run query > > > > > > > > > > XQUERY <hits>{ count(//text()) }</hits> > > > > > > > > > > CLOSE > > > > > > > > > > > > > > > % basex foo.bsx > > > > > > > > > > Stopped at /.../foo.bsx, 1/9: > > > > > > > > > > [XPST0003] Unexpected end of query: 'DB test...'. > > > > > > > > > > > > I think the suffix of the file needs to be ".bxs" to be treated as a > > > > command sequence, otherwise it is treated as XQuery code. So you have > > > > the wrong file suffix. > > > > > > > > > >
JR
DOH! Thanks for the help, I seem to be a little dsylexic today ;->
Now it works, but SET
WRITEBACK true doesn't seem to be working:DROP DB oshb-morphology
CREATE DB oshb-morphologyADD ./morphhb/wlc
SET WRITEBACK trueXQUERY ./xquery/merge-proper-nouns-oshb.xq
EXPORT ./out
This query does an update. If I run the same query in the GUI, it works, but not in this ... um ... bxs file. What do I do to enable updates?
Jonathan
Quoted text · Jonathan Robie · 33 lines
On Mon, Jun 28, 2021 at 3:57 PM Martin Honnen <martin.honnen at gmx.de> wrote: > On 28.06.2021 20:54, Jonathan Robie wrote: > > I decided to try creating some command files to simplify administration: > > > > https://docs.basex.org/wiki/Commands#String_Syntax > > <https://docs.basex.org/wiki/Commands#String_Syntax> > > > > I was surprised that this did not work: > > > > % cat foo.bsx > > > > CREATE DB test > > > > ADD TO embedded.xml <root>embedded</root> > > > > # run query > > > > XQUERY <hits>{ count(//text()) }</hits> > > > > CLOSE > > > > > > % basex foo.bsx > > > > Stopped at /.../foo.bsx, 1/9: > > > > [XPST0003] Unexpected end of query: 'DB test...'. > > > I think the suffix of the file needs to be ".bxs" to be treated as a > command sequence, otherwise it is treated as XQuery code. So you have > the wrong file suffix. >
CG
Christian Grün29 Jun 2021 10:47 #
Hi Jonathan,
Quoted text · Jonathan Robie · 2 lines
> ... > XQUERY ./xquery/merge-proper-nouns-oshb.xq
I don’t have access to the query file you want to evaluate, but I assume your command script should work if you replace XQUERY with the RUN command [1].
You can skip the
WRITEBACK option if you perform updates on database. It is only required to update local files [2]:
XQUERY file:write(file:base-dir() || 'example.xml', <a/>)
SET WRITEBACK true
XQUERY insert node <b/> into doc('example.xml')/*
XQUERY doc('example.xml')
Hope this helps,
Christian
[1] https://docs.basex.org/wiki/Commands#RUN
[2] https://docs.basex.org/wiki/Options#WRITEBACKCG
Hi Jonathan,
Quoted text · Jonathan Robie · 2 lines
> SET WRITEBACK true > RUN ./xquery/merge-proper-nouns-oshb.xq
I see you are still using
WRITEBACK… Does your query file
'merge-proper-nouns-oshb.xq' access any local files that you are updating?Quoted text · Jonathan Robie · 1 line
> XQUERY ./xquery/mappings.xq
Once again, you may need to replace XQUERY with RUN. If you use XQUERY, you must supply the query itself after the command.
Quoted text · Jonathan Robie · 1 line
> how do I run a query that is in a file and write the results back to a different file?
You could use the File Module for that, or
fn:put. If you want to write the result of a query evaluation to a file, you’ll probably need to do this on command line for all results returned by a script>
basex -o result.xml script.bxs
Hope this helps,
ChristianJR
Thanks - this fixed my update problem.
One more question ... how do I run a query that is in a file and write the results back to a different file? The last line in this script does not produce output:
DROP DB oshb-morphology
CREATE DB oshb-morphologyADD ./morphhb/wlc
SET WRITEBACK trueRUN ./xquery/merge-proper-nouns-oshb.xq
EXPORT ./out
XQUERY ./xquery/mappings.xq
It's not a problem with databases that are not open, I open databases explicitly in mappings.xq, e.g. db:open("oshb-morphology") and the same query works fine from the GUI with no databases explicitly opened.
Jonathan
Quoted text · Christian Grün · 25 lines
On Tue, Jun 29, 2021 at 4:48 AM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Jonathan, > > > ... > > XQUERY ./xquery/merge-proper-nouns-oshb.xq > > I don’t have access to the query file you want to evaluate, but I > assume your command script should work if you replace XQUERY with the > RUN command [1]. > > You can skip the WRITEBACK option if you perform updates on database. > It is only required to update local files [2]: > > XQUERY file:write(file:base-dir() || 'example.xml', <a/>) > SET WRITEBACK true > XQUERY insert node <b/> into doc('example.xml')/* > XQUERY doc('example.xml') > > Hope this helps, > Christian > > [1] https://docs.basex.org/wiki/Commands#RUN > [2] https://docs.basex.org/wiki/Options#WRITEBACK >
JR
Great, thanks!
Jonathan
Quoted text · Christian Grün · 28 lines
On Tue, Jun 29, 2021 at 10:22 AM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Jonathan, > > > SET WRITEBACK true > > RUN ./xquery/merge-proper-nouns-oshb.xq > > I see you are still using WRITEBACK… Does your query file > 'merge-proper-nouns-oshb.xq' access any local files that you are > updating? > > > XQUERY ./xquery/mappings.xq > > Once again, you may need to replace XQUERY with RUN. If you use > XQUERY, you must supply the query itself after the command. > > > how do I run a query that is in a file and write the results back to a > different file? > > You could use the File Module for that, or fn:put. If you want to > write the result of a query evaluation to a file, you’ll probably need > to do this on command line for all results returned by a script> > > basex -o result.xml script.bxs > > Hope this helps, > Christian >
Written for BaseX 9.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery