Forums » Saxon/C Help and Discussions »
PyXsltProcessor.transform_to_file() not working.
Added by Daniel Haley almost 4 years ago
I'm using Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C on Windows 10 with Python 3.8.7 and I'm having an issue with transform_to_file() using the PyXsltProcessor. I don't get an output file and I don't see any exceptions. Everything seems to work fine with the PyXslt30Processor.
I've attached sample Python, XSLT, and XML input to reproduce.
This seems like a bug, but maybe I'm missing something?
Here's the Python:
import os
import sys
import saxonc
def try_to_string(proc):
xsltproc = proc.new_xslt_processor()
xsltproc.set_parameter("before", proc.make_string_value("two"))
xsltproc.set_parameter("after", proc.make_string_value("duece"))
result = xsltproc.transform_to_string(source_file="try_saxon_input.xml",
stylesheet_file="try_saxon.xsl")
return bool({'duece' in result})
def try_to_file(proc, proc_type=None):
output_filename = "try_saxon_output.xml"
if os.path.isfile(output_filename):
os.remove(output_filename)
if proc_type == "3.0":
# WORKS
xsltproc = proc.new_xslt30_processor()
else:
# DOES NOT WORK (No file output. No error message.)
xsltproc = proc.new_xslt_processor()
xsltproc.set_parameter("before", proc.make_string_value("two"))
xsltproc.set_parameter("after", proc.make_string_value("duece"))
xsltproc.set_cwd(os.getcwd())
xsltproc.transform_to_file(source_file="try_saxon_input.xml",
stylesheet_file="try_saxon.xsl",
output_file=output_filename)
try:
with open(output_filename) as output_file:
return bool({'duece' in output_file})
except FileNotFoundError:
return False
def main():
with saxonc.PySaxonProcessor(license=False) as proc:
print(f"Processor version: {proc.version}")
print(f"Did try_to_string() work? - {try_to_string(proc)}")
print(f"Did try_to_file() work? - {try_to_file(proc)}")
print(f"Did try_to_file() (3.0) work? - {try_to_file(proc, '3.0')}")
if __name__ == '__main__':
sys.exit(main())
try_saxon.py (1.71 KB) try_saxon.py | |||
try_saxon.xsl (1.05 KB) try_saxon.xsl | |||
try_saxon_input.xml (233 Bytes) try_saxon_input.xml |
Replies (7)
Please register to reply
RE: PyXsltProcessor.transform_to_file() not working. - Added by O'Neil Delpratt almost 4 years ago
Hi,
This looks like a bug. I will investigate it further.
In the case of the PyXslProcessor what happens if you add in the following methods after the transform_to_file()
if xsltproc.exception_occurred():
print(xsltproc.get_error_message(0))
RE: PyXsltProcessor.transform_to_file() not working. - Added by Daniel Haley almost 4 years ago
Thanks for the feedback.
After adding the methods, there is an exception printed:
net.sf.saxon.option.cpp.SaxonCException : No destination has been supplied
Thanks for the tip on using the "exception_occurred" method.
RE: PyXsltProcessor.transform_to_file() not working. - Added by O'Neil Delpratt almost 4 years ago
The PyXslt30Processor is the preferred API for Xslt 3.0 processor.
But I still not sure my the PyXsltProcressor
should fail. Maybe the xsltproc.set_cwd(dir)
is required too. I will investigate this further.
RE: PyXsltProcessor.transform_to_file() not working. - Added by Daniel Haley almost 4 years ago
Yes I switched over to PyXslt30Processor
so I'm not blocked, but still wanted to figure out why I couldn't get PyXsltProcessor
working.
I'm setting the cwd with xsltproc.set_cwd(os.getcwd())
, so I don't think that's the issue.
Thanks for investigating!
RE: PyXsltProcessor.transform_to_file() not working. - Added by O'Neil Delpratt almost 4 years ago
I will investigate this further as it might be a bug in the PyXsltProcessor class.
RE: PyXsltProcessor.transform_to_file() not working. - Added by O'Neil Delpratt almost 4 years ago
Hi,
I have reported a bug on the underlying XsltProcessor class for PyXsltProcessor, see: https://saxonica.plan.io/issues/4943
The workaround for this class is to set the output file via set_output_file(filename_str)
or using the set_property('o', 'outputfilename')
RE: PyXsltProcessor.transform_to_file() not working. - Added by Daniel Haley almost 4 years ago
Thank you Sir!
Please register to reply