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 :param processor:

__init__()

Creates a new instance, keeping a reference to its PySaxonProcessor :param processor:

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

Helper for pickle.

__setstate__()
apply_templates_returning_file(self, **kwds)

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. :param **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(self, **kwds)

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. :param **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. 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)

  1. node = saxon_proc.parse_xml(xml_text=”<in/>”) content = executable.apply_templates_returning_string(xdm_value=node)

    print(content)

apply_templates_returning_value(self, **kwds)

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. :param **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()

  1. result = executable.apply_templates_returning_value(source_file=”cat.xml”)

call_function_returning_file(self, str function_name, list args, **kwds)

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. :param function_name: The name of the function to invoke, in clark notation {uri}local :type function_name: str :param args: Pointer array of PyXdmValue objects - the values of the arguments to be supplied

to the function.

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”, [])

  1. 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(self, str function_name, list args, **kwds)

Invoke a transformation by calling a named function and return the result as a serialized string. :param function_name: The name of the function to invoke, in clark notation {uri}local :type function_name: str :param args: Pointer array of PyXdmValue objects - the values of the arguments to be supplied

to the function.

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(self, str function_name, list args, **kwds)

Invoke a transformation by calling a named function and return the result as a PyXdmValue. :param function_name: The name of the function to invoke, in clark notation {uri}local :type function_name: str :param args: Pointer array of PyXdmValue objects - the values of the arguments to be supplied

to the function.

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(self, str template_name, **kwds)

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. :param template_name: The name of the template to invoke. If None is supplied then call the initial-template. :type template_name: str

**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”)

  1. 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(self, str template_name, **kwds)

Invoke a transformation by calling a named template and return the result as a string. :param template_name: The name of the template to invoke. If None is supplied then call the initial-template. :type template_name: str

**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”)

  1. executable.set_global_context_item(file_name=”cat.xml”) result = executable.call_template_returning_string()

    print(result)

call_template_returning_value(self, str template_name, **kwds)

Invoke a transformation by calling a named template and return the result as a PyXdmValue. :param template_name: The name of the template to invoke. If None is supplied then call the initial-template. :type template_name: str

**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_parameter(self) Clear all parameters set on the processor for execution of the stylesheet

clear_properties(self)

Clear all properties set on the processor

clear_xsl_messages(self)

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(self)

Create a clone object of this PyXsltExecutable object :returns: copy of this object :rtype: PyXsltExecutable

cwd

Property represents the current working directory

error_message

from 12.1 this method is no longer used, since exceptions are now thrown. A transformation may have a number of errors reported against it. Get the error message if there are any errors :returns: The message of the exception. Returns None if the exception does not exist. :rtype: str

Type:

Deprecated

exception_clear(self)

Deprecated: from 12.1 this method is no longer used, since exceptions are now thrown. Clear any exception thrown

exception_occurred

from 12.1 this method is no longer used, since exceptions are now thrown. Property to check for pending exceptions without creating a local reference to the exception object :returns: True when there is a pending exception; otherwise False :rtype: boolean

Type:

Deprecated

export_stylesheet(self, str file_name)

Produce a representation of the compiled stylesheet, in XML form, suitable for distribution and reloading. :param file_name: The name of the file where the compiled stylesheet is to be saved :type file_name: str

get_initial_template_parameters(tunnel)

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(self, name)

Get a parameter value by a given name :param name: The name of the stylesheet parameter :type name: str

Returns:

The XDM value of the parameter

Return type:

PyXdmValue

get_result_documents(self)

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(self)

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:

value (PyXdmValue):

remove_parameter(self, name)

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. :param name: The name of the stylesheet parameter :type name: str

Returns:

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

Return type:

bool

set_base_output_uri(self, base_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. :param base_uri: the base output URI :type base_uri: str

set_capture_result_documents(self, bool value, bool raw_result)

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. :param value: true causes secondary result documents from the transformation to be saved in a map;

false disables this option.

Parameters:

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(self, cwd)

Set the current working directory. :param cwd: current working directory :type cwd: str

set_global_context_item(self, **kwds)

Set the global context item for the transformation. :param **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(self, **kwds)

The initial value to which templates are to be applied (equivalent to the select attribute of xsl:apply-templates). :param **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(self, name)

Set the initial mode for the transformation :param name: 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(self, bool tunnel, dict parameters)

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. :param tunnel: 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.

Example

  1. paramArr = {‘a’:saxonproc.make_integer_value(12), ‘b’:saxonproc.make_integer_value(5)}

    xsltproc.set_initial_template_parameters(False, paramArr)

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

set_output_file(self, output_file)

Set the output file where the output of the transformation will be sent :param output_file: The output file supplied as a string :type output_file: str

set_parameter(self, name, value)

Set the value of a stylesheet parameter :param name: the name of the stylesheet parameter, as a string. For a namespaced parameter use

clark notation {uri}local

Parameters:

value (PyXdmValue) – the value of the stylesheet parameter

set_property(self, name, value)

Set a property specific to the processor in use. :param name: The name of the property :type name: str :param value: The value of the property :type value: str

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(self, bool is_raw)

Set true if the return type of callTemplate, applyTemplates and transform methods is to return PyXdmValue, otherwise return PyXdmNode object with root Document node :param is_raw: True if returning raw result, i.e. PyXdmValue, otherwise return PyXdmNode :type is_raw: bool

set_save_xsl_message(self, show, str file_name)

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. :param show: Boolean to indicate if xsl:message should be outputted. Default (True) is on. :type show: bool :param file_name: The name of the file to send output :type file_name: str

transform_to_file(self, **kwds)

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. :param **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()

  1. node = saxon_proc.parse_xml(xml_text=”<in/>”) executable.transform_to_file(output_file=”result.xml”, xdm_node= node)

transform_to_string(self, **kwds)

Execute a transformation and return the result as a string. :param **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()

  1. node = saxon_proc.parse_xml(xml_text=”<in/>”) result = executable.transform_to_string(xdm_node= node)

transform_to_value(self, **kwds)

Execute a transformation and return the result as a PyXdmValue object. :param **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()

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