Project

Profile

Help

Bug #4299

closed

Saxon-HE v9.9.1.4 Exception XPDY0002 running .NET C# test example XsltSimple1.

Added by Finnur Jonsson about 5 years ago. Updated about 5 years ago.

Status:
Closed
Priority:
Normal
Category:
.NET API
Sprint/Milestone:
Start date:
2019-08-25
Due date:
% Done:

100%

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

Description

Saxon-HE v9.9.1.4 Exception XPDY0002 running .NET C# test example XsltSimple1.

I am using Saxon API for .NET (Saxon-HE v9.9.1.4 version). I am testing on Windows 10 Enterprise 1809 with Visual Studio Professional 2019 version 16.2.0.

I made a Windows console program with .NET 4.7.2 runtime using the ExamplesHE.cs source code unmodified. I also used the books.xml and books.xsl files unmodified. Then I set the project startup parameters in the debugger to run the XsltSimple1 test:

// Relevant code fragment:
// ------------------------------------------------------------------
public override void run(Uri samplesDir)    // running XsltSimple1 test
    {
        // Create a Processor instance.
        Processor processor = new Processor();

        // Load the source document
        XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));

        // Create a transformer for the stylesheet.
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load30();

        // Create a serializer, with output to the standard output stream
        Serializer serializer = processor.NewSerializer();
        serializer.SetOutputWriter(Console.Out);

        // Transform the source XML and serialize the result document
        transformer.ApplyTemplates(input, serializer);     // XPDY0002 exception here 
    }
}
// End of code fragment
// ------------------------------------------------------------------

Error detail.

Data {System.Collections.ListDictionaryInternal}
ErrorCode {err:XPDY0002}
Hresult  -2146233088
HelpLink null
InnerException null
IsTypeError false
LineNumber 10
Message = "The context item for axis step root/descendant::CATEGORY is absent"
ModuleUri = "file:///c:/mittN...august/SaxonHETestData/styles/books.xsl"
Source  "saxon9he-api"
StackTrace = "   at Saxon.Api.Xslt30Transformer.ApplyTemplates(XdmValue selection, XmlDestination destination)\r\n   at SaxonHE.XsltSimple1.run(Uri samplesDir) in C:\\mittN1Reikn\\Test21august\\SaxonHETest\\Program.cs:line 460\r\n   at SaxonHE.ExamplesHE.Main(String[] a...
TargetSite = {Void ApplyTemplates(Saxon.Api.XdmValue, Saxon.Api.XmlDestination)}
UnderlyingException = {"The context item for axis step root/descendant::CATEGORY is absent"}

Can anyone explain what the reason is? I think it is fairly unlikely that there is such a basic bug in the SaxonHE library or the sample files provided. I did have to make the Visual Studio project myself as I did not use the Ant files for building.

Please note that even though I am a programmer with decades of experience, working with XML on occasion, this is my first attempt at performing an XSLT transformation from code. So the error I am getting might be explained by some configuration or setup error. Interestingly the XsltSimple2 test runs without error but the XsltSimple3 fails with the same error. So at least the setup is somewhat working.

When I run the transform.exe command utility provided I get no errors when I run it on the same files as used in XsltSimple1 test.

The test command used was: transform -s:books.xml -xsl:books.xsl -o:booksOut.xml

Actions #1

Updated by Finnur Jonsson about 5 years ago

Formatting error, this should have been in the code block: Processor processor = new Processor();

Actions #2

Updated by Michael Kay about 5 years ago

  • Description updated (diff)
Actions #3

Updated by Michael Kay about 5 years ago

I think the most likely reason is that your stylesheet contains a global variable that is dependent (directly or indirectly) on the global context item, and you have not set a global context item.

In XSLT 3.0 the "global context item" and the "initial match selection" can be set independently, and the applyTemplates() invocation method only sets the initial match selection. From the command line (for reasons of tradition) the -s option sets both, and you can also set both using the XsltTransformer.transform() method. Alternatively, you can set the global context item directly on the XsltTransformer.

Actions #4

Updated by Martin Honnen about 5 years ago

@Michael, so the error is present in the samples as https://dev.saxonica.com/repos/archive/opensource/latest9.9/samples/styles/books.xsl has the global variable <xsl:variable name="categories" select="//CATEGORY"/> and https://dev.saxonica.com/repos/archive/opensource/latest9.9/samples/cs/ExamplesHE.cs in the example XsltSimple1 does not use the transform method (I don't think it exists on the .NET side of the API so far) nor does it set the global context item:

    public class XsltSimple1 : Example
    {

        public override String testName
        {
            get { return "XsltSimple1"; }
        }

        public override void run(Uri samplesDir)
        {
            // Create a Processor instance.
            Processor processor = new Processor();

            // Load the source document
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));

            // Create a transformer for the stylesheet.
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load30();

            // Create a serializer, with output to the standard output stream
            Serializer serializer = processor.NewSerializer();
            serializer.SetOutputWriter(Console.Out);

            // Transform the source XML and serialize the result document
            transformer.ApplyTemplates(input, serializer);
        }
    }
Actions #5

Updated by Finnur Jonsson about 5 years ago

I got this to run by doing the following changes:

  public override void run(Uri samplesDir)
    {
        // Create a Processor instance.

        Processor processor = new Processor();

        // Load the source document
        XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));

        // Create a transformer for the stylesheet.
        // Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load30();
        XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load();

        // Set the root node of the source document to be the initial context node
        transformer.InitialContextNode = input;

        // Create a serializer, with output to the standard output stream
        Serializer serializer = processor.NewSerializer();
        serializer.SetOutputWriter(Console.Out);

        // Transform the source XML and serialize the result document
        // transformer.ApplyTemplates(input, serializer);     // XPDY0002 exception here 
        transformer.Run(serializer);     // transformer.transform() does not seem to exist in the .NET version
    }

I was using the data and styles files unmodified from the samples. There seems to be a mismatch between the sample files and the sample source code. Still this was a learning experience for me. I don't need the XSLT 3 version features.

Actions #6

Updated by Michael Kay about 5 years ago

  • Assignee changed from Community Admin to Debbie Lockett
Actions #8

Updated by Debbie Lockett about 5 years ago

  • Status changed from New to In Progress

The .NET samples were updated prior to the first 9.9 .NET release (9.9.1.1) to use Xslt30Transformer rather than XsltTransformer, re bug #3948. It looks like these changes were not properly tested. None of the test examples set the global context item, but evidently there are some cases where this is required, e.g. XsltSimple1.

These should have been tested as part of the build and release process (Ant target "test-api-n"), so I'm not quite sure how these errors were not picked up then. Either the wrong versions of the samples were tested, or we simply didn't notice the errors. Apologies.

Actions #9

Updated by Finnur Jonsson about 5 years ago

Thanks for the update. Anyway the quick response is a sign of a product that is actively supported.

Actions #10

Updated by Debbie Lockett about 5 years ago

  • Tracker changed from Support to Bug
  • Applies to branch trunk added

Reclassifying this as a bug: the problems are due to mistakes in the .NET samples in the 9.9 saxon-resources. Although the samples are run as part of the .NET build and release process, the output was not properly checked for correctness.

Example XsltSimple1 (and similarly XsltSimple3) can be fixed by adding the following after the Xslt30Transformer transformer has been created:

            // Set the root node of the source document to be the global context item
            transformer.GlobalContextItem = input;

(As an alternative to reverting back to use an XsltTransformer, as Finnur has done in comment #4299-5.)

A number of other issues have been found in the .NET samples ExamplesEE.cs, ExamplesPE.cs, and ExamplesHE.cs; as well as unnecessary inconsistencies between them (a number of tests are found in two, or all three, of these files).

Fixes have been committed for the samples on the 9.9 and 10.0 dev branches, and these are now being checked. A couple of issues remain to be resolved (e.g. further changes may be required following the resolution of .NET bug #4296).

I have also tidied up the build-n Ant scripts to consistently take the samples source from latest9.9/samples/cs for 9.9 (and svn/samples/cs on the dev branch for 10.0). Previously the build99 script was using latest9.9/hen/csource/samples, but we don't need these two versions, and I think hen/csource/samples can actually now be removed. Note that this directory exists for the different Saxon versions, but the content and structure is not consistent between 9.9, 9.8, 9.7, 9.6... so using this for the samples source makes maintaining the build scripts awkward.

Actions #11

Updated by Debbie Lockett about 5 years ago

  • Status changed from In Progress to Resolved
  • Fix Committed on Branch 9.9, trunk added

Various changes have been made in the .NET samples to get them working correctly. Fixes committed on the 9.9 and 10.0 dev branches.

The latest9.9/hen/csource/samples directory has been removed, as suggested above, to avoid duplication of latest9.9/samples/cs.

The 9.9 saxon-resources download will be updated with the next maintenance release.

Actions #12

Updated by O'Neil Delpratt about 5 years ago

  • Status changed from Resolved to Closed
  • % Done changed from 0 to 100
  • Fixed in Maintenance Release 9.9.1.5 added

Bug fix applied in the Saxon 9.9.1.5 maintenance release.

Please register to edit this issue

Also available in: Atom PDF