Bug #3578

How to use XQuery and Serializer on .NET to output a map respectively how to ensure that output options in XQuery are used by Serializer

Added by Martin Honnen almost 4 years ago. Updated over 3 years ago.

Start date:
Due date:
% Done:


Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
9.8, trunk
Fixed in Maintenance Release:


I am having trouble to find a way to use a Serializer on .NET to output the result of an XQuery that is not XML, like a map, when the XQuery uses declare option output:method "adaptive"; that should allow for a flexible and dynamic output method.

A reduced test case run against Saxon HE is

            Processor processor = new Processor(false);
            XQueryCompiler compiler = processor.NewXQueryCompiler();

            XQueryEvaluator evaluator = compiler.Compile("declare namespace output = \"\"; declare option output:method \"adaptive\"; map { 'string' : 'foo', 'number' : 1, 'boolean' : true() }").Load();

            Serializer serializer = processor.NewSerializer(Console.Out);


and throws the following stack trace:

java.lang.UnsupportedOperationException occurred
  Message=A map has no string value
  Source=<Cannot evaluate the exception source>
   at net.sf.saxon.serialize.UncommittedSerializer.append(Item item, Location locationId, Int32 copyNamespaces)
   at net.sf.saxon.expr.Literal.process(XPathContext context)
   at env, Result result, Properties outputProperties)
   at Saxon.Api.XQueryEvaluator.Run(XmlDestination destination)
   at Saxon98XQueryTest.Program.Main(String[] args) in c:\users\martin honnen\documents\visual studio 2017\Projects\Saxon98XQueryTest\Saxon98XQueryTest\Program.cs:line 21

A similar Java program

        Processor processor = new Processor(false);
        XQueryCompiler compiler = processor.newXQueryCompiler();
        XQueryEvaluator evaluator = compiler.compile("declare namespace output = \"\"; declare option output:method \"adaptive\"; map { 'string' : 'foo', 'number' : 1, 'boolean' : true() }").load();
        Serializer serializer = processor.newSerializer(System.out);

however runs fine and outputs the map as


What code do I need on the .NET side to write XQuery results to a Serializer so that the output options in the code are honoured, like it works on the Java side?


#1 Updated by Michael Kay almost 4 years ago

I've been doing work on this area recently for XSLT - but only on the development branch. Basically, i think the; always invokes Sequence Normalization, which fails if the raw result contains a map. You need to use the evaluate() method to return an XdmValue, and then serialize that. I will review this.

#2 Updated by O'Neil Delpratt almost 4 years ago

  • Assignee set to Michael Kay

Monitor changes in the Java code to transfer to .NET code base.

#3 Updated by Joe Wicentowski over 3 years ago

I thought I'd note that this issue came up today when I was reviewing Martin's XQueryFiddle tool and noticed that the following query (also posted at returns with an XML declaration:

xquery version "3.1";

declare namespace output="";

declare option output:method "xml";
declare option output:omit-xml-declaration "yes";


The query returns:

<?xml version="1.0" encoding="UTF-8"?>

See also the discussion at

Is there any ETA for addressing this issue? I ask because I'm pointing the readers of my forthcoming book on XQuery for non-programmers to use this site for testing out basic queries. But at the moment it doesn't work as a tool for learning XQuery serialization options. Thanks in advance!

#4 Updated by O'Neil Delpratt over 3 years ago

  • Tracker changed from Support to Bug
  • Category set to .NET API
  • Status changed from New to In Progress
  • Assignee changed from Michael Kay to O'Neil Delpratt
  • Applies to branch 9.8 added

Thanks for reporting the issue, which we have reproduced. Investigating the issue now.

#5 Updated by O'Neil Delpratt over 3 years ago

  • Status changed from In Progress to Resolved
  • % Done changed from 0 to 100
  • Fix Committed on Branch 9.8, trunk added

Bug fixed and committed to svn branches: 9.8 and trunk. This will be applied in the next maintenance release.

#6 Updated by Debbie Lockett over 3 years ago

  • Status changed from Resolved to Closed
  • Fixed in Maintenance Release added

Bug fix applied in the Saxon maintenance release.

Please register to edit this issue

Also available in: Atom PDF