Link to home
Start Free TrialLog in
Avatar of Kai77
Kai77

asked on

Transforming an XML stream on the fly with XSLT

Hi,

I am using the following code to generate an XML output. The code works, but now I want it to transform it using XSLT. How can I do this?

All the examples I have seen show how to read an existing XML file. Where as mine is created dynamically with the xmltextwriter.
------ My Code:

        Dim oXMLWriter As System.Xml.XmlTextWriter
        oXMLWriter = New System.Xml.XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8)

        With oXMLWriter
            .Formatting = System.Xml.Formatting.Indented
            .Indentation = 4
            .IndentChar = CChar(" ")
            .WriteStartDocument()
            .WriteStartElement("navigation")

            Call WriteNavigationItems(oXMLWriter, PageInfo.AtaChapterID)

            .WriteEndElement()
            .WriteEndDocument()
        End With

        oXMLWriter.Flush()
        oXMLWriter.Close()
Avatar of b1xml2
b1xml2
Flag of Australia image

.WriteStartDocument()
.WriteProcessingInstruction("xml-stylesheet",@"type='text/xsl' href='XsltFile1.xsl'")

this adds the xml-stylesheet processing instruction. and causes the browser (MSIE/Firefox) to transform it on the fly as well.

also,
Dim oXMLWriter As New System.Xml.XmlTextWriter(Response.Output)
Avatar of Kai77
Kai77

ASKER

nice thinkin.....but the thing is that I do not want it to output to the browser in XML format and have him do the transformation, because this block of code needs to output the navigation of a webpage (it's a usercontrol). So the output should be in the correct HTML format already.
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That'll get the data into the XPathDocument and then transformed and sent down to the client.
Avatar of Kai77

ASKER

the code works i think,

even though I can compile i am getting the following code underline in visual studio:
stylesheet.Transform(document.CreateNavigator(), Nothing, Response.OutputStream)

saying:

'Public Sub Transform(input as ...... output as system.io.stream)' is obsolete: 'You should pass XMLResolver to Transform method'
yep, but that's just a warning. You can ignore it.
or you could just add Nothing as the last argument
stylesheet.Transform(document.CreateNavigator(),Nothing,Response.OutputStream,Nothing)
Avatar of Kai77

ASKER

Ok got that.....but I was mistaken, I cannot see any transformation.

This is my simple XSL test stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <xmlns:output method="html" indent="yes"/>
    <xmlns:template match="/">
    <p>MMMMMMMMMMMMMMMMMMMMME</p>
    </xmlns:template>
</stylesheet>

In any case I should see     at least "<p>MMMMMMMMMMMMMMMMMMMMME</p>" on my webpage right?
Avatar of Kai77

ASKER

Btw the xslt path is correct, otherwise it would have given me an error saying it could not be found.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/">
    <p>MMMMMMMMMMMMMMMMMMMMME</p>
    </xsl:template>
</xsl:stylesheet>

make sure you use the correct syntax in writing your xslt.
Avatar of Kai77

ASKER

Have a great weekend!
Avatar of Kai77

ASKER

Just one quick question: if i wanted to output the results to a Literal control instead of the Response.OutputStream, could this be easily done?
yes,
Dim sw As New System.IO.StringWriter()
...
stylesheet.Transform(document.CreateNavigator(), Nothing, sw,Nothing)


'assuming the literal control is named XmlLiteral
XmlLiteral.Text = sw.ToString

Avatar of Kai77

ASKER

Got it! Thank you for your patience.......