Project

Profile

Help

map:build use with SaxonCS 12 on wrapped System.Xml.Linq.XDocument gives System.NotImplementedException

Added by Martin Honnen almost 2 years ago

I couldn't find any sample on using SaxonCS 12 with XDocument in .NET so I tried to write my own sample, I came up with the following:

using Saxon.Api;
using System.Xml.Linq;

XDocument doc = XDocument.Parse(@"<root>
  <item>
    <name>Item 1</name>
    <categories>
      <cat>a</cat>
      <cat>b</cat>
    </categories>
  </item>
  <item>
    <name>Item 2</name>
    <categories>
      <cat>a</cat>
      <cat>c</cat>
    </categories>
  </item>
  <item>
    <name>Item 3</name>
    <categories>
      <cat>b</cat>
      <cat>c</cat>
    </categories>
  </item>
</root>");


Processor processor = new(true);

DocumentBuilder docBuilder = processor.NewDocumentBuilder();

XdmNode xdmDoc = docBuilder.Wrap(doc);

XQueryCompiler xqueryCompiler = processor.NewXQueryCompiler();

xqueryCompiler.XQueryLanguageVersion = "4.0";

var xquery = @"xquery version '4.0';
declare namespace map = 'http://www.w3.org/2005/xpath-functions/map';
map:build(root/item/categories/cat, identity#1, ->{ancestor::item})
";

var xquerySelector = xqueryCompiler.Compile(xquery).Load();
xquerySelector.ContextItem = xdmDoc;

var result = xquerySelector.Evaluate();

Console.WriteLine(result);

Throws an exception on the xquerySelector.Evaluate():

System.NotImplementedException
  HResult=0x80004001
  Nachricht = The method or operation is not implemented.
  Quelle = SaxonCS
  Stapelüberwachung:
   bei Saxon.Hej.tree.wrapper.AbstractNodeWrapper.getNamespaceUri()
   bei Saxon.Hej.pattern.NameTest.test(NodeInfo node)
   bei Saxon.Hej.tree.util.Navigator.AxisFilter.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.ma.map.MapFunctionSet.MapBuild.call(XPathContext context, Sequence[] arguments)
   bei Saxon.Hej.expr.SystemFunctionCall.SystemFunctionCallElaborator.<>c__DisplayClass1_0.<elaborateForPull>b__3(XPathContext context)
   bei Saxon.Hej.query.XQueryExpression.getExpressionIterator(XPathContext context)
   bei Saxon.Hej.query.XQueryExpression.iterator(DynamicQueryContext env)
   bei Saxon.Hej.s9api.XQueryEvaluator.evaluate()
   bei Saxon.Api.XQueryEvaluator.Evaluate()
   bei Program.<Main>$(String[] args) in C:\Users\marti\source\repos\SaxonCS12LinqTest1\Program.cs: Zeile47

The same query using Saxon's native XdmNode works fine:

using Saxon.Api;

var xmlSource = @"<root>
  <item>
    <name>Item 1</name>
    <categories>
      <cat>a</cat>
      <cat>b</cat>
    </categories>
  </item>
  <item>
    <name>Item 2</name>
    <categories>
      <cat>a</cat>
      <cat>c</cat>
    </categories>
  </item>
  <item>
    <name>Item 3</name>
    <categories>
      <cat>b</cat>
      <cat>c</cat>
    </categories>
  </item>
</root>";


Processor processor = new(true);

DocumentBuilder docBuilder = processor.NewDocumentBuilder();

XdmNode xdmDoc = docBuilder.Build(new StringReader(xmlSource));

XQueryCompiler xqueryCompiler = processor.NewXQueryCompiler();

xqueryCompiler.XQueryLanguageVersion = "4.0";

var xquery = @"xquery version '4.0';
declare namespace map = 'http://www.w3.org/2005/xpath-functions/map';
map:build(root/item/categories/cat, identity#1, ->{ancestor::item})
";

var xquerySelector = xqueryCompiler.Compile(xquery).Load();
xquerySelector.ContextItem = xdmDoc;

var result = xquerySelector.Evaluate();

Console.WriteLine(result);

Is this a bug in the bridge between SaxonCS and XDocument?


Replies (4)

Please register to reply

RE: map:build use with SaxonCS 12 on wrapped System.Xml.Linq.XDocument gives System.NotImplementedException - Added by Martin Honnen almost 2 years ago

Even "normal XQuery 3.1" grouping triggers the bug as long as I run it against a wrapped XDocument:

var xquery = @"xquery version '3.1';
declare namespace map = 'http://www.w3.org/2005/xpath-functions/map';
(for $cat in root/item/categories/cat
group by $cat-key := $cat
return map:entry($cat-key, $cat/ancestor::item)) => map:merge()
";
System.NotImplementedException
  HResult=0x80004001
  Nachricht = The method or operation is not implemented.
  Quelle = SaxonCS
  Stapelüberwachung:
   bei Saxon.Hej.tree.wrapper.AbstractNodeWrapper.getNamespaceUri()
   bei Saxon.Hej.pattern.NameTest.test(NodeInfo node)
   bei Saxon.Hej.tree.util.Navigator.AxisFilter.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.expr.MappingIterator.next()
   bei Saxon.Hej.om.FocusTrackingIterator.next()
   bei Saxon.Hej.expr.flwor.ForClausePull.nextTuple(XPathContext context)
   bei Saxon.Hej.expr.flwor.LetClausePull.nextTuple(XPathContext context)
   bei Saxon.Hej.expr.flwor.GroupByClausePull.nextTuple(XPathContext context)
   bei Saxon.Hej.expr.flwor.ReturnClauseIterator.next()
   bei Saxon.Hej.ma.map.MapFunctionSet.MapMerge.mergeMaps(SequenceIterator iter, XPathContext context, String duplicates, String duplicatesErrorCode, FunctionItem onDuplicates)
   bei Saxon.Hej.ma.map.MapFunctionSet.MapMerge.call(XPathContext context, Sequence[] arguments)
   bei Saxon.Hej.expr.SystemFunctionCall.SystemFunctionCallElaborator.<>c__DisplayClass1_0.<elaborateForPull>b__1(XPathContext context)
   bei Saxon.Hej.query.XQueryExpression.getExpressionIterator(XPathContext context)
   bei Saxon.Hej.query.XQueryExpression.iterator(DynamicQueryContext env)
   bei Saxon.Hej.s9api.XQueryEvaluator.evaluate()
   bei Saxon.Api.XQueryEvaluator.Evaluate()
   bei Program.<Main>$(String[] args) in C:\Users\marti\source\repos\SaxonCS12LinqTest1\Program.cs: Zeile53

RE: map:build use with SaxonCS 12 on wrapped System.Xml.Linq.XDocument gives System.NotImplementedException - Added by Martin Honnen almost 2 years ago

Even a simple descendant::cat against a wrapped XDocument triggers this:

using Saxon.Api;
using System.Xml.Linq;

XDocument doc = XDocument.Parse(@"<root>
  <item>
    <name>Item 1</name>
    <categories>
      <cat>a</cat>
      <cat>b</cat>
    </categories>
  </item>
  <item>
    <name>Item 2</name>
    <categories>
      <cat>a</cat>
      <cat>c</cat>
    </categories>
  </item>
  <item>
    <name>Item 3</name>
    <categories>
      <cat>b</cat>
      <cat>c</cat>
    </categories>
  </item>
</root>");


Processor processor = new(true);

DocumentBuilder docBuilder = processor.NewDocumentBuilder();

XdmNode xdmDoc = docBuilder.Wrap(doc);

XQueryCompiler xqueryCompiler = processor.NewXQueryCompiler();

xqueryCompiler.XQueryLanguageVersion = "3.1";

var xquery = @"xquery version '3.1';
descendant::cat
";

var xquerySelector = xqueryCompiler.Compile(xquery).Load();
xquerySelector.ContextItem = xdmDoc;

var result = xquerySelector.Evaluate();

Console.WriteLine(result);

Wow, that seems too simple to not have been caught be existing test suites.

Is there anything wrong in my C# API use?

RE: map:build use with SaxonCS 12 on wrapped System.Xml.Linq.XDocument gives System.NotImplementedException - Added by Martin Honnen almost 2 years ago

This seems to prevent any use of a wrapped XDocument, a simple XSLT transformation also runs into that error

System.NotImplementedException
  HResult=0x80004001
  Nachricht = The method or operation is not implemented.
  Quelle = SaxonCS
  Stapelüberwachung:
   bei Saxon.Hej.tree.wrapper.AbstractNodeWrapper.getNamespaceUri()
   bei Saxon.Hej.trans.SimpleMode.findBestRuleForNodeInfo(NodeInfo node, XPathContext context)
   bei Saxon.Hej.trans.SimpleMode.getRule(Item item, XPathContext context)
   bei Saxon.Hej.trans.Mode.applyTemplates(ParameterSet parameters, ParameterSet tunnelParameters, NodeInfo separator, Outputter output, XPathContextMajor context, Location locationId)
   bei Saxon.Hej.trans.rules.ShallowCopyRuleSet.process(Item item, ParameterSet parameters, ParameterSet tunnelParams, Outputter out, XPathContext context, Location locationId)
   bei Saxon.Hej.trans.Mode.applyTemplates(ParameterSet parameters, ParameterSet tunnelParameters, NodeInfo separator, Outputter output, XPathContextMajor context, Location locationId)
   bei Saxon.Hej.trans.XsltController.applyTemplates(Sequence source, Receiver out)
   bei Saxon.Hej.s9api.Xslt30Transformer.applyTemplates(XdmValue selection, Destination destination)
   bei Saxon.Api.Xslt30Transformer.ApplyTemplates(XdmValue selection, IDestination destination)
   bei Program.<Main>$(String[] args) in C:\Users\marti\source\repos\SaxonCS12XDoc2XDocXSLT\Program.cs: Zeile61

Somehow your test suites must have failed or the published release kind of contains code that was not tested against wrapped XDocuments.

RE: map:build use with SaxonCS 12 on wrapped System.Xml.Linq.XDocument gives System.NotImplementedException - Added by Michael Kay almost 2 years ago

Thanks, yes, I'm aware of this and have already fixed it but it didn't make it into the release. I discovered at the last minute that the tests for Linq integration weren't being routinely run and that the feature had broken since the coding was done about a year ago.

    (1-4/4)

    Please register to reply