Project

Profile

Help

Support #4697

closed

How to get details on XML parsing error during stylesheet compilation in Saxon 10.2 .NET

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

Status:
Closed
Priority:
Low
Category:
s9api API
Sprint/Milestone:
-
Start date:
2020-08-26
Due date:
% Done:

100%

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

Description

While trying to integrate Saxon 10.2 .NET HE I have found that any errors reported by the XML parser during stylesheet compilation lack specific details given by the XML parser that are present in 9.9.1.7 HE.

Example:

           Processor processor = new Processor();

            XsltCompiler xsltCompiler = processor.NewXsltCompiler();

            xsltCompiler.SetErrorList(new List<XmlProcessingError>());

            string xslt = @"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""3.0""</xsl:stylesheet>";

            XsltExecutable xsltExecutable;

            using (StringReader xsltReader = new StringReader(xslt))
            {
                try
                {
                    xsltExecutable = xsltCompiler.Compile(xsltReader);
                }
                catch (Exception)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("Stylesheet compilation failed with {0} error(s):", xsltCompiler.GetErrorList().Count);
                    sb.AppendLine();
                    int errCount = 0;
                    foreach (var error in xsltCompiler.GetErrorList())
                    {
                        errCount++;
                        string message;
                        try
                        {
                            message = error.InnerMessage;
                        }
                        catch (NullReferenceException)
                        {
                            message = error.Message;
                        }

                        sb.AppendFormat("Error {0} at line {1}:{2} : {3}", errCount, error.LineNumber, error.ColumnNumber, message);
                        sb.AppendLine();
                    }
                    Console.WriteLine(sb.ToString());
                }
            }

with Saxon 10 only gives the general error 

Stylesheet compilation failed with 1 error(s): Error 1 at line 1:79 : Error reported by XML parser

while the previous code for Saxon 9.9

            Processor processor = new Processor();

            XsltCompiler xsltCompiler = processor.NewXsltCompiler();

            xsltCompiler.ErrorList = new List<StaticError>();

            string xslt = @"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""3.0""</xsl:stylesheet>";

            XsltExecutable xsltExecutable;
            
            using (StringReader xsltReader = new StringReader(xslt))
            {
                try
                {
                    xsltExecutable = xsltCompiler.Compile(xsltReader);
                }
                catch (Exception)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("Stylesheet compilation failed with {0} error(s):", xsltCompiler.ErrorList.Count);
                    sb.AppendLine();
                    int errCount = 0;
                    foreach (StaticError error in xsltCompiler.ErrorList)
                    {
                        errCount++;
                        string message;
                        try
                        {
                            message = error.InnerMessage;
                        }
                        catch (NullReferenceException)
                        {
                            message = error.Message;
                        }
                        sb.AppendFormat("Error {0} at line {1}:{2} : {3}", errCount, error.LineNumber, error.ColumnNumber, message);
                        sb.AppendLine();
                    }
                    Console.WriteLine(sb.ToString());
                }
            }

outputs

Stylesheet compilation failed with 1 error(s):
Error 1 at line 1:79 : Error reported by XML parser: Element type "xsl:stylesheet" must be followed by either attribute specifications, ">" or "/>".

i.e. has the precise message from the XML parser.

Is there any way to get that precise message raised by the XML parser in Saxon 10.2 .NET as well?

Actions #1

Updated by Martin Honnen over 3 years ago

The difference between 9.9 and 10.2 seems to be that the InnerMessage trying to concat return exception.getMessage() + ": " + exception.getCause().Message adds the parser error details (e.g. Element type "xsl:stylesheet" must be followed by either attribute specifications, ">" or "/>") in 9.9 while it throws a NullReferenceException in 10.2.

Why is the getCause() null, is that something that has changed on the Java side or solely on the .NET side?

Actions #2

Updated by Martin Honnen over 3 years ago

Saxon 9.9 has

       public XsltExecutable Compile(TextReader input)
        {
            try {
                JStreamSource ss = new JStreamSource(new JDotNetReader(input));
                if (baseUri != null)
                {
                    ss.setSystemId(Uri.EscapeUriString(baseUri.ToString()));
                }

                return new XsltExecutable(xsltCompiler.compile(ss));
            }
            catch (JSaxonApiException ex) {
                throw new StaticError(ex);
            }
        }

Saxon 10.2 has

       public XsltExecutable Compile(TextReader input)
        {
            try {
                JStreamSource ss = new JStreamSource(new JDotNetReader(input));
                if (baseUri != null)
                {
                    ss.setSystemId(Uri.EscapeUriString(baseUri.ToString()));
                }

                XsltExecutable executable = new XsltExecutable(xsltCompiler.compile(ss));
                executable.InternalProcessor = processor;
                return executable;
            }
            catch (JSaxonApiException ex) {
                throw new StaticError(ex);
            }
        }

Both have using JSaxonApiException = net.sf.saxon.s9api.SaxonApiException;.

Saxon 9.9 has

   public class StaticError : Exception
    {

        private XPathException exception;
        internal bool isWarning;

        // internal constructor: Create a new StaticError, wrapping a Saxon XPathException

        internal StaticError(JException err)
        {
            if (err is XPathException)
            {
                this.exception = (XPathException)err;
            }
            else
            {
                this.exception = XPathException.makeXPathException(err);
            }
        }

Saxon 10.2

    public class StaticError : Exception
    {

        internal XPathException exception;
        internal bool isWarning;

        // internal constructor: Create a new StaticError, wrapping a Saxon XPathException

        internal StaticError(JException err)
        {
            if (err is XPathException)
            {
                this.exception = (XPathException)err;
            }
            else
            {
                this.exception = XPathException.makeXPathException(err);
            }
        }

Both with

using XPathException = net.sf.saxon.trans.XPathException;
using JException = java.lang.Exception;

So far the only difference I notice is private XPathException exception; in 9.9 versus internal XPathException exception; in 10.2

Actions #3

Updated by Michael Kay over 3 years ago

  • Assignee set to O'Neil Delpratt
Actions #4

Updated by O'Neil Delpratt over 3 years ago

  • Status changed from New to In Progress

Thanks for reporting this issue and the code which I have used to reproduce the error message. Currently investigating the bug issue.

Actions #5

Updated by O'Neil Delpratt over 3 years ago

There has been a change in Saxon 10 in how we retrieve errors in the errorList. We now create a list of XmlProcessingError objects. This class is a subclass of StaticError. Somehow the XmlProcessingError object is not capturing all the details from the exception that coming through from the underlying compiler.

Actions #6

Updated by O'Neil Delpratt over 3 years ago

  • Category changed from .NET API to s9api API
  • Applies to branch trunk added

I am marking this issue as a bug against the Java product. The following repo show how we can reproduce the error on Java:

    public void testXmlProcessingError() {

        Processor processor = new Processor(false);

                    XsltCompiler xsltCompiler = processor.newXsltCompiler();
                    final List<StaticError> errors = new ArrayList<>(4);

                    xsltCompiler.setErrorList(errors);

                    String xslt = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0</xsl:stylesheet>";

                    XsltExecutable xsltExecutable;

                    StringReader xsltReader = new StringReader(xslt);

                        try
                        {
                            xsltExecutable = xsltCompiler.compile(new StreamSource(xsltReader));
                        }
                        catch (Exception ex)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.append("Stylesheet compilation failed with {0} error(s):");

                            int errCount = 0;
                            ErrorReporter reporter = xsltCompiler.getErrorReporter();

                            for (StaticError error : errors)
                            {
                                errCount++;
                                String message;
                                try
                                {
                                    message = error.getMessage();
                                }
                                catch (Exception ex1)
                                {
                                    message = ex1.getMessage();
                                }
                                
                            }
                        }
                        

    }

The getMessage() method only returns @Error reported by XML parser@

Actions #7

Updated by O'Neil Delpratt over 3 years ago

The parser message is lost in the method reportError(SAXParseException e, boolean isFatal) of the class StandardErrorHandler.

In this method we are concatenating the error message from the parser to the string Error reported by XML parser.

Actions #8

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 10, trunk added

Bug fixed in the StandardErrorHandler to capture the error message from the SAX parser. Added junit and nunit tests for Saxon 10 and 11.

Actions #10

Updated by O'Neil Delpratt over 3 years ago

Bug fix applied in the Saxon 10.3 maintenance release

Actions #11

Updated by O'Neil Delpratt over 3 years ago

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

Please register to edit this issue

Also available in: Atom PDF