def transform(source, transforms, params=None, output=None): """ Convenience function for applying an XSLT transform. Returns a result object. source - XML source document in the form of a string (not Unicode object), file-like object (stream), file path, URI or amara.lib.inputsource instance. If string or stream it must be self-contained XML (i.e. not requiring access to any other resource such as external entities or includes) transforms - XSLT document (or list thereof) in the form of a string, stream, URL, file path or amara.lib.inputsource instance params - optional dictionary of stylesheet parameters, the keys of which may be given as unicode objects if they have no namespace, or as (uri, localname) tuples if they do. output - optional file-like object to which output is written (incrementally, as processed) """ #do the imports within the function: a tad bit less efficient, but #avoid circular crap from amara.lib import inputsource from amara.xpath.util import parameterize from amara.xslt.result import streamresult, stringresult from amara.xslt.processor import processor params = parameterize(params) if params else {} proc = processor() if isinstance(transforms, (list, tuple)): for transform in transforms: proc.append_transform(inputsource(transform)) else: proc.append_transform(inputsource(transforms)) if output is not None: result = streamresult(output) else: result = stringresult() return proc.run(inputsource(source), params, result)
def test_processor(self): P = processor() for transform in self.transform: P.append_transform(transform) parameters = self.parameters if parameters: parameters = util.parameterize(parameters) result = P.run(self.source, parameters=parameters) self._assert_result(result)
def _run(source_xml, transform_xml, expected, parameters, compare_method, source_uri=None, transform_uri=None, processor_kwargs={}): P = processor(**processor_kwargs) source = inputsource(source_xml, source_uri) transform = inputsource(transform_xml, transform_uri) P.append_transform(transform) if parameters is not None: parameters = util.parameterize(parameters) result = str(P.run(source, parameters=parameters)) try: diff = compare_method(result, expected) diff = list(diff) assert not diff, (source_xml, transform_xml, result, expected, diff) except Exception, err: # I don't have a quick way to tell which string caused # the error, so let the person debugging figure it out. print "=== RESULT ===" print result print "=== EXPECTED ===" print expected print "=== DONE ===" raise