How to use the collection()-function with saxonche
Question:
I am trying to use the collection()
-function using the new saxonche
-Python-Module (https://pypi.org/project/saxonche/).
I would expect, that it returns all XML-documents inside the current directory. Instead it just returns None
.
My code looks like:
from saxonche import PySaxonProcessor
from os import getcwd
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xq = proc.new_xquery_processor()
xq.set_query_base_uri(getcwd())
xq.set_query_content("collection('.')/node()")
r = xq.run_query_to_value()
print(r)
Any suggestions?
Answers:
I get the suggested collection('?select=*.xml')
to work if I use e.g.
from pathlib import Path
and then set
xq.set_query_base_uri(Path('.', 'foo.xml').absolute().as_uri())
For the method set_query_base_uri the documentation (i.e. https://www.saxonica.com/saxon-c/doc12/html/saxonc.html#PyXQueryProcessor-set_query_base_uri) states:
set_query_base_uri(self, base_uri)
Set the static base URI for the query.
Args:
base_uri (str): The static base URI; or None to indicate that no base URI is available
Therefore we need to supply URI as a string. See another solution below which is based on the first one:
from saxonche import PySaxonProcessor
from os import getcwd
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xq = proc.new_xquery_processor()
xq.set_query_base_uri('file://'+getcwd()+'/')
xq.set_query_content("collection('?select=*.xml')")
r = xq.run_query_to_value()
print(r)
I am trying to use the collection()
-function using the new saxonche
-Python-Module (https://pypi.org/project/saxonche/).
I would expect, that it returns all XML-documents inside the current directory. Instead it just returns None
.
My code looks like:
from saxonche import PySaxonProcessor
from os import getcwd
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xq = proc.new_xquery_processor()
xq.set_query_base_uri(getcwd())
xq.set_query_content("collection('.')/node()")
r = xq.run_query_to_value()
print(r)
Any suggestions?
I get the suggested collection('?select=*.xml')
to work if I use e.g.
from pathlib import Path
and then set
xq.set_query_base_uri(Path('.', 'foo.xml').absolute().as_uri())
For the method set_query_base_uri the documentation (i.e. https://www.saxonica.com/saxon-c/doc12/html/saxonc.html#PyXQueryProcessor-set_query_base_uri) states:
set_query_base_uri(self, base_uri)
Set the static base URI for the query.
Args:
base_uri (str): The static base URI; or None to indicate that no base URI is available
Therefore we need to supply URI as a string. See another solution below which is based on the first one:
from saxonche import PySaxonProcessor
from os import getcwd
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xq = proc.new_xquery_processor()
xq.set_query_base_uri('file://'+getcwd()+'/')
xq.set_query_content("collection('?select=*.xml')")
r = xq.run_query_to_value()
print(r)