Conceptual problems using BaseX from Python
Thursday, 1 November 2018 · 3 messages · last reply 3 Nov 2018
KK
Kristian Kankainen1 Nov 2018 13:01 #
Hello,
XQuery is a lovely language and together with BaseX it is very lovely.
But now, for a project that need Python, I find difficulties understanding the general workflow communicating between BaseX and Python.
My basic situation boils down to 1) execute a query on BaseX that returns a list of data 2) analyse every item in the in Python 3) push the analysis result back in to BaseX.
I have done updates like this solely in XQuery but this time I need the analysis part done in Python.
Since I seem to only get a list of strings out of a query executed by the client, is this usecase even possible?
Below is attached my sample running code
Best regards,
Kristian K
from BaseXClient import BaseXClient
# open a session
session = BaseXClient.Session('localhost', 1984, 'admin', 'admin')
# execute a query that returns a list of entry elements
query = session.query("""
let $list :=
<list>
<entry>
<a>1</a>
<b>2</b>
</entry>
<entry>
<a>1</a>
<b>2</b>
<c>3</c>
</entry>
</list>
let $items :=
for $entry in $list/entry[not(exists(./c))]
return $entry
return $items
"""
)
# process each item of the query
for typecode, item in query.iter():
print(type(item))
print(item)
session.close()This returns simply:
<class 'str'> # the type of returned item is of class 'str'
<entry> # this is the content of the plain text object
<a>1</a>
<b>2</b>
</entry>BD
Bridger Dyson-Smith1 Nov 2018 15:58 #
Hi Kristian -
I'm sorry that I can't speak to the Python portion of your question, but maybe you can leverage some of the functx functions to get better information? I confess that I'm not sure how to determine typing for items beyond a certain point - hopefully someone more knowledgeable will weigh in.
xquery version "3.1";
import module namespace functx = "http://www.functx.com";
let $list :=
<list>
<entry>
<a>1</a>
<b>2</b>
</entry>
<entry>
<a>1</a>
<b>2</b>
<c>3</c>
</entry>
</list>
let $items := for $entry in $list/entry[not(exists(./c))] return $entry
return(
<result>
<type-of>{ functx:atomic-type($items/a) }</type-of>
<sequence-type>{ functx:sequence-type($items/a) }</sequence-type>
{ $items }
</result>
)
The above returns
<result>
<type-of>xs:untypedAtomic</type-of>
<sequence-type>element()</sequence-type>
<entry>
<a>1</a>
<b>2</b>
</entry>
</result>I hope that's helpful (but recognize that it probably isn't).
Cheers,
Bridger
Quoted text · Kristian Kankainen · 70 lines
On Thu, Nov 1, 2018 at 8:01 AM Kristian Kankainen <kristian at keeleleek.ee> wrote: > Hello, > > XQuery is a lovely language and together with BaseX it is very lovely. > > But now, for a project that need Python, I find difficulties understanding > the general workflow communicating between BaseX and Python. > > My basic situation boils down to 1) execute a query on BaseX that returns > a list of data 2) analyse every item in the in Python 3) push the analysis > result back in to BaseX. > > I have done updates like this solely in XQuery but this time I need the > analysis part done in Python. > > Since I seem to only get a list of strings out of a query executed by the > client, is this usecase even possible? > > Below is attached my sample running code > > Best regards, > Kristian K > > ```Python > from BaseXClient import BaseXClient > > # open a session > session = BaseXClient.Session('localhost', 1984, 'admin', 'admin') > > # execute a query that returns a list of entry elements > query = session.query(""" > let $list := > <list> > <entry> > <a>1</a> > <b>2</b> > </entry> > <entry> > <a>1</a> > <b>2</b> > <c>3</c> > </entry> > </list> > > let $items := > for $entry in $list/entry[not(exists(./c))] > return $entry > > return $items > """ > ) > > # process each item of the query > for typecode, item in query.iter(): > print(type(item)) > print(item) > > session.close() > ``` > > This returns simply: > <class 'str'> # the type of returned item is of class 'str' > <entry> # this is the content of the plain text object > <a>1</a> > <b>2</b> > </entry> > > >
CG
Hi Kristian,
the client bindings are all fairly simple and generic. However, the BaseX server protocol provides the command "FULL", which will also give you the XQuery data type of each item [1]. You could enhance the Python client to retrieve the types as well.
Hope this helps
Christian
[1] http://docs.basex.org/wiki/Server_Protocol
Am Do., 1. Nov. 2018 13:01 hat Kristian Kankainen <kristian at keeleleek.ee> geschrieben:
Quoted text · Kristian Kankainen · 68 lines
> Hello, > > XQuery is a lovely language and together with BaseX it is very lovely. > > But now, for a project that need Python, I find difficulties understanding > the general workflow communicating between BaseX and Python. > > My basic situation boils down to 1) execute a query on BaseX that returns > a list of data 2) analyse every item in the in Python 3) push the analysis > result back in to BaseX. > > I have done updates like this solely in XQuery but this time I need the > analysis part done in Python. > > Since I seem to only get a list of strings out of a query executed by the > client, is this usecase even possible? > > Below is attached my sample running code > > Best regards, > Kristian K > > ```Python > from BaseXClient import BaseXClient > > # open a session > session = BaseXClient.Session('localhost', 1984, 'admin', 'admin') > > # execute a query that returns a list of entry elements > query = session.query(""" > let $list := > <list> > <entry> > <a>1</a> > <b>2</b> > </entry> > <entry> > <a>1</a> > <b>2</b> > <c>3</c> > </entry> > </list> > > let $items := > for $entry in $list/entry[not(exists(./c))] > return $entry > > return $items > """ > ) > > # process each item of the query > for typecode, item in query.iter(): > print(type(item)) > print(item) > > session.close() > ``` > > This returns simply: > <class 'str'> # the type of returned item is of class 'str' > <entry> # this is the content of the plain text object > <a>1</a> > <b>2</b> > </entry> > > >
Written for BaseX 9.1. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery