Forums » Saxon/C Help and Discussions »
PyXdmValue creation and population
Added by Martin Honnen 11 months ago
For me, both under Windows and Linux code like
from saxonche import PySaxonProcessor, PyXdmValue
with PySaxonProcessor() as saxon_proc:
xdm_value = PyXdmValue()
#print(xdm_value)
xdm_value.add_xdm_item(saxon_proc.make_string_value('foo'))
print(xdm_value)
xdm_value.add_xdm_item(saxon_proc.make_string_value('bar'))
print(xdm_value)
crashes, on Windows I get some
Traceback (most recent call last):
File "C:\Users\marti\PycharmProjects\SaxonC12PyXdmValueTest1\main.py", line 10, in <module>
print(xdm_value)
TypeError: __str__ returned non-string (type NoneType)
Process finished with exit code -1073740940 (0xC0000374)
under Linux a segmentation fault.
What is the right way to create a PyXdmValue sequence of strings?
Replies (5)
Please register to reply
RE: PyXdmValue creation and population - Added by O'Neil Delpratt 11 months ago
This is a bug. I have created the following bug issue to keep track of its progress #6324 Thanks for reporting it.
RE: PyXdmValue creation and population - Added by Martin Honnen 11 months ago
Thanks, O'Neil.
What I was trying to do was to create a sequence/a PyXdmValue and pass it to XSLT as a parameter, for instance like in this code:
from saxonche import PySaxonProcessor, PyXdmValue
with PySaxonProcessor() as saxon_proc:
#xdm_seq = saxon_proc.new_xpath_processor().evaluate('"foo", "bar"')
xdm_seq = PyXdmValue()
xdm_seq.add_xdm_item(saxon_proc.make_string_value('test1'))
xdm_seq.add_xdm_item(saxon_proc.make_string_value('test2'))
xslt30_processor = saxon_proc.new_xslt30_processor()
xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_file=r'sample1.xsl')
xslt30_executable.set_parameter('image-names', xdm_seq)
xslt_result = xslt30_executable.call_template_returning_value()
print(xslt_result)
This doesn't work, instead (under Windows, at least,) gives an error
java.lang.IllegalArgumentException: Invalid handle
at com.oracle.svm.core.handles.ObjectHandlesImpl.doGet(ObjectHandlesImpl.java:232)
at com.oracle.svm.core.handles.ObjectHandlesImpl.get(ObjectHandlesImpl.java:220)
at net.sf.saxon.option.cpp.ProcessorDataAccumulator.addProcessorValue(ProcessorDataAccumulator.java:139)
I can't tell from the bug https://saxonica.plan.io/issues/6324 you have created if the above will work once that bug is fixed or whether there is an additional problem. I hope you can investigate that as well, at least once you have a fix for https://saxonica.plan.io/issues/6324.
A sample stylesheet for the above Python code is e.g.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:param name="image-names" as="xs:string*" select="'ABC', 'MNO'"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/" name="xsl:initial-template">
<axsl:stylesheet version="3.0">
<axsl:template name="Dunning">
<axsl:param name="Image"/>
<axsl:choose>
<axsl:when test="$Image = '{$image-names[1]}'">
<axsl:text>Found</axsl:text>
</axsl:when>
<axsl:when test="$Image = '{$image-names[2]}'">
<axsl:text>NotFound</axsl:text>
</axsl:when>
</axsl:choose>
</axsl:template>
</axsl:stylesheet>
</xsl:template>
</xsl:stylesheet>
RE: PyXdmValue creation and population - Added by O'Neil Delpratt 11 months ago
Thanks for the update. For the first issue the problem is the reference count is not being incremented when an XdmItem is added to the XdmValue
using add_xdm_item
. I have fixed the issue and will update the bug issue. The workaround is to create an XdmAtomicValue with a variable.
For the following code:
xdm_value.add_xdm_item(saxon_proc.make_string_value('foo'))
The workaround works for me:
avalue = saxon_proc.make_string_value('foo')
xdm_value.add_xdm_item(avalue)
RE: PyXdmValue creation and population - Added by O'Neil Delpratt 11 months ago
RE: PyXdmValue creation and population is relating to the same issue.
workaround:
from saxoncee import PySaxonProcessor, PyXdmValue
with PySaxonProcessor() as saxon_proc:
#xdm_seq = saxon_proc.new_xpath_processor().evaluate('"foo", "bar"')
xdm_seq = PyXdmValue()
avalue1 = saxon_proc.make_string_value('test1')
avalue2 = saxon_proc.make_string_value('test12')
xdm_seq.add_xdm_item(avalue1)
xdm_seq.add_xdm_item(avalue2)
xslt30_processor = saxon_proc.new_xslt30_processor()
xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_file=r'sample1.xsl')
xslt30_executable.set_parameter('image-names', xdm_seq)
xslt_result = xslt30_executable.call_template_returning_value()
print(xslt_result)
but this will be fixed in the next maintenance release.
RE: PyXdmValue creation and population - Added by Martin Honnen 11 months ago
Thanks again, O'Neil,
that workaround indeed works fine.
Interestingly enough if I try to reuse a single variable e.g.
xdm_seq = PyXdmValue()
xdm_string = saxon_proc.make_string_value('DEF')
xdm_seq.add_xdm_item(xdm_string)
xdm_string = saxon_proc.make_string_value('PQR')
xdm_seq.add_xdm_item(xdm_string)
xslt30_executable.set_parameter('image-names', xdm_seq)
stylesheet_result = xslt30_executable.call_template_returning_value()
print(stylesheet_result)
I get the same or very similar error I mentioned in https://saxonica.plan.io/boards/4/topics/9589?r=9591#message-9591:
java.lang.IllegalArgumentException: Invalid handle
at com.oracle.svm.core.handles.ObjectHandlesImpl.doGet(ObjectHandlesImpl.java:232)
at com.oracle.svm.core.handles.ObjectHandlesImpl.get(ObjectHandlesImpl.java:220)
at net.sf.saxon.option.cpp.ProcessorDataAccumulator.addProcessorValue(ProcessorDataAccumulator.java:139)
Please register to reply