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.
This class has no public constructor. To construct a PyXslt30Processor, use the factory method
PySaxonProcessor.new_xslt30_processor().- 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
saveandoutput_file.One (and only one) of
stylesheet_text,stylesheet_file,stylesheet_node, orassociated_filemust be provided to pass the stylesheet to be compiled in.- Keyword Arguments:
stylesheet_file (str) – The path to the stylesheet.
stylesheet_text (str) – The stylesheet as a string.
stylesheet_node (PyXdmNode) – The stylesheet as an already-parsed XML node.
associated_file (str) – The path to an XML file which references the stylesheet to compile using the <?xml-stylesheet … ?> processing instruction.
encoding (str) – The encoding of the string passed if stylesheet_text is used. If not specified, the platform default encoding is used.
lang (str) – The XSLT/XPath language level to be supported by the processor (possible values: ‘3.0’ and ‘4.0’).
fast_compile (bool) – Request fast compilation of the stylesheet.
save (bool) – If true, generate an exported stylesheet (SEF) file .
output_file (str) – When generating an SEF, write it to this path.
target (str) – When generating an SEF, set the target Saxon edition the SEF will be executed with.
relocate (bool) – When generating an SEF, set whether it can be deployed to a different location, with a different base URI.
- Returns:
The compiled stylesheet. The PyXsltExecutable is not thread-safe; so to run it in multiple transformations, in series or concurrently, you must clone the object for each thread using the
clone()method.- Return type:
- Raises:
PySaxonApiError – Error raised if the stylesheet contains static errors or if it cannot be read.
Examples
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.
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_stylesheet()method to throw an exception (errors in code that is actually executed will still be notified but this may happen after thecompile_stylesheet()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.
- Parameters:
jit (bool) –
Trueif just-in-time compilation is to be enabled.
- 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
Noneto clear a previously set valueencoding (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 aPyXsltExecutable, and use the methods of that class.- Keyword Arguments:
stylesheet_file (str) – The path to the stylesheet file to compile and run.
source_file (str) – The path to the source file to transform.
output_file (str) – The path to write the transformation result to.
base_output_uri (str) – The URI to be used for resolving relative URIs in the href attribute of the
<xsl:result-document>instruction.
The keyword arguments
stylesheet_file,source_file, andoutput_fileare required.- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation
Example
xsltproc.transform_to_file(source_file="cat.xml", stylesheet_file="test1.xsl", output_file="result.xml")
- 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 aPyXsltExecutable, and use the methods of that class.- Keyword Arguments:
stylesheet_file (str) – The path to the stylesheet file to compile and run
source_file (str) – The path to the source file to transform.
base_output_uri (str) – The URI to be used for resolving relative URIs in the href attribute of the
<xsl:result-document>instruction.encoding (str) – 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.
The keyword arguments stylesheet_file and
source_fileare required.- Returns:
The result of the transformation.
- Return type:
- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation
Example
result = xsltproc.transform_to_string(source_file="cat.xml", stylesheet_file="test1.xsl")
- 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 aPyXsltExecutable, and use the methods of that class.- Keyword Arguments:
The keyword arguments
stylesheet_fileandsource_fileare required.- Returns:
Result of the transformation as a PyXdmValue object
- Return type:
- Raises:
PySaxonApiError – Error raised if failure in XSLT transformation
Example
result = xsltproc.transform_to_value(source_file="cat.xml", stylesheet_file="test1.xsl")
- use_schema(schema: PyXsdSchema)¶