PyXslt30Processor¶
- class PyXslt30Processor¶
Bases:
objectA PyXslt30Processor represents a factory to compile, load and execute stylesheets. It is possible to cache the context and the stylesheet in the PyXslt30Processor.
Creates a new PyXslt30Processor, keeping a reference to its PySaxonProcessor :param processor:
- __init__()¶
Creates a new PyXslt30Processor, keeping a reference to its PySaxonProcessor :param processor:
- classmethod __new__(*args, **kwargs)¶
- __reduce__()¶
Helper for pickle.
- __setstate__()¶
- clear_parameters()¶
clear_parameter(self) Clear all parameters set on the processor for execution of the stylesheet
- compile_stylesheet(self, **kwds)¶
Compile a stylesheet received as text, uri, as a node object, or as referenced in a specified XML document via the xml-stylesheet processing instruction. The term “compile” here indicates that the stylesheet is converted into an executable form. The compilation uses a snapshot of the properties of the PyXslt30Processor at the time this method is invoked. It is also possible to save the compiled stylesheet (SEF file) given the options ‘save’ and ‘output_file’. :param **kwds: Possible keyword arguments: one of stylesheet_text (str), stylesheet_file (str),
associated_file (str) or stylesheet_node (PyXdmNode); save (bool) and output_file (str) can be used to save the exported stylesheet (SEF) to file; lang (str) can be used to set the XSLT (and XPath) language level to be supported by the processor (possible values: ‘3.0’ and ‘4.0’); fast_compile (bool) which requests fast compilation. The following additional keywords can be used with ‘save’: target (str) which sets the target edition under which the compiled stylesheet will be executed; and relocate (bool) which says whether the compiled stylesheet can be deployed to a different location, with a different base URI. The keyword ‘encoding’ (str) can be used with stylesheet_text to specify the encoding used to decode the string (if not specified then the platform default encoding is used).
- Returns:
which represents the compiled stylesheet. The PyXsltExecutable is immutable and thread-safe; it may be used to run multiple transformations, in series or concurrently.
- Return type:
- Raises:
PySaxonApiError – Error raised if the stylesheet contains static errors or if it cannot be read.
Example
xsltproc = saxon_proc.new_xslt30_processor() 1) executable = xsltproc.compile_stylesheet(stylesheet_text=”<xsl:stylesheet xmlns:xsl=’http://www.w3.org/1999/XSL/Transform’ version=’2.0’>
<xsl:param name=’values’ select=’(2,3,4)’ /><xsl:output method=’xml’ indent=’yes’ /> <xsl:template match=’*’><output><xsl:value-of select=’//person[1]’/> <xsl:for-each select=’$values’ >
<out><xsl:value-of select=’. * 3’/></out>
</xsl:for-each></output></xsl:template></xsl:stylesheet>”)
executable = xsltproc.compile_stylesheet(stylesheet_file=”test1.xsl”, save=True, output_file=”test1.sef”, target=”HE”)
executable = xsltproc.compile_stylesheet(associated_file=”foo.xml”)
- cwd¶
Property represents the current working directory
- error_code¶
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. This property returns the error code if there are any errors. :returns: The error code associated with the exception. Returns None if the exception does not exist. :rtype: str
- Type:
Deprecated
- 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. This property returns an 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 an exception thrown; otherwise False :rtype: boolean
- Type:
Deprecated
- get_jit_compilation(self)¶
Get the flag set for just-in-time compilation of template rules. :returns: True if just-in-time compilation of template rules is set :rtype: bool
- get_parameter(self, name, encoding=None)¶
Get a parameter value by a given name :param name: The name of the stylesheet parameter :type name: str :param encoding: The encoding of the name string. If not specified then the platform default encoding is used. :type encoding: str
- Returns:
The XDM value of the parameter
- Return type:
- import_package(self, package_file_name)¶
Import a library package. Calling this method makes the supplied package available for reference in the xsl:use-package declarations of subsequent compilations performed using this Xslt30Processor. :param package_file_name: the file name of the package to be imported, which should be supplied
as an SEF. If relative, the file name of the SEF is resolved against the cwd, which is set using the set_cwd method.
Example
xsltproc = saxon_proc.new_xslt30_processor() xsltproc.import_package(‘test-package-001.pack’) executable = xsltproc.compile_stylesheet(stylesheet_file=”foo.xsl”)
- remove_parameter(self, name, encoding=None)¶
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 :param encoding: The encoding of the name string. If not specified then the platform default encoding is used. :type encoding: str
- Returns:
True if the removal of the parameter has been successful, False otherwise.
- Return type:
- set_cwd(self, cwd)¶
Set the current working directory. :param cwd: current working directory :type cwd: str
- set_jit_compilation(self, bool jit)¶
Say whether just-in-time compilation of template rules should be used. :param jit: True if just-in-time compilation is to be enabled. With this option enabled,
static analysis of a template rule is deferred until the first time that the template is matched. This can improve performance when many template rules are rarely used during the course of a particular transformation; however, it means that static errors in the stylesheet will not necessarily cause the compile(Source) method to throw an exception (errors in code that is actually executed will still be notified but this may happen after the compile(Source) method returns). This option is enabled by default in SaxonC-EE, and is not available in SaxonC-HE or SaxonC-PE. Recommendation: disable this option unless you are confident that the stylesheet you are compiling is error-free.
- set_parameter(self, name, PyXdmValue value, encoding=None)¶
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, or NULL to clear a previously set value
encoding (str) – The encoding of the name string. If not specified then the platform default encoding is used.
- transform_to_file(self, **kwds)¶
Execute a transformation with the result saved to file. For a more elaborate API for transformation use the compile_stylesheet method to compile the stylesheet to a PyXsltExecutable, and use the methods of that class. :param **kwds: Required keyword arguments: source_file (str), stylesheet_file (str) and output_file (str).
Possible argument: base_output_uri (str) which is used for resolving relative URIs in the href attribute of the xsl:result-document instruction.
Example
xsltproc.transform_to_file(source_file=”cat.xml”, stylesheet_file=”test1.xsl”, output_file=”result.xml”)
- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation
- transform_to_string(self, **kwds)¶
Execute a transformation and return the result as a string. For a more elaborate API for transformation use the compile_stylesheet method to compile the stylesheet to a PyXsltExecutable, and use the methods of that class. :param **kwds: Required keyword arguments: source_file (str) and stylesheet_file (str).
Possible 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. If not specified then the platform default encoding is used.
Example
result = xsltproc.transform_to_string(source_file=”cat.xml”, stylesheet_file=”test1.xsl”)
- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation
- transform_to_value(self, **kwds)¶
Execute a transformation and return the result as a PyXdmValue object. For a more elaborate API for transformation use the compile_stylesheet method to compile the stylesheet to a PyXsltExecutable, and use the methods of that class. :param **kwds: Required keyword arguments: source_file (str) and stylesheet_file (str).
Possible argument: 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:
Example
result = xsltproc.transform_to_value(source_file=”cat.xml”, stylesheet_file=”test1.xsl”)
- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation