Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Transforming an XML stream on the fly with XSLT

Posted on 2005-04-29
14
Medium Priority
?
365 Views
Last Modified: 2010-08-05
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()
0
Comment
Question by:Kai77
  • 7
  • 7
14 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13893451
.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)
0
 

Author Comment

by:Kai77
ID: 13893553
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.
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13893709
Dim ms As New System.IO.MemoryStream
Dim oXMLWriter As New System.Xml.XmlTextWriter(ms, System.Text.Encoding.UTF8)

...
oXmlWriter.Flush()
ms.Position = 0
Dim document As New XPathDocument(ms)
Dim stylesheet As New XslTransform
stylesheet.Load(Server.MapPath("file.xslt"))
stylesheet.Transform(document.CreateNavigator(),Nothing,Response.OutputStream)
oXmlWriter.Close()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:b1xml2
ID: 13893730
That'll get the data into the XPathDocument and then transformed and sent down to the client.
0
 

Author Comment

by:Kai77
ID: 13894152
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'
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13894170
yep, but that's just a warning. You can ignore it.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13894179
or you could just add Nothing as the last argument
stylesheet.Transform(document.CreateNavigator(),Nothing,Response.OutputStream,Nothing)
0
 

Author Comment

by:Kai77
ID: 13894224
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?
0
 

Author Comment

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

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13894290
<?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.
0
 

Author Comment

by:Kai77
ID: 13894331
Have a great weekend!
0
 

Author Comment

by:Kai77
ID: 13894422
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?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13894469
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

0
 

Author Comment

by:Kai77
ID: 13894516
Got it! Thank you for your patience.......
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question