PyXsltExecutable

class PyXsltExecutable

Bases: object

A PyXsltExecutable represents the compiled form of a stylesheet. A PyXsltExecutable is created by using one of the compile methods on the PyXslt30Processor class.

Creates a new instance, keeping a reference to its PySaxonProcessor

Parameters:

processor (PySaxonProcessor) – The processor

__init__()

Creates a new instance, keeping a reference to its PySaxonProcessor

Parameters:

processor (PySaxonProcessor) – The processor

classmethod __new__(*args, **kwargs)
__reduce__()

Helper for pickle.

__setstate__()
apply_templates_returning_file(**kwds)

Invoke the stylesheet by applying templates and serializing to a file

Invoke the stylesheet by applying templates to a supplied input sequence, saving the results to file. It is possible to specify the output file as an argument or using the set_output_file method. It is possible to specify the initial match selection either as an argument or using the set_initial_match_selection method. This method does not set the global context item for the transformation; if that is required, it can be done separately using the set_global_context_item method.

Parameters:

**kwds – Possible keyword arguments: source_file (str) or xdm_value (PyXdmValue) can be used to supply the initial match selection; output_file (str), and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")
executable.set_initial_match_selection(file_name="cat.xml")
executable.apply_templates_returning_file(output_file="result.xml")
apply_templates_returning_string(**kwargs)

Invoke the stylesheet by applying templates and serializing to a string

Invoke the stylesheet by applying templates to a supplied input sequence, saving the results as a string. It is possible to specify the initial match selection either as an argument or using the set_initial_match_selection method. This method does not set the global context item for the transformation; if that is required, it can be done separately using the set_global_context_item method.

Parameters:

**kwargs – Possible keyword arguments: source_file (str) or xdm_value (PyXdmValue) can be used to supply the initial match selection; and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction. Also accept the keyword ‘encoding’ (str) to specify the encoding of the output string. This must match the encoding specified by xsl:output in the stylesheet, or as specified using set_property() on this PyXsltExecutable. If not specified then the platform default encoding is used.

Returns:

Result of the transformation as a str value

Return type:

str

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

xslt30_proc = saxon_proc.new_xslt30_processor()
executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

executable.set_initial_match_selection(file_name="cat.xml")
content = executable.apply_templates_returning_string()
print(content)

2):

node = saxon_proc.parse_xml(xml_text="<in/>")
content = executable.apply_templates_returning_string(xdm_value=node)
print(content)
apply_templates_returning_value(**kwds)

Invoke the stylesheet by applying templates and returning a value

Invoke the stylesheet by applying templates to a supplied input sequence, saving the results as a PyXdmValue. It is possible to specify the initial match selection either as an argument or using the set_initial_match_selection method. This method does not set the global context item for the transformation; if that is required, it can be done separately using the set_global_context_item method.

Parameters:

**kwds – Possible keyword arguments: source_file (str) or xdm_value (PyXdmValue) can be used to supply the initial match selection; and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Returns:

Result of the transformation as a PyXdmValue object

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

xslt30_proc = saxon_proc.new_xslt30_processor()
executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

executable.set_initial_match_selection(file_name="cat.xml")
result = executable.apply_templates_returning_value()

2):

result = executable.apply_templates_returning_value(source_file="cat.xml")
call_function_returning_file(function_name, args, **kwargs)

Invoke by calling a named function with the result saved to file

Invoke a transformation by calling a named function with the result saved to file. It is possible to specify the output file as an argument or using the set_output_file method.

Parameters:
  • function_name – The name of the function to invoke, in clark notation {uri}local

  • args (list[PyXdmValue]) – Pointer array of PyXdmValue objects - the values of the arguments to be supplied to the function.

  • **kwargs – Possible keyword arguments: output_file (str) and base_output_uri (str)

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test2.xsl")

1):

executable.set_output_file("result.xml")
executable.call_function_returning_file("{http://localhost/example}func", [])

2):

executable.set_global_context_item(file_name="cat.xml")
executable.call_function_returning_file("{http://localhost/test}add", [saxonproc.make_integer_value(2)], output_file="result.xml")
call_function_returning_string(function_name, args, **kwargs)

Invoke by calling a named function and return the result as a serialized string

Invoke a transformation by calling a named function and return the result as a serialized string.

Parameters:
  • function_name (str) – The name of the function to invoke, in clark notation {uri}local

  • args (list[PyXdmValue]) – Pointer array of PyXdmValue objects - the values of the arguments to be supplied to the function.

  • **kwargs – Possible keyword arguments: base_output_uri (str). Also accept the keyword ‘encoding’ (str) to specify the encoding of the output string. This must match the encoding specified by xsl:output in the stylesheet, or as specified using set_property() on this PyXsltExecutable. If not specified then the platform default encoding is used.

Returns:

Result of the transformation as a str value

Return type:

str

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.call_function_returning_string("{http://localhost/example}func", [])
2)::

executable.set_global_context_item(file_name=”cat.xml”) result = executable.call_function_returning_string(“{http://localhost/test}add”, [saxonproc.make_integer_value(2)])

call_function_returning_value(function_name: str, arguments: list[PyXdmValue], **kwargs)

Invoke by calling a named function and return the result as a PyXdmValue

Invoke a transformation by calling a named function and return the result as a PyXdmValue.

Parameters:
  • function_name – The name of the function to invoke, in clark notation {uri}local

  • arguments – Pointer array of PyXdmValue objects - the values of the arguments to be supplied to the function.

  • **kwargs – Possible keyword arguments

Returns:

Result of the transformation as a PyXdmValue object

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.call_function_returning_value("{http://localhost/example}func", [])

2):

executable.set_global_context_item(file_name="cat.xml")
result = executable.call_function_returning_value("{http://localhost/test}add", [saxonproc.make_integer_value(2)])
call_template_returning_file(template_name=None, **kwds)

Invoke by calling a named template with the result saved to file

Invoke a transformation by calling a named template with the result saved to file. It is possible to specify the output file as an argument or using the set_output_file method.

Parameters:
  • template_name (str) – The name of the template to invoke. If None is supplied then call the initial-template.

  • **kwds – Possible keyword arguments: output_file (str), and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

executable.call_template_returning_file("main", output_file="result.xml")

2):

executable.set_global_context_item(file_name="cat.xml")
executable.call_template_returning_file("main", output_file="result.xml")

3):

executable.set_global_context_item(file_name="cat.xml")
executable.set_output_file("result.xml")
executable.call_template_returning_file()
print(result)
call_template_returning_string(template_name=None, **kwds)

Invoke by calling a named template and return the result as a string.

Invoke a transformation by calling a named template and return the result as a string.

Parameters:
  • template_name (str) – The name of the template to invoke. If None is supplied then call the initial-template.

  • **kwds – Possible keyword arguments: base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction. Also accept the keyword ‘encoding’ (str) to specify the encoding of the output string. This must match the encoding specified by xsl:output in the stylesheet, or as specified using set_property() on this PyXsltExecutable. If not specified then the platform default encoding is used.

Returns:

Result of the transformation as a PyXdmValue object

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.call_template_returning_string("main")

2):

executable.set_global_context_item(file_name="cat.xml")
result = executable.call_template_returning_string("main")

3):

executable.set_global_context_item(file_name="cat.xml")
result = executable.call_template_returning_string()
print(result)
call_template_returning_value(template_name=None, **kwds)

Invoke by calling a named template and return the result as a PyXdmValue

Invoke a transformation by calling a named template and return the result as a PyXdmValue.

Parameters:
  • template_name (str) – The name of the template to invoke. If None is supplied then call the initial-template.

  • **kwds – Possible keyword arguments: base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Returns:

Result of the transformation as a PyXdmValue object

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

xslt30_proc = saxon_proc.new_xslt30_processor()
executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.call_template_returning_value("main")

2):

executable.set_global_context_item(file_name="cat.xml")
result = executable.call_template_returning_value("main")
clear_parameters()

Clear all parameters set on the processor for execution of the stylesheet

clear_properties()

Clear all properties set on the processor

clear_xsl_messages()

Clear the messages written using <xsl:message>

Clear the messages written using the xsl:message instruction to the in-memory list. If xsl:messages are enabled to write to file then this method has no affect

clone()

Create a clone object of this PyXsltExecutable object

Returns:

copy of this object

Return type:

PyXsltExecutable

cwd

Property represents the current working directory

export_stylesheet(file_name)

Export a representation of the compiled stylesheet

Produce a representation of the compiled stylesheet, in XML form, suitable for distribution and reloading.

Parameters:

file_name (str) – The name of the file where the compiled stylesheet is to be saved

get_initial_template_parameters(tunnel)

Returns the current initial template parameters

Returns the currently set dictionary of either tunneling or non-tunneling initial template parameters. Any changes you make to the dictionary WILL NOT be reflected in the processor. This is a read-only view of the parameters.

Parameters:

tunnel (bool) – True if these values are to be used for setting tunnel parameters; False if they are to be used for non-tunnel parameters. The default is false.

Returns:

(dict) the parameters dictionary

get_parameter(name)

Get a parameter value by a given name

Parameters:

name (str) – The name of the stylesheet parameter

Returns:

The XDM value of the parameter

Return type:

PyXdmValue

get_result_documents()

Return secondary result documents from a transformation

Return the secondary result documents resulting from the execution of the stylesheet. Null is returned if the user has not enabled this feature via the method set_capture_result_documents.

Returns:

Dict of the key-value pairs. Indexed by the absolute URI of each result document, and the corresponding value is a PyXdmValue object containing the result document (as an in-memory tree, without serialization).

Return type:

dict [str, PyXdmValue]

get_xsl_messages()

Get the messages written using <xsl:message>

Get the messages written using the xsl:message instruction. Return NULL if the user has not enabled capturing of xsl:messages via the method set_save_xsl_message().

Returns:

The messages

Return type:

PyXdmValue

remove_parameter(name)

Remove the parameter given by name from the PyXslt30Processor

Remove the parameter given by name from the PyXslt30Processor. The parameter will not have any effect on the stylesheet if it has not yet been executed.

Parameters:

name (str) – The name of the stylesheet parameter

Returns:

True if the removal of the parameter has been successful, False otherwise.

Return type:

bool

set_base_output_uri(base_uri)

Set the base output URI

Set the base output URI. The default is the base URI of the principal output of the transformation. If a base output URI is supplied using this function then it takes precedence over any base URI defined in the principal output, and it may cause the base URI of the principal output to be modified in situ. The base output URI is used for resolving relative URIs in the ‘href’ attribute of the xsl:result-document instruction; it is accessible to XSLT stylesheet code using the XPath current-output-uri() function.

Parameters:

base_uri (str) – the base output URI

set_capture_result_documents(value, raw_result=False)

Enable the capture of the secondary result documents output into a dict

Enable the capture of the result document output into a dict. This overrides the default mechanism. If this option is enabled, then any document created using xsl:result-document is saved (as a PyXdmNode) in a dict object where it is accessible using the URI as a key. After the execution of the transformation a call on the get_result_documents method is required to get access to the result documents in the map. It is also possible to capture the result document as a raw result directly as a PyXdmValue, without constructing an XML tree, and without serialization. It corresponds to the serialization.

Parameters:
  • value (bool) – true causes secondary result documents from the transformation to be saved in a map; false disables this option.

  • raw_result (bool) – true enables the handling of raw destination for result documents. If not supplied this can also be set on the set_result_as_raw_value method. The set_result_as_raw_value method has higher priority to this flag.

set_cwd(cwd)

Set the current working directory.

Parameters:

cwd (str) – current working directory

set_global_context_item(**kwds)

Set the global context item for the transformation.

Parameters:

**kwds – Possible keyword arguments: must be one of the following (file_name|xdm_item)

Raises:

Exception – Exception is raised if keyword argument is not one of file_name or xdm_item (providing a PyXdmItem).

set_initial_match_selection(**kwds)

The initial value to which templates are to be applied

The initial value to which templates are to be applied (equivalent to the select attribute of xsl:apply-templates).

Parameters:

**kwds – Possible keyword arguments: must be one of the following (file_name|xdm_value)

Raises:

Exception – Exception is raised if keyword argument is not one of file_name or xdm_value (providing a PyXdmValue).

set_initial_mode(name)

Set the initial mode for the transformation

Parameters:

name (str) – the EQName of the initial mode. Two special values are recognized, in the reserved XSLT namespace: xsl:unnamed to indicate the mode with no name, and xsl:default to indicate the mode defined in the stylesheet header as the default mode. The value null also indicates the default mode (which defaults to the unnamed mode, but can be set differently in an XSLT 3.0 stylesheet).

set_initial_template_parameters(tunnel, parameters)

Set parameters to be passed to the initial template

Set parameters to be passed to the initial template. These are used whether the transformation is invoked by applying templates to an initial source item, or by invoking a named template. The parameters in question are the xsl:param elements appearing as children of the xsl:template element.

Parameters:
  • tunnel (bool) – True if these values are to be used for setting tunnel parameters; False if they are to be used for non-tunnel parameters. The default is false.

  • parameters (dict) – the parameters to be used for the initial template supplied as key-value pairs.

Example

1):

paramArr = {'a':saxonproc.make_integer_value(12), 'b': saxonproc.make_integer_value(5)}
xsltproc.set_initial_template_parameters(False, paramArr)

2):

set_initial_template_parameters(False, {a: saxonproc.make_integer_value(12)})
set_output_file(output_file)

Set the output file where the output of the transformation will be sent

Parameters:

output_file (str) – The output file supplied as a string

set_parameter(name, value)

Set the value of a stylesheet parameter

Parameters:
  • name (str) – the name of the stylesheet parameter, as a string. For a namespaced parameter use clark notation "{uri}local"

  • value (PyXdmValue) – the value of the stylesheet parameter

set_property(name, value)

Set a property specific to the processor in use.

Parameters:
  • name (str) – The name of the property

  • value (str) – The value of the property

Example

PyXsltExecutable: set serialization properties (names start with ‘!’ e.g. name “!method” -> “xml”) ‘o’: output file name, ‘it’: initial template, ‘im’: initial mode, ‘s’: source as file name ‘m’: switch on message listener for xsl:message instructions, ‘item’| ‘node’: source supplied as a PyXdmNode object, ‘extc’: Set the native library to use with Saxon for extension functions written in C/C++/PHP

set_result_as_raw_value(is_raw)

Control whether results are wrapped in a document() node or as raw XDM value

Set true if the return type of callTemplate, applyTemplates and transform methods is to return PyXdmValue, otherwise return PyXdmNode object with root Document node

Parameters:

is_raw (bool) – True if returning raw result, i.e. PyXdmValue, otherwise return PyXdmNode

set_save_xsl_message(show, file_name=None)

Switch the xsl:message feature on or off

Gives users the option to switch the xsl:message feature on or off. It is also possible to send the xsl:message outputs to file given by file name.

Parameters:
  • show (bool) – Boolean to indicate if xsl:message should be outputted. Default (True) is on.

  • file_name (str) – The name of the file to send output

transform_to_file(**kwds)

Execute a transformation with the result saved to a file

Execute a transformation with the result saved to file. It is possible to specify the output file as an argument or using the set_output_file method.

Parameters:

**kwds – Possible keyword arguments: source_file (str) or xdm_node (PyXdmNode); output_file (str), and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

executable.transform_to_file(source_file="cat.xml", output_file="result.xml")

2):

executable.set_initial_match_selection("cat.xml")
executable.set_output_file("result.xml")
executable.transform_to_file()

3):

node = saxon_proc.parse_xml(xml_text="<in/>")
executable.transform_to_file(output_file="result.xml", xdm_node= node)
transform_to_string(**kwds)

Execute a transformation and return the result as a string.

Parameters:

**kwds – Possible keyword arguments: one of source_file (str) or xdm_node (PyXdmNode); base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction. Also accept the keyword ‘encoding’ (str) to specify the encoding of the output string. This must match the encoding specified by xsl:output in the stylesheet, or as specified using set_property() on this PyXsltExecutable. If not specified then the platform default encoding is used.

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.transform_to_string(source_file="cat.xml")

2):

executable.set_initial_match_selection(file_name="cat.xml")
result = executable.transform_to_string()

3):

node = saxon_proc.parse_xml(xml_text="<in/>")
result = executable.transform_to_string(xdm_node= node)
transform_to_value(**kwds)

Execute a transformation and return the result as a PyXdmValue object.

Parameters:

**kwds – Possible keyword arguments: source_file (str) or xdm_node (PyXdmNode); and base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.

Returns:

Result of the transformation as a PyXdmValue object

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error raised in the event of a dynamic error

Example

xslt30_proc = saxon_proc.new_xslt30_processor()
executable = xslt30_proc.compile_stylesheet(stylesheet_file="test1.xsl")

1):

result = executable.transform_to_value(source_file="cat.xml")

2):

executable.set_initial_match_selection("cat.xml")
result = executable.transform_to_value()

3):

node = saxon_proc.parse_xml(xml_text="<in/>")
result = executable.transform_to_value(xdm_node= node)