Project

Profile

Help

Returning maps from XQuery

Added by Martin Honnen 11 months ago

So far I understand that XQuery/XSLT/XPath 3 maps and arrays as functions have no representation in the Python API. Nevertheless I wonder whether a query creating a map and serializing as JSON and using run_query_to_string should not return the JSON serialization of the map. I seem to get None. Is that intended?

One file I have tries

import saxonc

with saxonc.PySaxonProcessor(license=False) as proc:
    print(proc.version)

    xquery_processor = proc.new_xquery_processor()

    query = """declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'json';

map { 'prop1' : 'value 1', 'prop2' : 'value 2' }"""

    xquery_processor.set_property("!method", "json")

    result = xquery_processor.run_query_to_string(query_text = query)

    print("string result: ", result)

    result = xquery_processor.run_query_to_value(query_text = query)

    print("value result: ", result)

but outputs

Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5J from Saxonica
string result:  None
value result:  None

, that is, None even for the run_query_to_string result.

The other tries to solely rely on XQuery's declare option for the serialization method

import saxonc

with saxonc.PySaxonProcessor(license=False) as proc:
    print(proc.version)

    xquery_processor = proc.new_xquery_processor()

    query = """declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'json';

map { 'prop1' : 'value 1', 'prop2' : 'value 2' }"""

    result = xquery_processor.run_query_to_string(query_text = query)

    print("string result: ", result)

    result = xquery_processor.run_query_to_value(query_text = query)

    print("value result: ", result)

and also outputs

Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5J from Saxonica
string result:  None
value result:  None

Replies (3)

Please register to reply

RE: Returning maps from XQuery - Added by Martin Honnen 11 months ago

I have run some similar tests now using XSLT 3 and there the JSON serialization of maps with the call/apply-templates_returning_string gives me a proper string output and not None. And to_value nicely emits a warning/error "Error: applyTemplateToValue: FunctionItem found. Currently not be handled".

So somehow the XSLT 3 part of the API is more advanced than the XQuery 3 part to serialize maps correctly when strings are requested or to emit a warning of lack of function handling when values are requested.

RE: Returning maps from XQuery - Added by O'Neil Delpratt 11 months ago

Hi,

Thanks for reporting the issues you have found.

Saxon/C has reached a mature status now we really should be supporting the XdmFunctionItem class like as in Java. We have all the capabilities given that the XdmFunctionItem object is being correctly passed to the C++ code, which it recognises but not doing anything with it.

I have created a bug issue for this feature request: 4365

Also the run_query_to_string should properly return a string of the map. I have raised a related bug issue for this:

4366

RE: Returning maps from XQuery - Added by O'Neil Delpratt 11 months ago

Bug issue #4366 has been fixed. Patch available in the next maintenance release. Serialization properties defined using declare option output:x = 'value' will now be picked up. Also properties defined using the setProperty function, e.g setProperty("!method", "json") will also be picked up.

    (1-3/3)

    Please register to reply