PyXslt30Processor¶
- class PyXslt30Processor¶
Bases:
objectA factory to compile, load and execute stylesheets.
A 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
- Parameters:
processor (PySaxonProcessor)
- __init__()¶
Creates a new PyXslt30Processor, keeping a reference to its PySaxonProcessor
- Parameters:
processor (PySaxonProcessor)
- classmethod __new__(*args, **kwargs)¶
- __reduce__()¶
Helper for pickle.
- __setstate__()¶
- clear_parameters()¶
Clear all parameters set on the processor for execution of the stylesheet
- compile_stylesheet(**kwds)¶
Compile a stylesheet
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’.
- Parameters:
**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.
- Exampls:
Create a processor:
xsltproc = saxon_proc.new_xslt30_processor()
Compile a stylesheet from an XSLT string:
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>")
Compile a stylesheet from a file, and exporting it to a SEF file:
executable = xsltproc.compile_stylesheet(stylesheet_file="test1.xsl", save=True, output_file="test1.sef", target="HE")
Compiling the stylesheet found through the <?xml-stylesheet?> Processing Instruction in an XML file:
executable = xsltproc.compile_stylesheet(associated_file="foo.xml")
- cwd¶
Property represents the current working directory
- get_jit_compilation()¶
Get the flag set for just-in-time compilation of template rules.
- Returns:
True if just-in-time compilation of template rules is set
- Return type:
- get_parameter(name, encoding=None)¶
Get a parameter value by a given name
- Parameters:
- Returns:
The XDM value of the parameter
- Return type:
- import_package(package_file_name)¶
Import a library package.
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.
- Parameters:
package_file_name (str) – 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”)
- is_schema_aware()¶
- remove_parameter(name, encoding=None)¶
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.
- set_jit_compilation(jit)¶
Say whether just-in-time compilation of template rules should be used.
- Parameters:
jit (bool) –
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(name, value, encoding=None)¶
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, 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.
- set_schema_aware(schema_aware)¶
- transform_to_file(**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.
- Parameters:
**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(**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.
- Parameters:
**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(**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.
- Parameters:
**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
- use_schema(schema: PyXsdSchema)¶