pythonxsltxslt-2.0saxonsaxon-c

Passing parameters into XSLT 2.0 transformation using Python and saxonche


After successful "pip install saxonche" Python should perform XSLT2.0-tranformations. The following xslt script though announces and refers also to two parameters, that would have to be passed through the Python script, but are not even metioned in the provided code hereafter. Obvious question: How to - if possible- modify the following Python-script, so two variables e.g. my_param1='value1' and my_param2='value2' could successfuly be refered to, like expressed in the xslt code? Answers are highly appreciated.

Python:

from saxonche import *

output_file = 'output.html'

with PySaxonProcessor(license=False) as proc:

  print(proc.version)
  try:
      xsltproc = proc.new_xslt30_processor()
      document = proc.parse_xml(xml_file_name='input.xml')
      executable = xsltproc.compile_stylesheet(stylesheet_file="xslt2.xsl")

      output = executable.transform_to_string(xdm_node=document)
      
      print(output)
          
      with open(output_file, 'wb') as f:
          f.write(output.encode('utf-8'))
        
  
  except PySaxonApiError as err:
              print('Error during function call', err)
              

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:param name="my_param1" />
    <xsl:param name="my_param2" />

    <xsl:template match="/">
        <html>
            <body>
                       <div>
              <xsl:value-of select="root/data" />
              <xsl:text> | </xsl:text>
              <xsl:value-of select="$my_param1" /> 
              <xsl:text> | </xsl:text>
              <xsl:value-of select="$my_param2" /> 
              </div>
            </body>
        </html>
    </xsl:template>
 </xsl:stylesheet>

XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <data>Hello world!</data>
</root>

Actual (rendered HTML):

Hello world! | |

Wanted (rendered HTML):

Hello world! | value1 | value2

Solution

  • Since you're just passing in strings, you can use executable.set_parameter() to pass in a string value using proc.make_string_value()...

    executable.set_parameter('my_param1', proc.make_string_value('value1'))
    executable.set_parameter('my_param2', proc.make_string_value('value2'))
    

    Full example...

    from saxonche import *
    
    output_file = 'output.html'
    
    with PySaxonProcessor(license=False) as proc:
        print(proc.version)
        try:
            xsltproc = proc.new_xslt30_processor()
            document = proc.parse_xml(xml_file_name='input.xml')
            executable = xsltproc.compile_stylesheet(stylesheet_file="xslt2.xsl")
    
            # Set params
            executable.set_parameter('my_param1', proc.make_string_value('value1'))
            executable.set_parameter('my_param2', proc.make_string_value('value2'))
    
            output = executable.transform_to_string(xdm_node=document)
    
            print(output)
    
            with open(output_file, 'wb') as f:
                f.write(output.encode('utf-8'))
    
        except PySaxonApiError as err:
            print('Error during function call', err)