Bug #6437
closedPyXdmValue of size 1 where the item is an XDM array returns that item as a PyXdmFunctionItem and calling get_array_value() on that item gives error AttributeError: 'saxonche.PyXdmArray' object has no attribute 'derivednptr'
100%
Description
I am struggling to write some Python code Iusing SaxonCHE 12.4.2) allowing me to process the result of an XPath evaluation that is an XDM array. The evaluate
gives me an PyXdmValue
of size
1, the head
then gives me a PyXdmFunctionItem
, not an array, so I try to call get_array_value()
on that function item but then get an error
File "python_saxon\saxonc.pyx", line 4607, in saxonche.PyXdmItem.get_array_value
AttributeError: 'saxonche.PyXdmArray' object has no attribute 'derivednptr'
Complete code:
from saxonche import PySaxonProcessor
xslt = '''<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output method="adaptive"/>
<xsl:template match="/">
<xsl:sequence select="array { order!items!item!@code!xs:integer(.) }"/>
</xsl:template>
</xsl:stylesheet>'''
xml = '''<order id="1021">
<items>
<item code="11">
<quantity>2</quantity>
<price>50</price>
</item>
<item code="21">
<quantity>1</quantity>
<price>250</price>
</item>
<item code="13">
<quantity>4</quantity>
<price>100</price>
</item>
</items>
<status>3</status>
</order>'''
with PySaxonProcessor() as processor:
xpath_processor = processor.new_xpath_processor()
xpath_processor.set_parameter('xslt', processor.make_string_value(xslt))
xpath_processor.set_parameter('xml', processor.make_string_value(xml))
xpath_result = xpath_processor.evaluate('''transform(
map {
'stylesheet-text' : $xslt,
'source-node' : parse-xml($xml),
'delivery-format' : 'raw'
}
)?output''')
print(type(xpath_result))
print(xpath_result.size)
head_item = xpath_result.head
print(type(head_item))
array_result = head_item.get_array_value()
result_list = [item.get_atomic_value() for item in array_result.as_list()]
print(result_list)
Output:
Traceback (most recent call last):
File "C:\Users\marti\PycharmProjects\Xslt1Test1\saxonc12test2.py", line 52, in <module>
array_result = head_item.get_array_value()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python_saxon\saxonc.pyx", line 4607, in saxonche.PyXdmItem.get_array_value
AttributeError: 'saxonche.PyXdmArray' object has no attribute 'derivednptr'
<class 'saxonche.PyXdmValue'>
1
<class 'saxonche.PyXdmFunctionItem'>
I wonder whether I shouldn't get a PyXdmArray
directly or if that is not possible how to get a PyXdmArray from the PyXdmFunctionItem.
Related issues
Updated by Martin Honnen 7 months ago
It seems in 12.3 I got an PyXdmArray directly and could use as wanted e.g. with 12.3 a code like
from saxonche import PySaxonProcessor
with PySaxonProcessor() as processor:
xpath_processor = processor.new_xpath_processor()
xpath_result = xpath_processor.evaluate_single('array { 1 to 5 }')
print(type(xpath_result))
array_as_list = [item.head.get_atomic_value() for item in xpath_result.as_list()]
print(array_as_list)
works and outputs
<class 'saxonche.PyXdmArray'>
[1, 2, 3, 4, 5]
but with 12.4.2 I get a PyFunctionItem and can't use it as an array:
<class 'saxonche.PyXdmFunctionItem'>
Traceback (most recent call last):
File "C:\Users\marti\PycharmProjects\SaxonC12XdmArrayTests\xpath-return-xdm-array-test1.py", line 10, in <module>
array_as_list = [item.head.get_atomic_value() for item in xpath_result.as_list()]
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'saxonche.PyXdmFunctionItem' object has no attribute 'as_list'
So this looks like a regression in 12.4.
Updated by O'Neil Delpratt 7 months ago
- Status changed from New to In Progress
Thank for reporting the issue. I have reproduced it and it is a bug. Fixed in the PyXdmFunctionItem error AttributeError: 'saxonche.PyXdmArray' object has no attribute 'derivednptr'
.
Investigating the other issue:
AttributeError: 'saxonche.PyXdmFunctionItem' object has no attribute 'as_list'
Updated by O'Neil Delpratt 7 months ago
- Copied to Bug #6439: AttributeError: 'saxonche.PyXdmFunctionItem' object has no attribute 'as_list' added
Updated by O'Neil Delpratt 7 months ago
For the issue in comment #1 I have created a new bug issue for it: #6439 It is a bug which I have now fixed
Updated by O'Neil Delpratt 7 months ago
- Status changed from In Progress to Resolved
- % Done changed from 0 to 100
Issue fixed and available for the next maintenance release. Test cases added to Pytest
Updated by O'Neil Delpratt 6 months ago
- Status changed from Resolved to Closed
- Fixed in version set to 12.5
Bug fix applied in the Saxon 12.5 Maintenance release.
Please register to edit this issue