SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
The XPath API
Standards Conformance
Extensibility
Saxon Extensions
| Introduction
| EXSLT Extensions
| Extension attributes (XSLT only)
| Additional serialization parameters
» | Extension functions
| Extension instructions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
XPath 2.0 Expression Syntax
Function Library
XSLT Patterns

# saxon:transform()

transform(jt:javax.xml.transform.Templates, $source as node()) ==> document-node()

This function takes as input a compiled XSLT stylesheet, and uses it to transform a source document into a result document. The first argument will generally be the result of calling the saxon:compile-stylesheet() extension function. The second argument can be any node (usually a document node or element node), either read from external filestore using the `doc()` or `document()` function, or constructed programmatically.

The function is available both in XQuery and in XSLT. It can thus be used for example in XQuery to pre-process the input using a stylesheet, or to post-process the output using a stylesheet. It can also be used to chain multiple XSLT transformations together.

The compiled stylesheet can be used repeatedly to transform multiple source documents.

The stylesheet may contain calls on `xsl:result-document` This allows the output of the stylesheet to be serialized directly to filestore rather than being returned to the calling transformation or query.

Here is an example of how to use the function from XQuery:

    let $results :=
      <customers>{ //customer[location="Scotland"] }</customer>
    let $rendition := saxon:compile-stylesheet(doc('show-customers.xsl'))
    return saxon:transform($rendition, $results)

The following example uses XSLT's ability to create multiple output files:

    let $splitter := saxon:compile-stylesheet(
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                           version="2.0">
        <xsl:template match="customer">
          <xsl:result-document href="{{@id}}.xml">
            <xsl:copy-of select="."/>
          </xsl:result-document>
        </xsl:template>
      </xsl:stylesheet>)
    let $results :=
      document {
         <customers>{ //customer[location="Scotland"] }</customers>
      }
    return saxon:transform($splitter, $results)

Note (a) the need to double the curly braces to ensure that the contained expression is expanded when the stylesheet is executed by XSLT, not when it is created by XQuery, and (b) the fact that the stylesheet and source document are supplied as document nodes, not element nodes.

Next