问题
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:
- Saxon.Api.DynamicError
- 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
回答1:
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.
来源:https://stackoverflow.com/questions/6772630/getting-detailed-error-description-from-saxon-9-x-in-net