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(R esponse.Ou tputStream , System.Text.Encoding.UTF8)
With oXMLWriter
.Formatting = System.Xml.Formatting.Inde nted
.Indentation = 4
.IndentChar = CChar(" ")
.WriteStartDocument()
.WriteStartElement("naviga tion")
Call WriteNavigationItems(oXMLW riter, PageInfo.AtaChapterID)
.WriteEndElement()
.WriteEndDocument()
End With
oXMLWriter.Flush()
oXMLWriter.Close()
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(R
With oXMLWriter
.Formatting = System.Xml.Formatting.Inde
.Indentation = 4
.IndentChar = CChar(" ")
.WriteStartDocument()
.WriteStartElement("naviga
Call WriteNavigationItems(oXMLW
.WriteEndElement()
.WriteEndDocument()
End With
oXMLWriter.Flush()
oXMLWriter.Close()
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That'll get the data into the XPathDocument and then transformed and sent down to the client.
ASKER
the code works i think,
even though I can compile i am getting the following code underline in visual studio:
stylesheet.Transform(docum ent.Create Navigator( ), Nothing, Response.OutputStream)
saying:
'Public Sub Transform(input as ...... output as system.io.stream)' is obsolete: 'You should pass XMLResolver to Transform method'
even though I can compile i am getting the following code underline in visual studio:
stylesheet.Transform(docum
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(docum ent.Create Navigator( ),Nothing, Response.O utputStrea m,Nothing)
stylesheet.Transform(docum
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?
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<
</xmlns:template>
</stylesheet>
In any case I should see at least "<p>MMMMMMMMMMMMMMMMMMMMME
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.
<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<
</xsl:template>
</xsl:stylesheet>
make sure you use the correct syntax in writing your xslt.
ASKER
Have a great weekend!
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(docum ent.Create Navigator( ), Nothing, sw,Nothing)
'assuming the literal control is named XmlLiteral
XmlLiteral.Text = sw.ToString
Dim sw As New System.IO.StringWriter()
...
stylesheet.Transform(docum
'assuming the literal control is named XmlLiteral
XmlLiteral.Text = sw.ToString
ASKER
Got it! Thank you for your patience.......
.WriteProcessingInstructio
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(R