Bug #5027
closedsetting xslt parameter from python/saxon-c
100%
Description
I am having difficulty setting a parameter in my XSLT stylesheet from Python. At the command line, I would write
$saxon [inputXML] [stylesheet] param=value
to supply a value for the <xsl:param name='param'/> at the start of my stylesheet. Using Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C I write
FILE="input.XML"
SCRIPT="stylesheet.xsl"
VALUE="value"
x = proc.new_xslt30_processor()
x.set_result_as_raw_value(True)
x.set_initial_match_selection(file_name=FILE)
x.set_parameter(param,VALUE)
result = x.apply_templates_returning_string(stylesheet_file=script)
(where the uppercase names are Python variables)
But this doesn't work: I get "nameError: name 'param' is not defined."
I am a beginner here so forgive me if this is obvious! I could not find any examples showing this kind of (fairly common) usage in https://www.saxonica.com/saxon-c/doc/html/saxonc.html
Updated by Michael Kay over 3 years ago
This is a Python error message, not a Saxon error message.
Did you write "param" without quotes, perhaps?
(Also note, the parameter value must be a PyXdmValue, not just a string).
Updated by Lou Burnard over 3 years ago
I did indeed forget the quotes..
x.set_parameter("param",VALUE)
now produces a Saxon error message in line with your comment ("TypeError: Argument 'value' has incorrect type (expected saxonc.PyXdmValue, got str) "
which is fair enough, but how do I tell Python to convert my string to a PyXdmValue?_
Updated by Michael Kay over 3 years ago
The most convenient way is probably the factory method proc.make_string_value("value")
Updated by Lou Burnard over 3 years ago
Brill! Many thanks. With the line
x.set_parameter("param," proc.make_string(VALUE))
everything works as it should.
Next time the doc gets updated, may I suggest it would be helpful to document this with an example?
Updated by Lou Burnard over 3 years ago
Oops., make that
x.set_parameter("param", proc.make_string(VALUE))
Updated by O'Neil Delpratt over 3 years ago
Agreed. Thanks for your suggestion. You can find examples in the download Saxon.C.API/python-saxon
directory.
See more details here: https://www.saxonica.com/saxon-c/documentation/index.html#!samples/samples_python
Updated by O'Neil Delpratt almost 3 years ago
- Status changed from New to Resolved
sample added to the documentation. Available for the next release.
Updated by O'Neil Delpratt almost 3 years ago
- Tracker changed from Support to Bug
Updated by O'Neil Delpratt almost 3 years ago
- Status changed from Resolved to Closed
- % Done changed from 0 to 100
- Fixed in version set to 11.1
Bug fix patched in SaxonC 11.1 release
Please register to edit this issue