xslt and System.ServiceModel.Syndication

P

Paul

Hi,

I am using System.ServiceModel.Syndication to create RSS feeds. I do not
know how to specify which XSL Stylesheet to associate with the feed. If you
build the XML doc "by hand" asp 2 style, you can add a line like this to the
top of the xml doc:
<?xml-stylesheet type="text/xsl" href="urlToXSLstyleSheet" version="1.0"?>

No I use code like the following to emit:

(the "feed" variable in the code is a
System.ServiceModel.Syndication.SyndicationFeed object)

Dim rssWriter As XmlWriter = New XmlTextWriter(Response.OutputStream,
Encoding.UTF8)
Dim rssFormatter As Rss20FeedFormatter = New Rss20FeedFormatter(feed)
rssFormatter.WriteTo(rssWriter)
rssWriter.Flush()
Response.ContentType = "text/xml"
Response.End()

My question: how do I specify a XSL Styelsheet for the
System.ServiceModel.Syndication.SyndicationFeed?

Thanks
Paul
 
S

Steven Cheng[MSFT]

Hi Paul,

As for the rss transforming issue, I think you may consider the following
approach:

Since you originally write the Syndication object into XMLWriter and flush
out to ASP.NET page response, you can consider first write the RSS xml into
a memoryStream(wrappered through a XmlWriter). Then, you can use .NET XSLT
api to perform tranformation on the XML document in the memorystream(read
it through a StreamReader).

Here are some information about how to use XSLT transformation classes in
..NET 2.0:

#XSLT Transformations
http://msdn2.microsoft.com/en-us/library/14689742.aspx

#XSL Transformations in .NET 2.0
http://www.15seconds.com/issue/060608.htm

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.










--------------------
 
P

Paul

Steven,

Thanks for your input. I have tried your approach, and find that I get
errors on the transform. I have tried with various data sources, like the
names of folders in my music collecection etc. I get errors like:

"'<', hexadecimal value 0x3C, is an invalid attribute character. Line 1,
position ..."
or
"There is an unclosed literal string ...

How do I deal with these errors without cancelling the whole transform?

Thanks
Paul
 
M

Martin Honnen

Paul said:
Hi,

I am using System.ServiceModel.Syndication to create RSS feeds. I do not
know how to specify which XSL Stylesheet to associate with the feed. If you
build the XML doc "by hand" asp 2 style, you can add a line like this to the
top of the xml doc:
<?xml-stylesheet type="text/xsl" href="urlToXSLstyleSheet" version="1.0"?>

No I use code like the following to emit:

(the "feed" variable in the code is a
System.ServiceModel.Syndication.SyndicationFeed object)

Dim rssWriter As XmlWriter = New XmlTextWriter(Response.OutputStream,
Encoding.UTF8)
Dim rssFormatter As Rss20FeedFormatter = New Rss20FeedFormatter(feed)
rssFormatter.WriteTo(rssWriter)
rssWriter.Flush()
Response.ContentType = "text/xml"
Response.End()

My question: how do I specify a XSL Styelsheet for the
System.ServiceModel.Syndication.SyndicationFeed?

Implement your own XmlWriter that writes out the xml-stylesheet
processing instruction. You can do that by subclassing XmlTextWriter for
instance.

Here is an example:

Public Class XmlStylesheetWriter : Inherits XmlTextWriter
Private written As Boolean
Private _type As String = "text/xsl"
Public Property Type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property

Private _href As String
Public Property Href() As String
Get
Return _href
End Get
Set(ByVal value As String)
_href = value
End Set
End Property

Public Sub New(ByVal w As System.IO.Stream, ByVal encoding As
System.Text.Encoding)
MyBase.New(w, encoding)
End Sub

Public Sub New(ByVal filename As String, ByVal encoding As
System.Text.Encoding)
MyBase.New(filename, encoding)
End Sub

Public Sub New(ByVal w As System.IO.TextWriter)
MyBase.New(w)
End Sub

Public Overrides Sub WriteStartDocument()
MyBase.WriteStartDocument()
WriteXmlStylesheetPI()
End Sub

Public Overrides Sub WriteStartDocument(ByVal standalone As
Boolean)
MyBase.WriteStartDocument(standalone)
WriteXmlStylesheetPI()
End Sub

Public Overrides Sub WriteStartElement(ByVal prefix As String,
ByVal localName As String, ByVal ns As String)
If Not (written) Then
WriteXmlStylesheetPI()
End If
MyBase.WriteStartElement(prefix, localName, ns)
End Sub

Private Sub WriteXmlStylesheetPI()
MyBase.WriteProcessingInstruction("xml-stylesheet",
String.Format("type=""{0}"" href=""{1}""", Type, Href))
written = True
End Sub
End Class


Used as follows:

Dim feed As New SyndicationFeed("Example feed", "This is an
example feed", New Uri("http://example.com/"))
Dim writer As New XmlStylesheetWriter(Console.Out)
writer.Href = "sheet.xsl"
writer.Formatting = Formatting.Indented

Dim formatter = feed.GetRss20Formatter(False)

formatter.WriteTo(writer)
writer.Close()
 
S

Steven Cheng[MSFT]

Thanks for your reply Paul,

Regarding on the new error message you mentioned, there seems exist some
certain character that make the XSLT transformor complain about. I suggest
you first save the RSS XML document(get from the Syndication object) into a
xml file and then open it in IE to see whether IE will detect any well-form
validation error. You can also try using a simple RSS collection so that
you can also open the saved XML document and check it in some text editor.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
Thread-Topic: xslt and System.ServiceModel.Syndication
thread-index: Achn8QRDXF8dmdNBRpuVMxLsXmxj0A==
Subject: RE: xslt and System.ServiceModel.Syndication
Date: Tue, 5 Feb 2008 04:17:03 -0800
 
P

Paul

Hi guys,

Thanks for the input. My problems were in the underlying memory streams,
sorry but I can't recall the detail now.

Here is the final method I used to emit from my aspx page:

Private Sub WriteOutput()
Dim feed As SyndicationFeed = New SyndicationFeed
feed = GetARSFeed()
'feed = getDummyFeed()

Dim Title As TextSyndicationContent = New
TextSyndicationContent(_FeedInfo.FeedName)
Dim Description As TextSyndicationContent = New
TextSyndicationContent(_FeedInfo.FeedDescription)
feed.Title = Title
feed.Description = Description

Select Case _FeedInfo_OutputFormat
Case "Raw RSS"
Dim memstream As New MemoryStream
Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
Dim rssFormatter As Rss20FeedFormatter = New
Rss20FeedFormatter(feed)
rssFormatter.WriteTo(rssWriter)

rssWriter.Flush()

memstream.Seek(0, SeekOrigin.Begin)
Dim xslReader As XmlReader = XmlReader.Create(memstream)

Response.ContentType = "text/xml"
Dim r As New StreamReader(memstream)
memstream.Seek(0, SeekOrigin.Begin)
Response.Write(r.ReadToEnd)
Response.End()
Case "Raw ATOM"
Dim memstream As New MemoryStream
Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
Dim atomFormatter As Atom10FeedFormatter = New
Atom10FeedFormatter(feed)
atomFormatter.WriteTo(rssWriter)

rssWriter.Flush()

memstream.Seek(0, SeekOrigin.Begin)
Dim xslReader As XmlReader = XmlReader.Create(memstream)

Response.ContentType = "text/xml"
Dim r As New StreamReader(memstream)
memstream.Seek(0, SeekOrigin.Begin)
Response.Write(r.ReadToEnd)
Response.End()
Case "RSS Presented as Formatted HTTP"
Dim memstream As New MemoryStream
Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
Dim rssFormatter As Rss20FeedFormatter = New
Rss20FeedFormatter(feed)
rssFormatter.WriteTo(rssWriter)

rssWriter.Flush()

Dim xslt As New Xsl.XslCompiledTransform()
xslt.Load(_FeedInfo.XSLTFileName)

memstream.Seek(0, SeekOrigin.Begin)
Dim xslReader As XmlReader = XmlReader.Create(memstream)

Dim newmem As New MemoryStream
xslt.Transform(xslReader, Nothing, newmem)

Response.ContentType = "text/HTML"
Dim r As New StreamReader(newmem)
newmem.Seek(0, SeekOrigin.Begin)
Response.Write(r.ReadToEnd)
Response.End()
End Select
 
S

Steven Cheng[MSFT]

Thanks for your followup.

I'm glad that you've got it working and also thanks for your sharing.

Have a nice day.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?UGF1bA==?= <[email protected]>
Subject: RE: xslt and System.ServiceModel.Syndication
Date: Mon, 11 Feb 2008 02:53:03 -0800
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top