Classes and methods in net.sf.saxon.instruct.* aren't designed to be called by user applications. If you can get this working, fine, but it's not designed to be used in this way and I can't help you with it.
As far as I can see, what you are doing can be done perfectly well in Saxon 8.9 using stylesheet functions written in XSLT (perhaps with the addition of a call on saxon:evaluate()). Something like this (though I'm not sure exactly what you are trying to achieve):
<xsl:function name="my:apply" as="node()">
  <xsl:param name="uri" as="xs:anyURI"/>
  <xsl:param name="path" as="xs:string"/>
  <xsl:apply-templates select="document($uri)/saxon:evaluate($path)"/>
Michael Kay

From: [] On Behalf Of LindsaySmith
Sent: 28 November 2007 18:27
Subject: [saxon] Calling ApplyTemplates on a different document from an Extension Function

I'm try to write an ExtensionInstruction for Saxon 8.9. When the stylesheet encounters my custom element, I would like to call applyTemplates on a completely different document, where the output is inserted in the current result document. Everything is going fine, apart from actually calling apply-templates. The code i have is:
   sax - the SAXSource for my other document
   path  - the XPath to call applyTemplates on in that document
   context - the context object passed into the 'process' method of my SimpleExpression
   styleElement  - my ExtensionInstruction
   compiledExpression - the SimpleExpression returned by 'compile' on the ExtensionInstruction
public void applyTemplatesToSAXSource(SAXSource sax, 
                                                      String path, 
                                                      XPathContext context, 
                                                      StyleElement styleElement, 
                                                      Expression compiledExpression) throws TransformerException {
	DocumentInfo doc = createSAXSource(sax);
	XPathContextMajor ctx = new XPathContextMajor(doc, styleElement.getExecutable());
	Expression selectExpression = styleElement.makeExpression(path);

	net.sf.saxon.instruct.ApplyTemplates a = new net.sf.saxon.instruct.ApplyTemplates(selectExpression, 

      Create a DocumentInfo from a SAXSource
    private DocumentInfo createSAXSource(Source s) throws TransformerException {

	net.sf.saxon.event.Builder sourceBuilder = this.controller.makeBuilder();
	Sender sender = new Sender(sourceBuilder.getPipelineConfiguration());
	Receiver r = sourceBuilder;

	sender.send(s, r);

	DocumentInfo doc = (DocumentInfo)sourceBuilder.getCurrentRoot();
	this.controller.registerDocument(doc, s.getSystemId());

	return doc;
This doesn't work however, I get the following exception when I call 'process' on the ApplyTemplates:
	at net.sf.saxon.expr.GeneralComparison10.effectiveBooleanValue(
	at net.sf.saxon.expr.FilterIterator$NonNumeric.matches(
	at net.sf.saxon.expr.FilterIterator.getNextMatchingItem(
	at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(
	at net.sf.saxon.instruct.ApplyTemplates.apply(
	at net.sf.saxon.instruct.ApplyTemplates.process(
I used to do this with Saxon 6.5.5 but now the api has changed and I can't seem to figure out the right way to set up a new context against this other document. Any help I can get is greatly appreciated. By the way, I'm using Saxon 8.9 instead of 9 becuase with version 9 I was encountering errors like the ones in this thread.


View this message in context: Calling ApplyTemplates on a different document from an Extension Function
Sent from the saxon-help mailing list archive at