Transform¶
Apply an XSLT 1.0 stylesheet to a document, the job lxml’s etree.XSLT does. A stylesheet is an
XML document, so it is read with turbohtml.parse_xml(); Transform holds the parsed stylesheet and is
callable over source documents, the compile-once, apply-many shape. The whole transformation runs in the C extension,
reusing turbohtml’s XPath 1.0 engine for every match pattern and select expression rather than growing a second path
evaluator.
- class turbohtml.transform.Transform(stylesheet, *, base_url=None)[source]¶
A compiled XSLT 1.0 stylesheet, callable over source documents (lxml’s
etree.XSLT).- Parameters:
stylesheet (
Node) – the stylesheet, a tree parsed withturbohtml.parse_xml().base_url (
str|None) – the stylesheet’s path or file URL, against whichxsl:importhrefs resolve; required only when the stylesheet imports.
- __call__(source, /, **params)[source]¶
Transform a source document and return the serialized result.
- Parameters:
- Raises:
ValueError – if the stylesheet or an expression is malformed, or a referenced key or named template is undeclared.
RuntimeError – on an
xsl:messagewithterminate="yes".
- Return type:
- Returns:
the transformed document serialized under the stylesheet’s
xsl:outputmethod.
- turbohtml.transform.transform(stylesheet, source, /, *, base_url=None, **params)[source]¶
Apply an XSLT 1.0 stylesheet to a source document in one call.
Equivalent to
Transform(stylesheet, base_url=base_url)(source, **params); useTransformto apply one stylesheet to many documents without re-reading it each time.- Parameters:
stylesheet (
Node) – the stylesheet, a tree parsed withturbohtml.parse_xml().source (
Node) – the document to transform, a parsed tree.base_url (
str|None) – the stylesheet’s path or file URL, against whichxsl:importhrefs resolve; required only when the stylesheet imports.params (
str) – top-levelxsl:paramvalues, each an XPath expression string.
- Return type:
- Returns:
the transformed document serialized under the stylesheet’s
xsl:outputmethod.
The engine covers the XSLT 1.0 core: xsl:template (match, name, mode, priority),
xsl:apply-templates (select, mode, xsl:sort, xsl:with-param), xsl:call-template,
xsl:for-each, xsl:if, xsl:choose/xsl:when/xsl:otherwise, xsl:value-of, xsl:copy and
xsl:copy-of, xsl:element/xsl:attribute/xsl:text/xsl:comment/xsl:processing-instruction,
xsl:variable/xsl:param (local and top-level), xsl:sort (data-type, order), xsl:number
(value, format), xsl:key with the key() function, the built-in template rules, and the section 5.5
conflict resolution by priority then document order. It emits the xml, html, and text output methods, and
adds the XSLT functions current(), key(), generate-id(), format-number(), system-property(),
function-available(), and element-available().
External-document loading is limited. xsl:import resolves local paths and file URLs against base_url; the
imported declarations join conflict resolution at lower import precedence. xsl:include and document() do not
resolve, and document() returns an empty node-set.