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.

First, we create a new XmlNameTableXmlNameTableXmlNameTable instance. This will be used to share information such as element and attribute names between the XML documents and the transformation. Next we create an XmlReaderSettingsXmlReaderSettingsXmlReaderSettings instance and set its NameTableNameTableNameTable property.

 
XmlNameTable nameTable = new NameTable();

XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.NameTable = nameTable;
 
 
 
Warning
In order for XmlPrime to work correctly all documents passed in to the transformation must be loaded with the XmlNameTableXmlNameTableXmlNameTable used to compile the transformation.
 

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

 
XdmDocument document;

using (XmlReader reader = XmlReader.Create("bib.xml", xmlReaderSettings))
{
    document = new XdmDocument(reader);
}
 

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. In order 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 CompileCompileCompile method. This returns us an XsltXsltXslt object encapsulating the transformation.

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

Xslt query = Xslt.Compile("bib.xsl", querySettings);
 

Executing the Transformation

Now we have our query object we now just need to execute it. We will use the ApplyTemplatesApplyTemplatesApplyTemplates 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 CallTemplateCallTemplateCallTemplate methods might also be used, depending on the required initiation options.