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?

Asked By: B Polit

||

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())
Answered By: Martin Honnen

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)                                                             
Answered By: ond1
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.