Bug #5451
closedSaxonC 11.3: AttributeError: 'saxonc.PySaxonProcessor' object has no attribute 'new_xslt_processor'
0%
Description
Hi!
I just upgraded from v1.2.1 to v11.3. Whilst my IDE is showing no issues, during executing of my script I get
AttributeError: 'saxonc.PySaxonProcessor' object has no attribute 'new_xslt_processor'
My Code:
import saxonc
file = ''
proc = saxonc.PySaxonProcessor(license=False)
xpath_proc = proc.new_xpath_processor()
xpath_proc.set_context(file_name=file)
item = xpath_proc.evaluate_single('/*/namespace::*[name()=\'\']')
Whilst the method is also available in the API docs: https://www.saxonica.com/saxon-c/doc11/html/saxonc.html#PySaxonProcessor
Related issues
Updated by O'Neil Delpratt over 2 years ago
- Category set to Python
- Status changed from New to AwaitingInfo
- Assignee set to O'Neil Delpratt
Hi,
The error indicates to me that somehow Saxon/C 1.2.1 is still being used or cached. The method 'new_xslt_processor()' is no longer available in SaxonC 11.
Updated by Peter Jan over 2 years ago
Ah. Now I also see that I misread https://www.saxonica.com/saxon-c/doc11/html/saxonc.html#PySaxonProcessor and its new_xslt30_processor
and not new_xslt_processor
.
I'm using PyCharm.
This is the part of my Dockerfile
which is relevant to build the extension:
# python & saxon deps
ARG SAXON='libsaxon-HEC-setup64-v11.3'
ENV VIRTUAL_ENV=/home/www/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV SAXONC_PYTHON=/home/www/saxonc
ENV PYTHONPATH="$PYTHONPATH:$SAXONC_PYTHON"
RUN set -eux; \
buildDeps='build-essential python3-dev'; \
apt-get update && apt-get install -y --no-install-recommends \
$buildDeps \
python3 \
python3-pip \
python3-pkg-resources \
python3-venv \
; \
su www -c "python3 -m venv ${VIRTUAL_ENV}"; \
su www -c "pip install --upgrade pip cython --no-cache-dir"; \
cd /tmp; \
su www -c "curl -s -o ${SAXON}.zip https://www.saxonica.com/download/${SAXON}.zip"; \
su www -c "unzip -qq ${SAXON}.zip"; \
cd libsaxon-HEC-*; \
cp libsaxonhec.so /usr/lib/.; \
cp -r rt /usr/lib/.; \
cp -r saxon-data /usr/lib/.; \
cd Saxon.C.API/python-saxon; \
su www -c "python saxon-setup.py -qq build_ext -if"; \
cp -r /tmp/libsaxon-HEC-*/Saxon.C.API/python-saxon $SAXONC_PYTHON; \
su www -c "pip uninstall -y cython"; \
apt-get purge --auto-remove -y $buildDeps; \
rm -rf /var/lib/apt/lists/* /tmp/*;
Let me check if its a caching issue in my IDE.
Updated by Peter Jan over 2 years ago
Ah!
So, the confusion is all mine. I was looking in my code at the function new_xpath_processor
, whilst the error message was about new_xslt_processor
(which is in another script).
Going to the right script, I noticed no red line below the new_xslt_processor
, but I noticed that autocomplete was broken. Chaning new_xslt_processor
to new_xslt30_processor
immediately showed me other outdated methods like set_source
and set_output_file
.
Updated by Peter Jan over 2 years ago
Would you happen to know (or point me to relevant text) how I can migrate the code below:
xslt_proc = proc.new_xslt_processor()
xslt_proc.set_source(file_name=file)
xslt_proc.set_output_file(result)
xslt_proc.compile_stylesheet(stylesheet_text=xsl)
xslt_proc.transform_to_file()
xslt_proc.clear_properties()
xslt_proc.clear_parameters()
set_source
, set_output_file
and clear_properties
are no longer available.
Updated by Peter Jan over 2 years ago
The current (v11) docs at https://www.saxonica.com/saxon-c/documentation11/index.html#!samples/samples_python still show the old new_xslt_processor
.
Updated by Peter Jan over 2 years ago
In my IDE, If I CTRL + click on the saxonc
part of import saxonc
, it opens the file saxonc.py
with the module definition.
Theres a few functions with example documentation, and they also mention the set_source
and set_output_file
function, but it seems these functions don't exist anymore.
Updated by O'Neil Delpratt over 2 years ago
Thanks for the spotting the bug in the documentation. I have created a new bug issue to track fix to the documentation. #5452
Updated by O'Neil Delpratt over 2 years ago
Peter Jan wrote in #note-6:
Would you happen to know (or point me to relevant text) how I can migrate the code below:
xslt_proc = proc.new_xslt_processor() xslt_proc.set_source(file_name=file) xslt_proc.set_output_file(result) xslt_proc.compile_stylesheet(stylesheet_text=xsl) xslt_proc.transform_to_file() xslt_proc.clear_properties()
The PyXslt30Processor has compile methods to create an PyXsltExecutable
object. See: https://www.saxonica.com/saxon-c/doc11/html/saxonc.html#PyXsltExecutable
See examples here: https://www.saxonica.com/saxon-c/documentation11/index.html#!samples/samples_python
Updated by O'Neil Delpratt over 2 years ago
Also check out the file test_saxonc.py
in the directory Saxon.C.API/python-saxon
Updated by Peter Jan over 2 years ago
Thanks! For future reference:
xslt_proc = proc.new_xslt30_processor()
executable = xslt_proc.compile_stylesheet(stylesheet_text=xsl)
executable.transform_to_file(source_file=file, output_file=result)
xslt_proc.clear_parameters()
What I also noticed is that exception_occurred
is no longer a method which returns a bool, but is now a property which returns a bool. Ofcourse this can be intentional.
I had:
if xslt_proc.exception_occurred():
which throws:
TypeError: 'bool' object is not callable
So now I have:
if xslt_proc.exception_occurred:
This issue can be closed, since you already created another for the documentation part. Thank you for your responses.
Updated by O'Neil Delpratt over 2 years ago
To convert your example there are two ways I would consider:
- Using the 'one-shot' methods in the
PyXslt30Processor
class
xslt_proc = proc.new_xslt30_processor()
xslt_proc.transform_to_file(stylesheet_file='style.xsl', output_file=result, source_file=file)
xslt_proc.clear_properties()
- Compiling the stylesheet to PyXsltExecutable object is the preferred way. It allows you to use more of the API features which is designed for XSLT 3.0:
xslt_proc = proc.new_xslt30_processor()
executable = xslt_proc.compile_stylesheet(stylesheet_text=xsl)
executable.transform_to_file(source_file=file, output_file=result)
Updated by O'Neil Delpratt over 2 years ago
- Related to Bug #5452: set_source and set_output_file functions don't exists but are in examples added
Updated by O'Neil Delpratt over 2 years ago
- Status changed from AwaitingInfo to Closed
Please register to edit this issue