从.net中的Saxon 9.x获取详细的错误描述

发布于 2024-11-25 06:53:10 字数 2114 浏览 1 评论 0原文

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

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

我想从 saxon 获得一些更详细的错误信息,例如错误的行号和原因。我能找到的唯一两个异常是:

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

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

我有以下代码:

<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

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: Failed to compile stylesheet. 1 error detected.

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?

I have following code:

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-12-02 06:53:10

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文