Jesper Tverskov, November 12, 2008

Saxon XQuery processor in ASP.NET

There are several XQuery processors for .NET to choose from. In this example I use Saxon having the benefit of also being an XSLT processor. When you have the XQuery part of the processor working or the XSLT 2.0 part, the other one is also ready for use.

1. Saxon’s documentation

Saxon’s .NET API Documentation is not easy reading unless you do .NET programming every day. The About Saxon webpage mentions that (the number will get higher one day) contains additional documentation. The file, the "-n" part of the name is not used any more, can be downloaded from the Saxonica download page, or from ("Documentation and samples"). [1]

Inside is a "samples/cs" directory and the file examples.cs for both XSLT and XQuery. The best way to open the "cs" file is from inside software knowing what "cs" means like "Visual Studio .Net", "Visual Web Developer 2008 Express Edition", etc.

The proper place to get help for Saxon in ASP.NET is at the Saxon Discussion Forums: Help.

2. What I do in this tutorial

The XQuery examples in are not easy reading unless you now the ins and outs of .NET programming already. In this tutorial I have simply recreated a very basic example in ASP.NET. When you have my example working, it is much easier to return to the original documentation of Saxon.

All the XQuery examples in "samples/cs" are rather simple, the XQuery being a string of a line or so from inside the Codebehind file. In my example we load both the input document from an XML file and the XQuery from a text file. The XQuery is an ASP.NET version of the XQuery used in my tutorial: Creating XHTML with XQuery, also having a comparison of XQuery and XSLT.

We use XQuery to transform or query "products.xml" into an XHTML table. We do it with a webpage. When a browser requests "products2table.aspx", the XQuery in the Codebehind file transform products.xml to an XHTML webpage server-side and sends it to the browser.

3. How make ASP.NET find Saxon

Here is the easy way. Let us use the free edition, Saxon-B. Download and unzip "" (the number will get higher one day). It contains a "bin" directory. Simply copy the files in the "bin" directory to a "bin" directory at the root of your ASP.NET website.

At the top of the CodeFile, you can now insert:

using Saxon.Api;

That's it!

4. Input XML file


<?xml version="1.0"?>
  <product id="p1">
  <product id="p2">
  <product id="p3">
  <product id="p4">
  <product id="p5">

5. XQuery file

The XQuery below is the same as the XQuery discussed in more detail in my tutorial, Creating XHTML with XQuery. Only the prolog with serialization parameters have been removed and inserted in the aspx.cs CodeFile instead.


<html xml:lang="en" xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
      body{{font-family: Verdana, Arial, Helvetica, sans-serif}}
      table, th, td{{border: 1px solid gray; border-collapse:collapse}}
    <title>Using XQuery</title>
    <h1>Using XQuery</h1>
    <table cellspacing="0" cellpadding="5">
      <tr class="th">
      {for $a at $b in *:products/*:product return
      <tr class="{if ($b mod 2 = 0) then "alt1" else "alt2"}">
        <td> {$a/@id/data(.)}</td>

Please note that the XQuery above uses wildcards for namespaces to make it ease to create XHTML when input XML is in no namespace. This method is considered "quick and dirty" and should only be used for tests. My tutorial, Creating XHTML with XQuery, shows some better but more cumbersome methods.

6. aspx webpage


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="products2table.aspx.cs" Inherits="_products2table" %>

7. aspx.cs (codefile)


using System;
using System.IO;
using Saxon.Api;

public partial class _products2table : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)
    Processor processor = new Processor();
    //Input loaded from xml file
    XdmNode indoc = processor.NewDocumentBuilder().Build(new Uri(Server.MapPath("products.xml")));
    //XQuery loaded from text file
    StreamReader query = new StreamReader(File.Open(Server.MapPath("products2table.xquery"), FileMode.Open));
    XQueryCompiler compiler = processor.NewXQueryCompiler();
    XQueryExecutable exp = compiler.Compile(query.ReadToEnd());
    XQueryEvaluator eval = exp.Load();
    eval.ContextItem = indoc;

    Serializer qout = new Serializer();
    qout.SetOutputProperty(Serializer.METHOD, "xml");
    qout.SetOutputProperty(Serializer.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN");
    qout.SetOutputProperty(Serializer.DOCTYPE_SYSTEM, "");
    qout.SetOutputProperty(Serializer.INDENT, "yes");
    qout.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "no");


For a discussion of serializing to "xml" and to "xhtml", see my tutorial, Creating XHTML with XQuery.

8. Output XHTML

The browser requests products2table.aspx. Using the XQuery, the codebehind file transforms products.xml to XHTML and sends it to the browser.



This tutorial is a remake of my tutorial XSLT 2.0 Saxon in ASP.NET using XQuery instead and focusing on a concrete example, "products2table".

Updated 2009-11-13