0

需要一些关于 saxon.net 的帮助。

谁能告诉我如何将 xslt 转换的 xlm 输出为 asp.net 文字?

' NOTE: Saxon now uses System.Xml internally on the .NET platform.

'我们可以导入它并使用它的任何派生类、方法等,'但在这个例子中,我们将使用 FileStream 来处理源文件和转换文件,Saxon 将从那里处理这些事情, ' 所以对于这个特定的示例不需要导入 System.Xml。Private Sub Page_Load(sender As [Object], e As EventArgs) ' 创建两个字符串,分别代表源文件和 ' 转​​换文件。这些值可以使用'多种方法(例如QueryString)设置,但现在'我们将保持简单并对这些值进行硬编码。Dim sourceUri As [String] = Server.MapPath("beispiel1.xml") Dim xsltUri As [String] = Server.MapPath("beispiel1.xslt")

' The new Saxon engine for the .NET platform allows the
' ability to pass in a Stream instance directly.
' Given that the Stream class implements IDisposable, we
' can use the 'using' keyword/codeblock to ensure that
' our resources are properly cleaned up, freeing up
' valuable system resources in the process.
' [UPDATE: Check that.  This really should read
' close all of the file streams we opened automagically.
' While its true we're cleaning up system resources
' It's not like theres really a choice.  One way
' or another the stream needs to be closed, this
' Just does this for us so we don't have to worry about it.
Using sXml As FileStream = File.OpenRead(sourceUri)
    ' We need to do this for each Stream that we pass in for processing.
    Using sXsl As FileStream = File.OpenRead(xsltUri)
        ' Now we simply create a Processor instance.
        Dim processor As New Processor()

        ' Load the source document into a DocumentBuilder
        Dim builder As DocumentBuilder = processor.NewDocumentBuilder()

        ' Because we have created the DocumentBuilder with a file stream
        ' we need to set the BaseUri manually, as this information is no
        ' longer available to the Saxon internals when a Stream, regardless
        ' of what the source of this Stream happens to be, is used for creating
        ' the DocumentBuilder instance.  With this in mind, we need to create
        ' an instance of a Uri using the sourceUri we created above.
        Dim sUri As New Uri(sourceUri)

        ' Now set the baseUri for the builder we created.
        builder.BaseUri = sUri

        ' Instantiating the Build method of the DocumentBuilder class will then
        ' provide the proper XdmNode type for processing.
        Dim input As XdmNode = builder.Build(sXml)

        ' Create a transformer for the transformation file, compiling and loading this
        ' file using the NewXsltCompiler method of the Saxon.Api namespace.
        Dim transformer As XsltTransformer = processor.NewXsltCompiler().Compile(sXsl).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()

        ' Set the serializer to write the transformation output directly
        ' to ASP.NET's Response.Output stream.
        serializer.SetOutputWriter(Response.Output)

        ' Run the transformation.
        transformer.Run(serializer)

        ' and we're done. :)
        Literal1.Text = "xslt xml transformed output here"
    End Using
End Using

结束子

我不想将它写入 response.output 流。但是变成了Literal1.Text。

提前谢谢

4

1 回答 1

2

我想你只是想要

Using sw As New StringWriter()
  serializer.SetOutputWriter(sw)
  transformer.Run(serializer)
  Literal1.Text = sw.ToString()
End Using
于 2012-07-02T10:03:56.930 回答