1

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?

B Polit
  • 162
  • 8

2 Answers2

2

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())
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks! It's a litte bit weird to me, why the name of any file is needed – but it works :) – B Polit Jan 17 '23 at 10:07
  • 1
    I think `xq.set_query_base_uri(Path('.').absolute().as_uri() + '/')` will also work, if that is less weird than perhaps use that. The problem is to, first of all have a URL and not a file path, and to make sure we select relative to the directory contents so either inventing a dummy file for the base URI or ensuring the URI ends in `/` should do. – Martin Honnen Jan 17 '23 at 10:28
1

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)                                                             
ond1
  • 691
  • 6
  • 10
  • I think your suggestion `xq.set_query_base_uri('file://'+getcwd()+'/')` does work on Linux, MacOs and wherever the file directory separator is the forwards slash `/` but on Windows for `getcwd()` I get e.g. `C:\Users\marti\PycharmProjects\SaxonC1199LocalWheelTest1` and that way, while not throwing an error on trying to set some oddly formed mixture of a Windows file path and a URI e.g. `file://C:\Users\marti\PycharmProjects\SaxonC1199LocalWheelTest1/` as the base URI, the query doesn't find anything. – Martin Honnen Jan 18 '23 at 16:53
  • 1
    So I am all ears for easier cross-platform solution in Python but I would think that my suggestion to use `xq.set_query_base_uri(Path('.', 'foo.xml').absolute().as_uri())` or `xq.set_query_base_uri(Path('.').absolute().as_uri() + '/')` is safer/has better cross platform compatibility. – Martin Honnen Jan 18 '23 at 16:53
  • Yes I agree. I am now looking at accepting a _Path_ as an argument which should work as a cross-platform solution. – ond1 Jan 18 '23 at 17:05
  • 1
    I have created the feature request for adding the Path as argument to set_query_base_uri here: https://saxonica.plan.io/issues/5833 – ond1 Jan 18 '23 at 17:49
  • as an alternative the following code can be used to solve the file separator issue: `import os os.path.sep` – ond1 Jan 23 '23 at 09:14