saxon.net将xslt转换为文字

时间:2012-07-02 09:58:30

标签: asp.net xml xslt saxon

需要一些saxon.net的帮助。

有人能告诉我如何将xslt转换后的xlm输出到asp.net文字中吗?

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

'我们可以导入它并使用它的任何派生类,方法等, '但是在这个例子中,我们将使用FileStream作为源代码 '和转换文件,撒克逊将从那里处理事情, '因此,无需为此特定示例导入System.Xml。 Private Sub Page_Load(发件人为[Object],e为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

End Sub

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

事先提前

1 个答案:

答案 0 :(得分:2)

我想你只想要

Using sw As New StringWriter()
  serializer.SetOutputWriter(sw)
  transformer.Run(serializer)
  Literal1.Text = sw.ToString()
End Using