On 31/05/2010 20:18, Asplund Marko wrote:
Hi,

Thanks for the feedback.
I'm doing XML-to-XML transformations with Saxon and the documents contain HTML fragments.
The HTML fragments need to be included in the result documents with HTML tags escaped.
The extension function was written to take an XPath sequence as argument and return it as a string.
HTMLEmitter is used as a receiver in the ComplexContentOutputter.
I'm a novice with Saxon so, if there's a better way to do this any pointers would be appreciated.

best regards,

marko
It looks to me as if the saxon:serialize() extension function does what you want. In fact, you are probably trying to replicate it.

If for some reason saxon:serialize() doesn't meet your needs, then the right way to write your extension function might be by calling new SerializerFactory().getReceiver() to get a suitable Receiver: you can subclass SerializerFactory to override some of its behaviours if you want.

The core of saxon:serialize, excluding logic concerned with serialization properties, validation, error handling etc is:

            NodeInfo node = (NodeInfo)arguments[0].next();
            StringWriter result = new StringWriter();
            SerializerFactory sf = context.getConfiguration().getSerializerFactory();
            PipelineConfiguration pipe = context.getController().makePipelineConfiguration();
            Receiver receiver = sf.getReceiver(new StreamResult(result), pipe, props);
            receiver.open();
            node.copy(receiver, (CopyOptions.ALL_NAMESPACES|CopyOptions.TYPE_ANNOTATIONS), 0);
            receiver.close();
            return SingletonIterator.makeIterator(new StringValue(result.toString()));

Using the ComplexContentOutputter seems to me rather low-level.