Search code examples
xsltpython-3.8saxon-c

python saxonche - why executable.transform_to_value returns None?


Just found saxonche library for python. Which is great. While playing around I encountered following issue:

When trying to use executable.transform_to_value to return a PyXdmNode, the code below produces None.

input xml:

<document>
    <a>123456</a>
</document>

xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <root>
      <b>
        <xsl:value-of select="document/a"/>
      </b>
    </root>
  </xsl:template>
</xsl:stylesheet>

python3.8.10:

from saxonche import PySaxonProcessor

input_xml ="examples/input.xml"
stylesheet = "examples/test.xslt"

proc = PySaxonProcessor(license=False)
xsltproc = proc.new_xslt30_processor()

input_xdm = proc.parse_xml(xml_file_name=input_xml)
print(type(input_xdm))
print("--------------")

stylesheet_xdm = proc.parse_xml(xml_file_name=stylesheet)
print(type(stylesheet_xdm))
print("==============")

executable = xsltproc.compile_stylesheet(stylesheet_node=stylesheet_xdm)

# produces None (what I'm doing wrong?)
result_xdm = executable.transform_to_value(xdm_node=input_xdm)
print(type(result_xdm))
print(result_xdm)

print("--------------")

#produces string (as expected)
result_str = executable.transform_to_string(xdm_node=input_xdm)
print(type(result_str))
print(result_str)

output:

python3 test_xslt_xdm_node.py
<class 'saxonche.PyXdmNode'>                                                       
--------------                                                                     
<class 'saxonche.PyXdmNode'>                                                       
==============                                                                     
<class 'NoneType'>                                                                 
None                                                                               
--------------                                                                     
<class 'str'>                                                                      
<?xml version="1.0" encoding="UTF-8"?><root><b>123456</b></root>

As you can see, I did try to transform to string and it works as expected. Does anybody have an idea what I'm doing wrong?


Solution

  • That seems a bug in SaxonC (filed as https://saxonica.plan.io/issues/6334), I suggest you change the Python code to

    result_xdm = executable.apply_templates_returning_value(xdm_value=input_xdm)
    print(type(result_xdm))
    print(result_xdm)
    

    as a workaround.