Using XSLT 2.0

This guide explains how to use XSLT 2.0 with XmlPrime.

This topic contains the following sections.

Loading the Input Document

In this example we will be using the bstore1.example.com/bib.xml document as described in XML Query Use Cases. The document is as follows:

 
<bib>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="2000">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
 
    <book year="1999">
        <title>The Economics of Technology and Content for Digital TV</title>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation>CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
 
</bib>
 

This document is assumed to be in the file bib.xml.

To transform the document we load it into an XdmDocumentXdmDocumentXdmDocument. For more information about document representations, see Document Representations.

 
XdmDocument document = new XdmDcoument("bib.xml", XmlSpace.Presserve);
 

Compiling a Transformation

We will run the following transformation over our document, which returns the books in ascending order of price.

 
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://www.w3.org/1999/xhtml">
                
  <xsl:template match="/bib">
    <xsl:copy>
      <xsl:for-each select="book">
        <xsl:sort select="xs:decimal(price)"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
 

This document is assumed to be in the file bib.xsl.

The next stage is to compile the transformation. To describe how the transformation should be compiled we need set up an XsltSettingsXsltSettingsXsltSettings object. This describes all the settings used for compilation. In particular, we will set the name table used by the transformation to match the one we used earlier and we will set the context item type. By default the context item type is set to none, and so the context item cannot be set unless we override the type here. We can then compile the transformation using the Xslt.CompileXslt.CompileXslt::Compile method. This returns us an XsltXsltXslt object encapsulating the transformation.

 
XsltSettings xsltSettings = new XsltSettings(nameTable);
xsltSettings.ContextItemType = XdmType.Node;

Xslt xslt = Xslt.Compile("bib.xsl", xsltSettings);
 

Executing the Transformation

Now we have our transformation object we now just need to execute it. We will use the Xslt.ApplyTemplatesXslt.ApplyTemplatesXslt::ApplyTemplates method to initiate a transformation by applying templates in the default mode and serializing the primary result document to a stream.

 
XPathNavigator contextItem = document.CreateNavigator();

Stream result = new MemoryStream();
xslt.ApplyTemplates(contextItem, result);
 
 
 
Security note
For security reasons, by default the doc and collection functions always raise an error. This behaviour is changed by setting the DocumentSetDocumentSetDocumentSet property on the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings. See Loading External Resources for more information.
 
 
 
Note
The Xslt.CallTemplateXslt.CallTemplateXslt::CallTemplate methods might also be used, depending on the required initiation options.
 

Multiple Result Documents.

The xsl:result-document element allows XSLT 2.0 programs to produce more than one result document. To evaluate transformations which produce multiple result documents, use one of the Xslt.ApplyTemplatesXslt.ApplyTemplatesXslt::ApplyTemplates methods which take an IResultDocumentHandlerIResultDocumentHandlerIResultDocumentHandler argument.

 
using (IResultDocumentHandler handler = new MyResultDocumentHandler())
{
    xslt.ApplyTemplates(contextItem, handler);
    handle.Complete();
}
      
 

The call to IResultDocumentHandler.CompleteIResultDocumentHandler.CompleteIResultDocumentHandler::Complete indicates that all newly created documents should be written out, for example, to the file system or back to the document set. Depending on the IResultDocumentHandlerIResultDocumentHandlerIResultDocumentHandler implementation, should an exception be raised during processing, all result documents might be discarded when the result document handler is disposed. Thus either all result documents would be created, or none at all.