July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Jürgen Hermann
You need to process XML documents and access external documents (e.g., stylesheets), but you can’t use filesystem paths (to keep documents portable) or Internet-accessible URLs (for performance and security).
4Suite’s
xml.xslt package (http://www.4suite.org/) gives you all the
power you need to handle XML stylesheets, including the hooks for
sophisticated needs such as those met by this recipe:
# uses 4Suite Version 0.10.2 or later
from xml.xslt.Processor import Processor
from xml.xslt.StylesheetReader import StylesheetReader
class StylesheetFromDict(StylesheetReader):
"A stylesheet reader that loads XSLT stylesheets from a python dictionary"
def _ _init_ _(self, styles, *args):
"Remember the dict we want to load the stylesheets from"
StylesheetReader._ _init_ _(self, *args)
self.styles = styles
self._ _myargs = args
def _ _getinitargs_ _(self):
"Return init args for clone( )"
return (self.styles,) + self._ _myargs
def fromUri(self, uri, baseUri='', ownerDoc=None, stripElements=None):
"Load stylesheet from a dict"
parts = uri.split(':', 1)
if parts[0] == 'internal' and self.styles.has_key(parts[1]):
# Load the stylesheet from the internal repository (your dictionary)
return StylesheetReader.fromString(self, self.styles[parts[1]],
baseUri, ownerDoc, stripElements) else: # Revert to normal behavior return StylesheetReader.fromUri(self, uri, baseUri, ownerDoc, stripElements) if _ _name_ _ == "_ _main_ _": # test ...Read now
Unlock full access