Project

Profile

Help

Support #5027

setting xslt parameter from python/saxon-c

Added by Lou Burnard 3 months ago. Updated 3 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Start date:
2021-06-25
Due date:
% Done:

0%

Estimated time:
Found in version:
1.2.1

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

History

#1 Updated by Michael Kay 3 months 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).

#2 Updated by Lou Burnard 3 months 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?_

#3 Updated by Michael Kay 3 months ago

The most convenient way is probably the factory method proc.make_string_value("value")

#4 Updated by Lou Burnard 3 months 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?

#5 Updated by Lou Burnard 3 months ago

Oops., make that

x.set_parameter("param", proc.make_string(VALUE))

#6 Updated by O'Neil Delpratt 3 months 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

Please register to edit this issue

Also available in: Atom PDF