本文介绍了从 .net 中的 Saxon 9.x 获取详细的错误描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 saxon 9.x 设置的 xslt2 转换引擎.我有一些带有大型 xsl 转换文件的大型 xml 文件.我可以使用 XQSharp XSLT2 引擎进行转换,但使用 saxon 时出现错误:

I have an xslt2 transform engine setted up with saxon 9.x. I have some big xml file with large xsl transformation file. I can make the transform with XQSharp XSLT2 engine, but with saxon i am getting error:

javax.xml.transform.TransformerConfigurationException:无法编译样式表.检测到 1 个错误.

我想从撒克逊人那里得到一些更详细的错误信息,例如错误的行号和原因.我能找到的唯一两个例外是:

I would like to get some more detailed error information from saxon with for example the linenumber of the error and the cause. The only two exceptions that i could found are:

  1. Saxon.Api.DynamicError
  2. Saxon.Api.StaticError

但它们不会被抛出.如何获得详细的错误描述?

But they are not thrown. How to get detailed error desription?

我有以下代码:

<WebMethod()> _
Public Function XSLTSaxon(ByVal inputXml As String, ByVal inputXsl As String) As String
        Dim response As String = ""

        Try
            ' Create a Processor instance.
            Dim processor As New Processor()

            ' Create xml reader based on xml string
            Dim xmlReader As XmlReader = xmlReader.Create(New StringReader(inputXml))

            ' Load the source document.
            Dim input As XdmNode = processor.NewDocumentBuilder().Build(xmlReader)

            ' Create a transformer for the stylesheet.
            Dim transformer As XsltTransformer = processor.NewXsltCompiler.Compile(New StringReader(inputXsl)).Load()
            ' Set the root node of the source document to be the initial context node.
            transformer.InitialContextNode = input

            ' Create a serializer.
            Dim serializer As New Serializer()

            Dim result As Stream = New MemoryStream()
            serializer.SetOutputStream(result)

            transformer.Run(serializer)
            result.Position = 0
            Using reader As StreamReader = New StreamReader(result)
                response = reader.ReadToEnd()
            End Using
        Catch ex As Saxon.Api.DynamicError
            response = String.Format("<error>dynamicerror</error>", ex.ToString)
        Catch ex As Saxon.Api.StaticError
            response = String.Format("<error>staticerror</error>", ex.ToString)
        Catch ex As Exception
            response = String.Format("<error>{0}</error>", ex.ToString)
        End Try

        Return response
End Function

推荐答案

默认情况下,编译错误消息会写入标准错误输出流.如果您不是从命令行控制台运行,那么它在 .NET 上的最终位置是一个谜 - 我认为它会转到系统中某个地方的某个日志文件,但它可能是可配置的.在应用程序中处理错误最灵活的方法是使用 XsltCompiler 的 ErrorList 属性 - 如果将此属性设置为空列表,Saxon 会将错误信息作为 StaticError 对象添加到列表的末尾,从应用程序可以检索它并将其显示在最合适的地方.

By default compilation error messages are written to the standard error output stream. Where this ends up on .NET is something of a mystery if you're not running from a command-line console - I think it goes to some log file buried somewhere in the system, but it's probably configurable. The most flexible way of handling errors in your application is to use the ErrorList property of the XsltCompiler - if you set this property to an empty list, Saxon will add the error information to the end of the list as a StaticError object, from where your application can retrieve it and display it wherever is most suitable.

请注意,如果您转到 Saxon 的 SourceForge 页面,您会发现有一个论坛和一个针对 Saxon 问题的邮件列表.发布到这些位置中的任何一个的问题总会得到答案.StackOverflow 是一个很棒的地方,但您的问题是否被注意到是一个偶然的机会.

Please note, if you go to the SourceForge page for Saxon you will find there is both a forum and a mailing list for Saxon questions. Questions posted to either of those locations will always get an answer. StackOverflow is a great place, but it's hit-and-miss whether your question gets noticed.

这篇关于从 .net 中的 Saxon 9.x 获取详细的错误描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 05:23