Project

Profile

Help

Bug #5027

closed

setting xslt parameter from python/saxon-c

Added by Lou Burnard almost 3 years ago. Updated about 2 years ago.

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

100%

Estimated time:
Found in version:
1.2.1
Fixed in version:
11.1
Platforms:

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

Actions #1

Updated by Michael Kay almost 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).

Actions #2

Updated by Lou Burnard almost 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?_

Actions #3

Updated by Michael Kay almost 3 years ago

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

Actions #4

Updated by Lou Burnard almost 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?

Actions #5

Updated by Lou Burnard almost 3 years ago

Oops., make that

x.set_parameter("param", proc.make_string(VALUE))
Actions #6

Updated by O'Neil Delpratt almost 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

Actions #7

Updated by O'Neil Delpratt about 2 years ago

  • Status changed from New to Resolved

sample added to the documentation. Available for the next release.

Actions #8

Updated by O'Neil Delpratt about 2 years ago

  • Tracker changed from Support to Bug
Actions #9

Updated by O'Neil Delpratt about 2 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

Also available in: Atom PDF