Project

Profile

Help

Should Python for .. in with XdmValue being a sequence of XdmMaps work?

Added by Martin Honnen over 2 years ago

From the documentation and some examples (although there the XdmValue seems a sequence of nodes) I take it that with SaxonC and Python you should be able to use the Python for .. in loop on an PyXdmValue.

But while I can do that for a sequence of nodes with e.g.

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)

    xpath_processor = proc.new_xpath_processor()

    result = xpath_processor.evaluate('(1 to 5) ! parse-xml("<item>item " || . || "</item>")')

    if result is None and xpath_processor.exception_occurred:
        print(xpath_processor.error_message)

    print(isinstance(result, PyXdmValue))
    
    print(result)
    
    for item in result:
        print(isinstance(item, PyXdmItem))
        print(isinstance(item, PyXdmNode))
        print(item)

the same with a sequence of maps fails:

Test SaxonC on Python
SaxonC-HE 11.2 from Saxonica
True
map{"value":1}
map{"value":2}
map{"value":3}
map{"value":4}
map{"value":5}
True
False
Traceback (most recent call last):
  File "C:\Users\marti\SomePath\arrays-and-maps\returnSequenceOfXdmMapsFromXPathTest2.py", line 21, in <module>
    print(item)
TypeError: __str__ returned non-string (type NoneType)

Sample:

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)

    xpath_processor = proc.new_xpath_processor()

    result = xpath_processor.evaluate('(1 to 5) ! map { "value" : . }')

    if result is None and xpath_processor.exception_occurred:
        print(xpath_processor.error_message)

    print(isinstance(result, PyXdmValue))
    
    print(result)
    
    for item in result:
        print(isinstance(item, PyXdmItem))
        print(isinstance(item, PyXdmMap))
        print(item)

Replies (4)

Please register to reply

RE: Should Python for .. in with XdmValue being a sequence of XdmMaps work? - Added by Martin Honnen over 2 years ago

Even when not trying to return the sequence of maps from XPath or XQuery but just constructing everything in Python the for .. in fails:

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)
    
    xdm_value = PyXdmValue()
    for i in range(1, 6):
        xdm_value.add_xdm_item(proc.make_map2({ 'value' : proc.make_integer_value(i)}))

    print(isinstance(xdm_value, PyXdmValue))
    
    print(xdm_value)
    
    for item in xdm_value:
        print(isinstance(item, PyXdmItem))
        print(isinstance(item, PyXdmMap))
        print(item)

Output

Test SaxonC on Python
SaxonC-HE 11.2 from Saxonica
True
map{"value":1}
map{"value":2}
map{"value":3}
map{"value":4}
map{"value":5}
True
False
Traceback (most recent call last):
  File "C:\SomePath\arrays-and-maps\createSequenceOfMapsTest1.py", line 18, in <module>
    print(item)
TypeError: __str__ returned non-string (type NoneType)

It seems that should work, or shouldn't it?

RE: Should Python for .. in with XdmValue being a sequence of XdmMaps work? - Added by O'Neil Delpratt over 2 years ago

The PyXdmValue is an iterator object which has implemented the method __iter__. We need to do the same for the PyXdmMap and PyXdmArray.

RE: Should Python for .. in with XdmValue being a sequence of XdmMaps work? - Added by O'Neil Delpratt over 2 years ago

I have created the following bug issue #5342 to keep track.

RE: Should Python for .. in with XdmValue being a sequence of XdmMaps work? - Added by O'Neil Delpratt over 2 years ago

Looking back over this issue. The problem is the item_at() method on the XmdValue does not currently support PyXdmMap and PyXdmArray

    (1-4/4)

    Please register to reply