Copyright 2005 Fourthought, Inc. (USA). Detailed license and copyright information: http://4suite.org/COPYRIGHT Project home, documentation, distributions: http://4suite.org/
Classes:
|
Globals:
|
Typical usage: from Ft.Lib.Uri import OsPathToUri from Ft.Xml import InputSource from Ft.Xml.Xslt import Processor # this is just one of several ways to create InputSources styuri = OsPathToUri('/absolute/path/to/stylesheet.xslt') srcuri = OsPathToUri('/absolute/path/to/doc.xml') STY = InputSource.DefaultFactory.fromUri(styuri) SRC = InputSource.DefaultFactory.fromUri(srcuri) proc = Processor.Processor() proc.appendStylesheet(STY) result = proc.run(SRC) Optional constructor arguments are: stylesheetAltUris: a list of alternative base URIs to use when resolving relative hrefs in xsl:import/xsl:include instructions. These URIs are only tried when the standard XSLT behavior of using the base URI of the xsl:import/include element itself fails to result in retrieval of a document. documentReader: an object that will be used to parse XML source documents (not stylesheets). It defaults to Ft.Xml.Domlette.NonvalidatingReader, but it can be any object that has a parse() method that returns a DOM or Domlette tree. implementation: a DOM implementation instance that will be used by the processor to create new source tree nodes, such as when generating result tree fragments or duplicating the source tree when runNode(node, preserveSrc=1) is called. Defaults to Ft.Xml.Domlette.implementation. Needs to have a createRootNode() method. See the run() and runNode() methods for additional runtime options. Important instance attributes: .extensionParams: a dictionary that allows one to attach additional metadata to a processor instance. We use this to make invocation-specific data like HTTP query args and logfile handles available to XSLT extension functions & elements when invoking the processor via the repository's HTTP server. .inputSourceFactory: InputSource factory instance used when obtaining source documents. Defaults to Ft.Xml.InputSource.DefaultFactory. .mediaPref: the preferred/target media, for the purpose of picking from multiple xml-stylesheet processing instructions. Defaults to None. If set to a string, xml-stylesheet PIs without that string in their 'media' pseudo-attribute will be ignored. .msgPrefix and .msgSuffix: strings emitted before and after xsl:message output. .stylesheet: the complete stylesheet tree.
Sets up the processor to start processing subsequently generated content with an output writer wrapper that first determines which XSLT output method is going to be used (i.e., by looking at the output parameters or waiting to see if an 'html' element is the first new node generated), then replaces itself with the appropriate writer instance. outputParams is an Ft.Xml.Xslt.OutputParameters instance. stream will be passed on to the constructor of the real writer.
This method establishes the stylesheet that the processor will use to do the transformation. If a stylesheet has already been appended, then this method is equivalent to having, in an outer "shell" stylesheet, an xsl:import for the most recently appended stylesheet followed by an xsl:import for the stylesheet accessible via the given InputSource.
This method establishes the stylesheet that the processor will use to do the transformation. If a stylesheet has already been appended, then this method is equivalent to having an xsl:import of the new stylesheet in the most recently appended stylesheet. An exception will be raised if the same stylesheet is appended more than once, just as if the same stylesheet were imported more than once. refUri is the base URI to assume for the stylesheet. It defaults to the base URI of the root node of the original stylesheet document with the highest import precedence. Note: Using the instant stylesheet tends to be less efficient than using the original document and appendStylesheet(), unless the stylesheet is large and complex, like DocBook XSL.
This method establishes the stylesheet that the processor will use to do the transformation. If a stylesheet has already been appended, then this method is equivalent to having an xsl:import of the new stylesheet in the most recently appended stylesheet. An exception will be raised if the same stylesheet is appended more than once, just as if the same stylesheet were imported more than once. refUri is the base URI to assume for the stylesheet. It defaults to the base URI of the given node. The given InputSourceFactory will be used in order to read external entities. It defaults to Ft.Xml.InputSource.DefaultFactory.
Implements the xsl:apply-templates instruction by attempting to let the stylesheet apply its own template for the given context. If the stylesheet does not have a matching template, the built-in templates are invoked. context is an XsltContext instance. params is a dictionary of parameters being passed in, defaulting to None.
Runs the stylesheet processor against the given XML DOM node with the stylesheets that have been registered. It does not mutate the source. If writer is given, it is used in place of the default output method decisions for choosing the proper writer.
Ends temporary output writing that was started with pushResultString(), pushResultTree(), or pushResult(), and returns the result.
Sets up the processor to start processing subsequently generated content with a new output writer (the given handler of SAX-like output events).
Sets up the processor to start processing subsequently generated content with an output writer that buffers the text from text events and keeps track of whether non-text events occurred. This is used by the implementations of XSLT instructions such as xsl:attribute.
Sets up the processor to start processing subsequently generated content with a new output writer that produces a separate document. The new document will have the given baseUri as its URI. This is used to generate result tree fragments. Allows specifying an alternative DOM implementation for the creation of the new document.
For example, implement your own extension element as a subclass of Ft.Xml.Xslt.XsltElement. To make the element available to the Processor, call this method, passing in the namespace URI and local name of the element, and a direct reference to the class that implements the extension. See also registerExtensionModules().
For example, implement your own extension function as a Python function that takes an Ft.Xml.XPath.Context.Context instance as its first argument. Then, to make the function available to the Processor, call this method, passing in the namespace URI and local name of the function, and a direct reference to the Python function that implements the extension. See also registerExtensionModules().
In a Python module that contains extension implementations, define a dictionary named ExtFunctions that, for each extension function or element, maps a (namespace-URI, xpath-function-name) tuple to a direct reference to the Python function that implements the extension. To make the function available to the Processor, call this method, passing in ['your.module.name']. See Ft.Xml.Xslt.Exslt.*, Ft.Xml.Xslt.BuiltInExtFunctions and BuiltInExtElements for working examples of extension modules.
Deletes the most recently added output writer.
Assumes that either the Processor instance has already had stylesheets appended (via appendStylesheet(), for example), or the source document contains xml-stylesheet processing instructions that are not being ignored. The ignorePis flag, if set, will cause xml-stylesheet processing instructions in the source document to be ignored. The topLevelParams argument is an optional dictionary of stylesheet parameters, the keys of which may be given as strings if they have no namespace, or as (uri, localname) tuples otherwise. The optional writer argument is a SAX-like event handler that is an Ft.Xml.Xslt.NullWriter subclass. The default writer is either an Ft.Xml.Xslt.XmlWriter, HtmlWriter or PlainTextWriter, depending on the stylesheet(s). The optional outputStream argument is a Python file-like object to be used as the destination for the writer's output.
Use Ft.Xml.Domlette.ConvertDocument() to create a Domlette from some other type of DOM. Assumes that either the Processor instance has already had stylesheets appended (via appendStylesheet(), for example), or the source document contains xml-stylesheet processing instructions that are not being ignored. sourceUri - The absolute URI of the document entity that the node represents, and should be explicitly provided, even if it is available from the node itself. ignorePis - (flag) If set, will cause xml-stylesheet processing instructions in the source document to be ignored. topLevelParams - optional dictionary of stylesheet parameters, the keys of which may be given as strings if they have no namespace, or as (uri, localname) tuples otherwise. writer - optional SAX-like event handler that is an Ft.Xml.Xslt.NullWriter subclass. The default writer is either an Ft.Xml.Xslt.XmlWriter, HtmlWriter or PlainTextWriter, depending on the stylesheet(s). outputStream - optional Python file-like object to be used as the destination for the writer's output. preserveSrc - (flag) If set signals that the source DOM should not be mutated, as would normally happen when honoring XSLT whitespace stripping requirements. Setting preserveSrc results in the creation of a copy of the source DOM. isrc - optional input source used strictly for further resolution relative the given DOM
The reader can be anything with a parse() interface that returns a DOM tree. It is normally Ft.Xml.Domlette.NonvalidatingReader or whatever was specified in the Processor constructor. This method is sometimes used to set the reader to Domlette.ValidatingReader.
It is normally an instance of Ft.Xml.Xslt.StylesheetReader.StylesheetReader
Used, for example, to announce that built-in templates are being invoked with params.
Used by xsl:message to emit a message to sys.stderr, unless such messages are suppressed (see messageControl()). Uses the msgPrefix & msgSuffix instance attributes.