【发布时间】:2011-07-21 07:25:13
【问题描述】:
我有一个使用 saxon 9.x 设置的 xslt2 转换引擎。我有一些带有大型 xsl 转换文件的大型 xml 文件。我可以使用 XQSharp XSLT2 引擎进行转换,但使用 saxon 时出现错误:
javax.xml.transform.TransformerConfigurationException:编译样式表失败。检测到 1 个错误。
我想从撒克逊获得一些更详细的错误信息,例如错误的行号和原因。我能找到的唯一两个例外是:
- Saxon.Api.DynamicError
- 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
【问题讨论】: