user168507
user168507

Reputation: 881

saxon.net write transformed xslt into a literal

need some help with saxon.net.

Can anybody tell me how to output a xslt transformed xlm into a asp.net literal?

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

' We could import it and use any of its derived classes, methods, and so forth, ' but in this example we will be using a FileStream for both the source ' and transformation files, and Saxon will be taking care of things from there, ' so theres no need to import System.Xml for this particular example. Private Sub Page_Load(sender As [Object], e As EventArgs) ' Create two strings that represent the source file and ' transformations file. These values could be set using ' a variety of methods (e.g. QueryString) but for now ' we will keep things simple and hardcode these values. 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

I don't want to write it to the response.output stream. But into Literal1.Text.

thx in advance

Upvotes: 0

Views: 453

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167471

I think you simply want

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

Upvotes: 2

Related Questions